# Looking for solvers like Matlab's fsolve for Julia

**URL:** https://discourse.julialang.org/t/looking-for-solvers-like-matlabs-fsolve-for-julia/4101
**Category:** General Usage
**Created:** [June 6, 2017, 11:42am UTC](https://discourse.julialang.org/t/looking-for-solvers-like-matlabs-fsolve-for-julia/4101 "2017-06-06T11:42:00Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)
#### Post date: [June 6, 2017, 11:42am UTC](https://discourse.julialang.org/t/looking-for-solvers-like-matlabs-fsolve-for-julia/4101/1 "2017-06-06T11:42:00Z")

</div>

I am looking for a tutorial/package for a solver in Julia that is similar to fsolve in Matlab. My problem is mostly that I have a system of equations that I want to find a solution for. My system of equation however takes a vector of variables are given, which need to be included but are not to be solved for.

Thank you very much!

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 6, 2017, 11:59am UTC](https://discourse.julialang.org/t/looking-for-solvers-like-matlabs-fsolve-for-julia/4101/2 "2017-06-06T11:59:43Z")

</div>

NLsolve.jl

> **[GitHub - JuliaNLSolvers/NLsolve.jl: Julia solvers for systems of nonlinear...](https://github.com/JuliaNLSolvers/NLsolve.jl)**
>
> Julia solvers for systems of nonlinear equations and mixed complementarity problems - GitHub - JuliaNLSolvers/NLsolve.jl: Julia solvers for systems of nonlinear equations and mixed complementarity ...

> [@IljaK91](#):
>
> My system of equation however takes a vector of variables are given

You can use a closure for parameters. `my_closure = (t,u) -> f(t,u,2.0)` encloses `2.0`. Anonymous functions are fast in Julia so this is fine.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [June 6, 2017, 12:00pm UTC](https://discourse.julialang.org/t/looking-for-solvers-like-matlabs-fsolve-for-julia/4101/3 "2017-06-06T12:00:12Z")

</div>

> [@IljaK91](#):
>
> My system of equation however takes a vector of variables are given, which need to be included but are not to be solved for.

This is solved with a closure. If you have a function `f(x,p)` where `p` is a parameter you can easily create a new function `f2(x) = f2(x, p)` which “closes over” `p`. See [Documentation - Stack Overflow](https://stackoverflow.com/documentation/julia-lang/5724/closures#t=201706061159301647931), [https://docs.julialang.org/en/stable/devdocs/functions/#closures](https://docs.julialang.org/en/stable/devdocs/functions/#closures).

---

<div class="post-metadata">

### Author: ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)
#### Post date: [June 6, 2017, 12:08pm UTC](https://discourse.julialang.org/t/looking-for-solvers-like-matlabs-fsolve-for-julia/4101/4 "2017-06-06T12:08:19Z")

</div>

Thanks a lot 🙂

---

<div class="post-metadata">

### Author: ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)
#### Post date: [June 7, 2017, 3:11pm UTC](https://discourse.julialang.org/t/looking-for-solvers-like-matlabs-fsolve-for-julia/4101/5 "2017-06-07T15:11:20Z")

</div>

For anyone that is interested in a minimal example, although of course you don’t need NLsolve to solve those equations 🙂

```julia
using NLsolve
f! = function (x,dx,a) # we want to find roots for f
  dx[1] = x[1] - a[1]
  dx[2] = x[2] - a[2]
end
a = [1;5]
g!(x,dx) = f!(x,dx,a) # g is our closure for a specific a

res = nlsolve(g!,[1.0;1.0])
res.zero # should give out [1;5] as the solution

```

---

<div class="post-metadata">

### Author: ![vvjn](https://avatars.discourse-cdn.com/v4/letter/v/5f9b8f/32.png) [@vvjn](https://discourse.julialang.org/u/vvjn)
#### Post date: [January 31, 2018, 3:30pm UTC](https://discourse.julialang.org/t/looking-for-solvers-like-matlabs-fsolve-for-julia/4101/6 "2018-01-31T15:30:22Z")

</div>

> [@IljaK91](#):
>
> ```julia
> f! = function (x,dx,a) # we want to find roots for f
> dx[1] = x[1] - a[1]
> dx[2] = x[2] - a[2]
> end
> 
> ```

Never saw this syntax for defining generic functions before. It’s interesting

---

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [January 31, 2018, 3:43pm UTC](https://discourse.julialang.org/t/looking-for-solvers-like-matlabs-fsolve-for-julia/4101/7 "2018-01-31T15:43:44Z")

</div>

> [@vvjn](#):
>
> Never saw this syntax for defining generic functions before. It’s interesting

It’s not actually a generic function (since you can’t easily define other methods), it’s just an anonymous function bound to a variable.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 31, 2018, 4:00pm UTC](https://discourse.julialang.org/t/looking-for-solvers-like-matlabs-fsolve-for-julia/4101/8 "2018-01-31T16:00:30Z")

</div>

Note that with NLsolve’s newest version the input should be flipped so that way it’s `(dx,x)`.

---

<div class="post-metadata">

### Author: ![vvjn](https://avatars.discourse-cdn.com/v4/letter/v/5f9b8f/32.png) [@vvjn](https://discourse.julialang.org/u/vvjn)
#### Post date: [February 1, 2018, 2:52pm UTC](https://discourse.julialang.org/t/looking-for-solvers-like-matlabs-fsolve-for-julia/4101/9 "2018-02-01T14:52:30Z")

</div>

Oh yeah that’s true, it’s an anonymous function. Makes more sense, syntax wise. Got confused at first because the output said “(::#1) (generic function with 1 method)”

---

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [February 1, 2018, 4:49pm UTC](https://discourse.julialang.org/t/looking-for-solvers-like-matlabs-fsolve-for-julia/4101/10 "2018-02-01T16:49:35Z")

</div>

I should have been more careful: the machinery underlying generic and anonymous functions is the same. The main practical difference is that generic functions give a `const` binding, which allows you to add more methods, e.g. a regular binding gives:

```julia
julia> f = x -> x+1
(::#1) (generic function with 1 method)

julia> f(x,y) = 2
ERROR: cannot define function f; it already has a value

```

But if you make it `const`:

```julia
julia> const f = x -> x+1
(::#1) (generic function with 1 method)

julia> f(x,y) = 2
(::#1) (generic function with 2 methods)

```

The only remaining difference between this and the usual definition is the lack of a function name (which is really only for user convenience).
