# Julia equivalent for python scipy.optimize.fsolve

**URL:** <https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139>\
**Category:** General Usage\
**Tags:** package, roots\
**Created:** [September 6, 2020, 12:09pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139 "2020-09-06T12:09:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [September 6, 2020, 12:09pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/1 "2020-09-06T12:09:12Z")

</div>

What would be the Julia equivalent for python scipy.optimize.fsolve,  
to resolve

```julia
function F (x)
  f1 = 1- x [1] - x [2]
  f2 = 8 - x [1] - 3 * x [2]
  return f1, f2
end

x0 = [1,1]

```

Solves a linear system of equations set in not matrix notation

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 6, 2020, 12:27pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/2 "2020-09-06T12:27:03Z")

</div>

Have you tried a simple search for “optimization in Julia”? There are so many packages out there that it is hard to suggest just one solution. Start looking at Optim.jl, BlackBoxOptim.jl, …

---

<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:** [September 6, 2020, 12:29pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/3 "2020-09-06T12:29:17Z")

</div>

> [@HerAdri](#):
>
> Solves a linear system of equations set in not matrix notation

What do you mean “set in not matrix notation”. If you have a function like `F` just pass `x -> collect(F(x))` to the solver, if the solver expects an array to be returned. Or do you mean it is a general non linear system?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 6, 2020, 12:36pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/4 "2020-09-06T12:36:13Z")

</div>

> [@HerAdri](#):
>
> Solves a linear system of equations

If your system is _linear_, you really want `\`.

> [@HerAdri](#):
>
> set in not matrix notation

You can either

1. rewrite,
2. or if you really insist, recover A x = b from a few evaluations of `F` (eg `b = F(zeros(2))`, etc).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 6, 2020, 12:54pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/5 "2020-09-06T12:54:56Z")

</div>

> [@HerAdri](#):
>
> What would be the Julia equivalent for python scipy.optimize.fsolve

There a several options, I think, but the [NLsolve.jl package](https://github.com/JuliaNLSolvers/NLsolve.jl) is one possibility:

```julia
julia> using NLsolve

julia> function F!(F, x)
         F[1] = 1 - x[1] - x[2]
         F[2] = 8 - x[1] - 3x[2]
       end

julia> result = nlsolve(F!, [1.0,1.0], autodiff=:forward)
Results of Nonlinear Solver Algorithm
 * Algorithm: Trust-region with dogleg and autoscaling
 * Starting Point: [1.0, 1.0]
 * Zero: [-2.5, 3.5]
 * Inf-norm of residuals: 0.000000
 * Iterations: 2
 * Convergence: true
   * |x - x'| < 0.0e+00: false
   * |f(x)| < 1.0e-08: true
 * Function Calls (f): 3
 * Jacobian Calls (df/dx): 3

julia> result.zero
2-element Vector{Float64}:
 -2.5
  3.5

```

Of course, if you _know_ that your equations are linear, you should really put them into some kind of matrix and use `\`:

```julia
julia> [1 1; 1 3] \ [1, 8]
2-element Vector{Float64}:
 -2.5
  3.5

```

but perhaps your real problem is nonlinear and you intended this `F` as a toy problem to figure out the library?

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [September 6, 2020, 12:55pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/6 "2020-09-06T12:55:07Z")

</div>

> [@juliohm](#):
>
> Optim.jl, BlackBoxOptim.jl

**A. Solving Linear Systems**

Here is a simple example which we can solve quite easily using the solve command.  
sys := { 3_x + 5_y + 2_z = 12, 2_x + 5_y - 4_z = 9,4_x - y + 5_z = -3};  
**solve( sys, {x,y,z});**

Or Exist in Julia a function or package that Convert a System of Linear Equations to Matrix Form?  
Given a system of linear equations, determine the associated augmented matrix

Augmented Matrix for a Linear System  
linear equations:

[3_x + 5_y + 2_z = 12, 2_x + 5_y - 4_z = 9,4_x - y + 5_z = -3]  
List of variables:

[x,y,z]

Augmented matrix:  
LinearAlgebraGenerateMatrix  
3 5 2 12  
2 5 -4 9  
4 -1 5 -3

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 6, 2020, 12:59pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/7 "2020-09-06T12:59:11Z")

</div>

> [@HerAdri](#):
>
> Or Exist in Julia a function or package that Convert a System of Linear Equations to Matrix Form?

Yes, if you use `NLsolve` as in my [post above](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/5) with `autodiff=:forward` then it uses [automatic differentiation](https://en.wikipedia.org/wiki/Automatic_differentiation) to form the Jacobian matrix for Newton iterations. If you have a linear system of equations, the Jacobian matrix is exactly the matrix form of the problem, and Newton iterations will converge in a single `\` step.

And if you know that your equations are linear, you can do the differentiation directly using the [ForwardDiff.jl package](https://github.com/JuliaDiff/ForwardDiff.jl):

```julia
julia> using ForwardDiff

julia> F(x) = [1- x[1] - x[2], 8 - x[1] - 3*x[2]]
F (generic function with 1 method)

julia> J = ForwardDiff.jacobian(F, [0,0])
2×2 Array{Int64,2}:
 -1 -1
 -1 -3

julia> x = -J \ F([0,0])
2-element Array{Float64,1}:
 -2.5
  3.5

```

Here, the Jacobian `J` corresponds to the “matrix form” of your problem for a right-hand-side of `-F([0,0])`, and appropriate use of `\` (equivalent to a single Newton step) gives the same solution `x` as above.

PS. [Please quote your code](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/75300).

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 6, 2020, 1:27pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/8 "2020-09-06T13:27:54Z")

</div>

[ModellingToolkit.jl](https://mtk.sciml.ai/stable/) also works quite well for this kind of manipulation:

```julia
julia> using ModelingToolkit

julia> @variables x y z
(x, y, z)

julia> eqs = [3x + 5y + 2z - 12, 2x + 5y - 4z - 9, 4x - y + 5z + 3]
3-element Array{Operation,1}:
 ((3x + 5y) + 2z) - 12
  ((2x + 5y) - 4z) - 9
   ((4x - y) + 5z) + 3

julia> M = get.(ModelingToolkit.jacobian(eqs, [x, y, z]))
3×3 Array{Int64,2}:
 3 5 2
 2 5 -4
 4 -1 5

julia> b = get.(substitute.(eqs, Ref([x, y, z] .=> 0)))
3-element Array{Int64,1}:
 -12
  -9
   3

julia> M \ -b
3-element Array{Float64,1}:
 -0.8918918918918918
  2.675675675675676
  0.6486486486486486

julia> substitute.(eqs, Ref([x, y, z] .=> ans))
3-element Array{ModelingToolkit.Constant,1}:
 ModelingToolkit.Constant(0.0)
 ModelingToolkit.Constant(0.0)
 ModelingToolkit.Constant(0.0)

```

To explicitely get the augmented matrix, you can just do the following:

```julia
julia> [M -b]
3×4 Array{Int64,2}:
 3 5 2 12
 2 5 -4 9
 4 -1 5 -3

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 6, 2020, 1:33pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/9 "2020-09-06T13:33:00Z")

</div>

Seeing lots of great options. I didn’t JuMP mentioned yet for solving larger linear programs, so here it is:

```julia
using JuMP, GLPK

m = model(GLPK.Optimizer)
@variable(m, x)
@variable(m, y)
@variable(m, z)
@constraints(m, begin
    3x + 5y + 2z == 12,
    2x + 5y - 4z == 9,
    4x - y + 5z == -3
end)
optimize!(m)
value.((x,y,z))

```

Where there above is just about the most verbose way to write it, but oh well 😜

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [September 6, 2020, 2:31pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/10 "2020-09-06T14:31:20Z")

</div>

```julia
Julia>using JuMP, GLPK
[Info: Precompiling JuMP [4076af6c-e467-56ae-b986-b466b2749572]
WARNING: using JuMP.@variables in module Main conflicts with an existing identifier.
[Info: Precompiling GLPK [60bf3e95-4087-53dc-ae20-288a0d20c6a6]

Julia>m = model(GLPK.Optimizer)
ERROR: UndefVarError: model not defined
Stacktrace:
 [1] top-level scope at REPL[177]:1
 [2] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1088

```

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [September 6, 2020, 2:46pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/11 "2020-09-06T14:46:31Z")

</div>

```julia
G(x) = [3x[1] + 5x[1] + 2x[3] - 12, 2x[1] + 5x[2] - 4x[3] - 9, 4x[1] - x[2] + 5x[3] + 3]
J = ForwardDiff.jacobian(G, [0,0,0])
x = -J \ G([0,0,0])
7:36:28->>3-element Array{Float64,1}:
  2.129032258064516
 -1.064516129032258
 -2.5161290322580645

```

**The expected results are:**  
x=-33/37 =\> -0.8918918918918919  
y=99/37 =\> 2.675675675675676  
z=24/37 =\>0.6486486486486487

---

<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:** [September 6, 2020, 2:51pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/12 "2020-09-06T14:51:31Z")

</div>

