# 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:** 10\
**Page:** 2

<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:23pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/21 "2020-09-06T15:23:41Z")

</div>

Yes, sure, there are great packages in Julia for symbolic manipulation, but the context here is, e.g., when you’re teaching engineering students to solve an electrical circuit in the simplest possible way. I want to completely replace MATLAB in this case by Julia, without introdicung complexities the students don’t care about. They want to answer the simple question of just give me the currents in the circuit.

---

<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:41pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/22 "2020-09-06T15:41:56Z")

</div>

> [@Seif\_Shebl](#):
>
> want to completely replace MATLAB in this case by Julia, without introdicung complexities the students don’t care about.

Just use vectors/matrices then — more compact, less error-prone. I imagine most EE students should be able to handle elementary linear algebra.

If you want a CAS without using packages, then that’s not possible in Julia.

---

<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, 4:57pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/23 "2020-09-06T16:57:03Z")

</div>

> [@HerAdri](#):
>
> The expected results are:  
> x=-33/37 =\> -0.8918918918918919  
> y=99/37 =\> 2.675675675675676  
> z=24/37 =\>0.6486486486486487

I mean, did you even test it yourself? I ran your code, I checked `G(x)`, it was zero with the result from the routine and non-zero with what you provided. Are you really so sure that you didn’t make a typo that you don’t have to check it yourself, even when both the computer and a human says that the solution you got is in fact correct?

---

<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, 6:53pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/24 "2020-09-06T18:53:30Z")

</div>

Above we can find 4 different proposals with which we obtain the expected results. And I don’t realize what that difference is!  
links:  
[20](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/20)  
[8](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/8)  
[14](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/14)  
[17](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/17)

---

<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, 7:01pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/25 "2020-09-06T19:01:27Z")

</div>

It was already said:

> [@dpsanders](#):
>
> 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, 7:16pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/26 "2020-09-06T19:16:07Z")

</div>

You are absolutely right. I would be wrong 'the index gave unsuccessful result.  
Here it is already corrected and we obtain the expected results.

```julia
G(x) = [3x[1] + 5x[2] + 2x[3] - 12, 2x[1] + 5x[2] - 4x[3] - 9, 4x[1] - x[2] + 5x[3] + 3]
22:09:46->>G (generic function with 1 method)
Julia>J = ForwardDiff.jacobian(G, [0,0,0])
22:09:47->>3×3 Array{Int64,2}:
 3 5 2
 2 5 -4
 4 -1 5
Julia>x = -J \ G([0,0,0])
22:09:49->>3-element Array{Float64,1}:
 -0.8918918918918918
  2.675675675675676
  0.6486486486486486
Rational.( -J) \ Rational.(G([0,0,0]))
22:18:58->>3-element Array{Rational{Int64},1}:
 -33//37
  99//37
  24//37

```

Thank you very much for allowing me happy dreams!

---

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

</div>

> [@simeonschaub](#):
>
> [ModellingToolkit.jl](https://mtk.sciml.ai/stable/) also works quite well for this kind of manipulation:
> 
> ```julia
> 
> ```

Note that @shashi added a `solve_for` function last week. Still not documented though.

---

<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, 9:39pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/28 "2020-09-06T21:39:17Z")

</div>

Oh, that’s awesome news! Is that also able to handle more complex non-linear equations?

---

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

</div>

Very very soon.

---

<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 7, 2020, 7:17am UTC](https://discourse.julialang.org/t/julia-equivalent-for-python-scipy-optimize-fsolve/46139/30 "2020-09-07T07:17:09Z")

</div>

> [@kristoffer.carlsson](#):
>
> It was already said:
> 
> > [@dpsanders](#):
> >
> > You have `5x[1]` instead of `5x[2]`

Which kind of underlies the point: don’t use the general form in code if it can be avoided, it is very error-prone. Use linear algebra notation, especially for linear problems.

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