# HP Forums

Full Version: Using fsolve() to solve non-linear system equation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have chance to use fsolve() to find numeric solution for single variable with the following options:
- specify the initial guess, and the fsolve() will return a solution (that in fact, could be many) for example fsolve(x^2+2*x-3 = 0,x=0). This will give x=1
- specify the range of solution that we need fsolve() to search for - for example fsolve(x^2 + 2*x - 3=0, x=-10..10). This will give x = -3,1

However, with 2-variable system equation, I can find solution by fsolve() by providing only initial guess e.g fsolve({x^2 + y^2 =1, x+y=1}, {x=0,y=0}). this will give x= 6.09E-16 (which is close to 0), y= 1.

But with 2-variable system equation, if I try to specify range of x, and y with the command below:
fsolve({x^2 + y^2 =1, x+y=1}, {x=-10..10,y=-10..10}), it gives error!!

Does anyone know how to let fsolve() to find solution for given range of x and y like the case above.

Thank you very much.
For univariate equations, a sign change on an interval warrants a root for a continuous fonction, it's not hard to implement a numeric solver on an interval. There is unfortunately no easy generalization for multivariate fonctions. That's why there is only an iterative implementation for generic systems, requiring a initial guess. However, polynomial systems like your system can be solved without initial guess with solve or fsolve (with [x,y] as 2nd argument).
Thank you, parisse. Got your point.

Yes, in fact, this system equation can be solved by solve(). I would like to explore fsolve() as sometimes, solve() cannot provide find all solutions.

For example, if I want to find the solution of the following system equation:
x+y =5*Pi/4
tan(x)=1
Given x,y = 0..2*Pi

solve({x+y=5*Pi/4, tan(x)=1},{x,y}) this gives [Pi/4, Pi].

However, there are some other solution too e.g [5*Pi/4, 0]. How can I find that another solution is [5*Pi/4, 0]?

Because of this, I started finding out another command to search for more complete solution.
(06-24-2019 05:53 PM)parisse Wrote: [ -> ]For univariate equations, a sign change on an interval warrants a root for a continuous fonction, it's not hard to implement a numeric solver on an interval. There is unfortunately no easy generalization for multivariate fonctions. That's why there is only an iterative implementation for generic systems, requiring a initial guess. However, polynomial systems like your system can be solved without initial guess with solve or fsolve (with [x,y] as 2nd argument).

Can a wrong initial guess eliminate the possibility of a solution? If not then why not let the machine make the guess internally?
Good starting values are important, you just have to experiment a little.

For example, fsolve([x+y=5/4*\pi, tan(x)=1],[x,y],[\pi,\pi]) returns [3.92699..., -1.01...e-15],

which is your desired second solution, or [5*\pi/4,0].
For fsolve(), yeah you are right. we need to try different initial guess. It works somehow. But it will be great if we can specify the range that fsolve() to find the solution which is available for one variable, but not multiple variables, as parisse said.

For solve({x+y=5*Pi/4, tan(x)=1},{x,y}), I though solve() will give a complete solution, as it solved symbolically.
Multivariate solve works for polynomial systems (or polynomial like). This is not the case for your system. Since your system is triangular, you can solve it by calling solve twice, first with second equation and variable x, then with 1st equation and variable y.
@parisse - got your point. Thank you.
Reference URL's
• HP Forums: https://www.hpmuseum.org/forum/index.php
• :