> [@HerAdri](#):
>
> **The expected results are:**  
> …

Why? `G(x)` gives you zero and using `x = [-33/37, 99/37, 24/37]` does not give a zero.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 6, 2020, 2:55pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/13 "2020-09-06T14:55:30Z")

</div>

> [@HerAdri](#):
>
> ```julia
> Julia>m = model(GLPK.Optimizer)
> 
> ```

apologies. It should be `Model`, not `model`

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [September 6, 2020, 2:58pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/14 "2020-09-06T14:58:38Z")

</div>

In MATLAB, this is done in a single line of code, I also wonder if there is a similar thing in Julia:

```julia
>> syms x y z
>> [x,y,z] = solve(3*x + 5*y + 2*z == 12, 2*x + 5*y - 4*z == 9,4*x - y + 5*z == -3)
 
x =
-33/37
 
 y =
99/37

z =
24/37

```

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [September 6, 2020, 2:58pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/15 "2020-09-06T14:58:46Z")

</div>

the result shown in  
[link](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/8)

```julia
M \ -b
3-element Array{Float64,1}:
 -0.8918918918918918
  2.675675675675676
  0.6486486486486486

```

The expected results are:  
x=-33/37 =\> -0.8918918918918919  
y=99/37 =\> 2.675675675675676  
z=24/37 =\>0.6486486486486487

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 6, 2020, 2:59pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/16 "2020-09-06T14:59:27Z")

</div>

You have `5x[1]` instead of `5x[2]`

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [September 6, 2020, 3:03pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/17 "2020-09-06T15:03:28Z")

</div>

sys := { 3_x + 5_y + 2_z = 12, 2_x + 5_y - 4_z = 9,  
4_x - y + 5_z = -3};

![sys := {3x+5y+2z = 12, 2x+5y-4z = 9, 4x-y+5...](https://global.discourse-cdn.com/julialang/original/3X/7/6/7693684930413bd89a2a91ce58661fce81b7cd75.gif)

> **solve( sys, {x,y,z});**

![{y = 99/37, z = 24/37, x = -33/37}](https://global.discourse-cdn.com/julialang/original/3X/6/8/687133690901ede247436bf87f460143fa365e33.gif)  
Maple will automatically use fractions. However, you can force decimal answers using the evalf command.

> **evalf(%);**

![{y = 2.675675676, z = .6486486486, x = -.8918918919...](https://global.discourse-cdn.com/julialang/original/3X/4/e/4e64968239099c35ae6587a58cfb751641e6afea.gif)

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [September 6, 2020, 3:05pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/18 "2020-09-06T15:05:30Z")

</div>

```julia
Julia>m = Model(GLPK.Optimizer)
ERROR: MethodError: no method matching Model(::Type{GLPK.Optimizer})
Closest candidates are:
  Model(::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any) at C:\Users\hermesr\.julia\packages\JuMP\iGamg\src\JuMP.jl:126
  Model(; caching_mode, solver) at C:\Users\hermesr\.julia\packages\JuMP\iGamg\src\JuMP.jl:161
  Model(::MathOptInterface.AbstractOptimizer, ::Dict{MathOptInterface.ConstraintIndex,AbstractShape}, ::Set{Any}, ::Any, ::Any, ::Dict{Symbol,Any}, ::Int64, ::Dict{Symbol,Any}) at C:\Users\hermesr\.julia\packages\JuMP\iGamg\src\JuMP.jl:126
  ...
Stacktrace:
 [1] top-level scope at REPL[189]:1
 [2] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1088

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [September 6, 2020, 3:05pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/19 "2020-09-06T15:05:49Z")

</div>

Yes, in MATLAB, you use `double([x y z])` to get decimal answer.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 6, 2020, 3:10pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/20 "2020-09-06T15:10:26Z")

</div>

> [@Seif\_Shebl](#):
>
> I also wonder if there is a similar thing in Julia

There are various packages for symbolic manipulations, but that’s probably overkill for this application.

I am curious about the context here. If one is talking about a _single_ linear system, it is trivial to transcribe it to a matrix form. If, on the other hand, one needs to solve a lot of linear systems, it is hard to imagine that they come in the general form and not as a matrix equation.

> [@HerAdri](#):
>
> Maple will automatically use fractions.

```julia
julia> A = [3 5 2;
       2 5 -4;
       4 -1 5];

julia> b = [12, 9, -3];

julia> Rational.(A) \ Rational.(b)
3-element Array{Rational{Int64},1}:
 -33//37
  99//37
  24//37

```

You may want to use `Rational{BigInt}` in general though.

That said, I am wondering if you misunderstand what Julia is for: while various packages address this, Julia in general is not a CAS like Maple, Mathematica, or Maxima.

[Next page](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139.md?page=2)
