You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am new to julia and I am still trying to learn about it.
I tried to use a user-defined function in my constraint
This is the function
using PyPlot
function LagrangePolynomial(x::T,vecx::Vector{T},vecy::Vector{T}) where T <: AbstractFloat # define an infinitely many large set of functions
n = length(vecx)
length(vecy)==n || error("Incompatible vector size")
S = zero(T)
for i ∈ 1:n
P = one(T)
for j ∈ 1:n
if j != i
P*=(x-vecx[j])/(vecx[i]-vecx[j])
end #chiude l'if
end #chiude il ciclo for interno
S += vecy[i]*P
end #chiude il ciclo for esterno
return S
end
Thus I tried to apply this function in my model:
f(x) = 1/sqrt(2π)*exp(-x^2/2);
a=-2; #estremi dell' intervallo
b = 2;
n = 12 #number of interpolating points
vecx = LinRange(a,b,n); #suddivisione dell' intervallo [a,b] in n punti equispaziati
xinterval = LinRange(a,b,1024); #Una suddivisione più precisa dell' intervallo
ypoly = [LagrangePolynomial(x,vecx,f.(vecx)) for x in xinterval]
plot(xinterval, ypoly)
plot(xinterval, f.(xinterval), "k");
plot(vecx, f.(vecx), "ro");
plt[:legend](["f(x)","interpolation points",L"L_{n-1}(x)"]
and I got this error:
MethodError: no method matching LagrangePolynomial(::Float64, ::LinRange{Float64}, ::Array{Float64,1})
Closest candidates are:
LagrangePolynomial(::T, !Matched::Array{T,1}, ::Array{T,1}) where T<:AbstractFloat at In[8]:3
Stacktrace:
[1] (::var"#11#12")(::Float64) at .\none:0
[2] iterate at .\generator.jl:47 [inlined]
[3] collect(::Base.Generator{LinRange{Float64},var"#11#12"}) at .\array.jl:686
[4] top-level scope at In[8]:25
[5] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
[6] execute_code(::String, ::String) at C:\Users\vinci.julia\packages\IJulia\a1SNk\src\execute_request.jl:27
[7] execute_request(::ZMQ.Socket, ::IJulia.Msg) at C:\Users\vinci.julia\packages\IJulia\a1SNk\src\execute_request.jl:86
[8] #invokelatest#1 at .\essentials.jl:710 [inlined]
[9] invokelatest at .\essentials.jl:709 [inlined]
[10] eventloop(::ZMQ.Socket) at C:\Users\vinci.julia\packages\IJulia\a1SNk\src\eventloop.jl:8
[11] (::IJulia.var"#15#18")() at .\task.jl:356
I realized that it might have to do something with the data types and array stuff?
Please help
The text was updated successfully, but these errors were encountered:
I am new to julia and I am still trying to learn about it.
I tried to use a user-defined function in my constraint
This is the function
using PyPlot
function LagrangePolynomial(x::T,vecx::Vector{T},vecy::Vector{T}) where T <: AbstractFloat # define an infinitely many large set of functions
n = length(vecx)
length(vecy)==n || error("Incompatible vector size")
S = zero(T)
for i ∈ 1:n
P = one(T)
for j ∈ 1:n
if j != i
P*=(x-vecx[j])/(vecx[i]-vecx[j])
end #chiude l'if
end #chiude il ciclo for interno
S += vecy[i]*P
end #chiude il ciclo for esterno
return S
end
Thus I tried to apply this function in my model:
f(x) = 1/sqrt(2π)*exp(-x^2/2);
a=-2; #estremi dell' intervallo
b = 2;
n = 12 #number of interpolating points
vecx = LinRange(a,b,n); #suddivisione dell' intervallo [a,b] in n punti equispaziati
xinterval = LinRange(a,b,1024); #Una suddivisione più precisa dell' intervallo
ypoly = [LagrangePolynomial(x,vecx,f.(vecx)) for x in xinterval]
plot(xinterval, ypoly)
plot(xinterval, f.(xinterval), "k");
plot(vecx, f.(vecx), "ro");
plt[:legend](["f(x)","interpolation points",L"L_{n-1}(x)"]
and I got this error:
MethodError: no method matching LagrangePolynomial(::Float64, ::LinRange{Float64}, ::Array{Float64,1})
Closest candidates are:
LagrangePolynomial(::T, !Matched::Array{T,1}, ::Array{T,1}) where T<:AbstractFloat at In[8]:3
Stacktrace:
[1] (::var"#11#12")(::Float64) at .\none:0
[2] iterate at .\generator.jl:47 [inlined]
[3] collect(::Base.Generator{LinRange{Float64},var"#11#12"}) at .\array.jl:686
[4] top-level scope at In[8]:25
[5] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
[6] execute_code(::String, ::String) at C:\Users\vinci.julia\packages\IJulia\a1SNk\src\execute_request.jl:27
[7] execute_request(::ZMQ.Socket, ::IJulia.Msg) at C:\Users\vinci.julia\packages\IJulia\a1SNk\src\execute_request.jl:86
[8] #invokelatest#1 at .\essentials.jl:710 [inlined]
[9] invokelatest at .\essentials.jl:709 [inlined]
[10] eventloop(::ZMQ.Socket) at C:\Users\vinci.julia\packages\IJulia\a1SNk\src\eventloop.jl:8
[11] (::IJulia.var"#15#18")() at .\task.jl:356
I realized that it might have to do something with the data types and array stuff?
Please help
The text was updated successfully, but these errors were encountered: