ERROR: LoadError: MethodError: no method matching +(::Array{Float64,1}, ::Int64) For element-wise addition, use broadcasting with dot syntax: array .+ scalar

Write the function periodic which takes as its input the vector t , vector c which contains the vector of coefficents ccc in the order as above, the integers d_poly and d_periodic corresponding to the order of the polynomial and periodic expansions, and the period T , and returns the following:

I simply want to create a period function ( polynomial plus cosine and sine function ).

function poly_periodic(
t::AbstractVector,
c::AbstractVector,
d_poly::Integer,
d_periodic::Integer,
T::Number
)

m = length(t)
y = zeros(m)

# Step 1: Extact polynomial and periodic coefficients
 c_poly = c[1:d_poly]

c_periodic = c[length(c_poly+1):end]
@assert length(c_periodic) == 2 * d_periodic "Number of periodic coeffs does not match 2 * d_periodic"

# Step 2: Compute the polynomial part 
for i in 0:d_poly
   y .+= c_poly[i+1]*t.^i
end

# Step 3: Compute the periodic part
for i in 1:d_periodic
    y .+= c_periodic[2*i-1]*cos.(2*i*pi*t/T) # cosine term

    y .+= c_periodic[2*i]*sin.(2*i*pi*t/T ) # sine term
end

return y

end
The error message is belowERROR: LoadError: MethodError: no method matching +(::Array{Float64,1}, ::Int64)
For element-wise addition, use broadcasting with dot syntax: array .+ scalar
Closest candidates are:
+(::Any, ::Any, !Matched::Any, !Matched::Any…) at operators.jl:538
+(!Matched::Complex{Bool}, ::Real) at complex.jl:301
+(!Matched::BigFloat, ::Union{Int16, Int32, Int64, Int8}) at mpfr.jl:378

what’s not clear about the error message?

1 Like

Thanks Jling, I am new to Julia. I got difficulty in which part of my code I need to edit.

This is the error. I think you want length(c_poly)+1 here?

(You also won’t get ideal performance from this style of code, but that’s a separate issue.)

1 Like

Many thanks, for your good spot. It worked.