Error with function, no methods matching

Hi, I’ve been working on a program recently but I can’t find where I went wrong, I need help.
It’s a simple program that makes recurrences.
Here’s my code:

using NLsolve

function systeme!(S,x)
    S[1]=f(f(x[1],x[2],x[3],x[4]),g(x[1],x[2],x[3],x[4]))-x[1]
    S[2]=g(f(x[1],x[2],x[3],x[4]),g(x[1],x[2],x[3],x[4]))-x[2]
end


function fonction(f,g,k,x,y,a,b)
    for i in 1:k
        x_temp=x
        x=f(x,y,a,b)
        y=g(x_temp,y,a,b)
    end    
   
    xyab=[x,y,a,b]
    sol=nlsolve(systeme!,xyab)
    return (x,y),sol
end        

f(x,y,a,b)=x^2.0+y
g(x,y,a,b)=x-2

fonction(f,g,3.0,1.4142,-0.5858,0.1,0.2)

Here’s what is returned in the REPL with the entire stacktrace:

ERROR: LoadError: MethodError: no method matching f(::Float64, ::Float64)
Closest candidates are:
  f(::Any, ::Any, ::Any, ::Any) at c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:21
Stacktrace:
  [1] systeme!(S::Vector{Float64}, x::Vector{Float64})
    @ Main c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:4
  [2] (::NLSolversBase.var"#fj_finitediff!#21"{typeof(systeme!), FiniteDiff.JacobianCache{Vector{Float64}, Vector{Float64}, Vector{Float64}, UnitRange{Int64}, Nothing, Val{:central}(), Float64}})(F::Vector{Float64}, J:M 
atrix{Float64}, x::Vector{Float64})
    @ NLSolversBase C:\Users\Maxime\.julia\packages\NLSolversBase\geyh3\src\objective_types\oncedifferentiable.jl:138
  [3] value_jacobian!!(obj::OnceDifferentiable{Vector{Float64}, Matrix{Float64}, Vector{Float64}}, F::Vector{Float64}, J::Matrix{Float64}, x::Vector{Float64})
    @ NLSolversBase C:\Users\Maxime\.julia\packages\NLSolversBase\geyh3\src\interface.jl:124
  [4] value_jacobian!!
    @ C:\Users\Maxime\.julia\packages\NLSolversBase\geyh3\src\interface.jl:122 [inlined]
  [5] trust_region_(df::OnceDifferentiable{Vector{Float64}, Matrix{Float64}, Vector{Float64}}, initial_x::Vector{Float64}, xtol::Float64, ftol::Float64, iterations::Int64, store_trace::Bool, show_trace::Bool, extended_trace::Bool, factor::Float64, autoscale::Bool, cache::NLsolve.NewtonTrustRegionCache{Vector{Float64}})
    @ NLsolve C:\Users\Maxime\.julia\packages\NLsolve\gJL1I\src\solvers\trust_region.jl:119
  [6] trust_region (repeats 2 times)
    @ C:\Users\Maxime\.julia\packages\NLsolve\gJL1I\src\solvers\trust_region.jl:235 [inlined]
  [7] nlsolve(df::OnceDifferentiable{Vector{Float64}, Matrix{Float64}, Vector{Float64}}, initial_x::Vector{Float64}; method::Symbol, xtol::Float64, ftol::Float64, iterations::Int64, store_trace::Bool, show_trace::Bool, extended_trace::Bool, linesearch::Static, linsolve::NLsolve.var"#29#31", factor::Float64, autoscale::Bool, m::Int64, beta::Int64, aa_start::Int64, droptol::Float64)
    @ NLsolve C:\Users\Maxime\.julia\packages\NLsolve\gJL1I\src\nlsolve\nlsolve.jl:26
  [8] nlsolve(f::Function, initial_x::Vector{Float64}; method::Symbol, autodiff::Symbol, inplace::Bool, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ NLsolve C:\Users\Maxime\.julia\packages\NLsolve\gJL1I\src\nlsolve\nlsolve.jl:52
  [9] nlsolve(f::Function, initial_x::Vector{Float64})
    @ NLsolve C:\Users\Maxime\.julia\packages\NLsolve\gJL1I\src\nlsolve\nlsolve.jl:46
 [10] fonction(f::typeof(f), g::typeof(g), k::Float64, x::Float64, y::Float64, a::Float64, b::Float64)
    @ Main c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:17
 [11] top-level scope
    @ c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:24
in expression starting at c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:24

I’m kinda new to this language and it seems hard for me to understand some errors. Thanks in advance.

In systeme!, you call f with two arguments, the returned values of the inner calls.

Written like this it’s more clear where you’re going wrong:

function systeme!(S,x)
    a = f(x[1],x[2],x[3],x[4])
    b = g(x[1],x[2],x[3],x[4])
    S[1]=f(a,b)-x[1]
    S[2]=g(a,b)-x[2]
end

Since you only defined f and g with four arguments, calling them with only two naturally won’t work.

3 Likes

On a different note, this does probably not do what you want it to do. Here, x_temp will refer to the same exact x, not a copy of it.

1 Like

I tried using what you wrote, I think the error is quite the same actually:

ERROR: LoadError: MethodError: no method matching f(::Float64, ::Float64)
Closest candidates are:
  f(::Any, ::Any, ::Any, ::Any) at c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:23
Stacktrace:
  [1] systeme!(S::Vector{Float64}, x::Vector{Float64})
    @ Main c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:6
  [2] (::NLSolversBase.var"#fj_finitediff!#21"{typeof(systeme!), FiniteDiff.JacobianCache{Vector{Float64}, Vector{Float64}, Vector{Float64}, UnitRange{Int64}, Nothing, Val{:central}(), Float64}})(F::Vector{Float64}, J::Matrix{Float64}, x::Vector{Float64})
    @ NLSolversBase C:\Users\Maxime\.julia\packages\NLSolversBase\geyh3\src\objective_types\oncedifferentiable.jl:138
  [3] value_jacobian!!(obj::OnceDifferentiable{Vector{Float64}, Matrix{Float64}, Vector{Float64}}, F::Vector{Float64}, J::Matrix{Float64}, x::Vector{Float64})
    @ NLSolversBase C:\Users\Maxime\.julia\packages\NLSolversBase\geyh3\src\interface.jl:124
  [4] value_jacobian!!
    @ C:\Users\Maxime\.julia\packages\NLSolversBase\geyh3\src\interface.jl:122 [inlined]
  [5] trust_region_(df::OnceDifferentiable{Vector{Float64}, Matrix{Float64}, Vector{Float64}}, initial_x::Vector{Float64}, xtol::Float64, ftol::Float64, iterations::Int64, store_trace::Bool, show_trace::Bool, extended_trace::Bool, factor::Float64, autoscale::Bool, cache::NLsolve.NewtonTrustRegionCache{Vector{Float64}})
    @ NLsolve C:\Users\Maxime\.julia\packages\NLsolve\gJL1I\src\solvers\trust_region.jl:119
  [6] trust_region (repeats 2 times)
    @ C:\Users\Maxime\.julia\packages\NLsolve\gJL1I\src\solvers\trust_region.jl:235 [inlined]
  [7] nlsolve(df::OnceDifferentiable{Vector{Float64}, Matrix{Float64}, Vector{Float64}}, initial_x::Vector{Float64}; method::Symbol, xtol::Float64, ftol::Float64, iterations::Int64, store_trace::Bool, show_trace::Bool, extended_trace::Bool, linesearch::Static, linsolve::NLsolve.var"#29#31", factor::Float64, autoscale::Bool, m::Int64, beta::Int64, aa_start::Int64, droptol::Float64)
    @ NLsolve C:\Users\Maxime\.julia\packages\NLsolve\gJL1I\src\nlsolve\nlsolve.jl:26
  [8] nlsolve(f::Function, initial_x::Vector{Float64}; method::Symbol, autodiff::Symbol, inplace::Bool, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ NLsolve C:\Users\Maxime\.julia\packages\NLsolve\gJL1I\src\nlsolve\nlsolve.jl:52
  [9] nlsolve(f::Function, initial_x::Vector{Float64})
    @ NLsolve C:\Users\Maxime\.julia\packages\NLsolve\gJL1I\src\nlsolve\nlsolve.jl:46
 [10] fonction(f::typeof(f), g::typeof(g), k::Float64, x::Float64, y::Float64, a::Float64, b::Float64)
    @ Main c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:19
 [11] top-level scope
    @ c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:26
in expression starting at c:\Users\Maxime\Desktop\Stage_2021\Julia\testFontction.jl:26

I wanted to save a copy of x, because it will be replaced by x=f(x,y,a,b)

Indeed, that’s the exact same error. What I’m trying to tell you with the code I wrote is that your function f is only defined for 4 arguments, but your code is calling it with only 2. This won’t work. I’ve simply rearranged your code to make it more obvious where the error occurs, the two are functionally equivalent.

1 Like

Oh ok I see, now it’s working, thank you so much for helping me !