Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

MethodError: no method matching LagrangePolynomial(::Float64, ::LinRange{Float64}, ::Array{Float64,1}) #38478

Closed
veenz98 opened this issue Nov 18, 2020 · 1 comment

Comments

@veenz98
Copy link

veenz98 commented Nov 18, 2020

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

@veenz98 veenz98 closed this as completed Nov 18, 2020
@veenz98 veenz98 reopened this Nov 18, 2020
@fredrikekre
Copy link
Member

Please ask questions on https://discourse.julialang.org/.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants