# Error with function, no methods matching

**URL:** <https://discourse.julialang.org/t/error-with-function-no-methods-matching/64489>\
**Category:** New to Julia\
**Tags:** functions\
**Created:** [July 12, 2021, 7:25am UTC](https://discourse.julialang.org/t/error-with-function-no-methods-matching/64489 "2021-07-12T07:25:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Broti](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Broti](https://discourse.julialang.org/u/Broti)\
**Post date:** [July 12, 2021, 7:25am UTC](https://discourse.julialang.org/t/error-with-function-no-methods-matching/64489/1 "2021-07-12T07:25:55Z")

</div>

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:

```julia
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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 12, 2021, 7:28am UTC](https://discourse.julialang.org/t/error-with-function-no-methods-matching/64489/2 "2021-07-12T07:28:54Z")

</div>

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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 12, 2021, 7:30am UTC](https://discourse.julialang.org/t/error-with-function-no-methods-matching/64489/3 "2021-07-12T07:30:12Z")

</div>

> [@Broti](#):
>
> `x_temp=x`

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.

---

<div class="post-metadata">

**Author:** ![Broti](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Broti](https://discourse.julialang.org/u/Broti)\
**Post date:** [July 12, 2021, 7:35am UTC](https://discourse.julialang.org/t/error-with-function-no-methods-matching/64489/4 "2021-07-12T07:35:08Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Broti](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Broti](https://discourse.julialang.org/u/Broti)\
**Post date:** [July 12, 2021, 7:35am UTC](https://discourse.julialang.org/t/error-with-function-no-methods-matching/64489/5 "2021-07-12T07:35:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 12, 2021, 7:40am UTC](https://discourse.julialang.org/t/error-with-function-no-methods-matching/64489/6 "2021-07-12T07:40:30Z")

</div>

> [@Broti](#):
>
> I tried using what you wrote, I think the error is quite the same actually

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.

---

<div class="post-metadata">

**Author:** ![Broti](https://avatars.discourse-cdn.com/v4/letter/b/977dab/32.png) [@Broti](https://discourse.julialang.org/u/Broti)\
**Post date:** [July 12, 2021, 7:47am UTC](https://discourse.julialang.org/t/error-with-function-no-methods-matching/64489/7 "2021-07-12T07:47:16Z")

</div>

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