ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 14 Feb 2017 11:51:06 +0100Solve a simple system of non-linear equationshttps://ask.sagemath.org/question/8557/solve-a-simple-system-of-non-linear-equations/Maple can solve a system of equations such as $\sin x + y =0, \sin x - y =0$. However,
var('x y')
solve([sin(x) + y ==0, sin(x) - y==0], [x, y])
produces no useful answer.
Is there any other way to proceed?Thu, 15 Dec 2011 09:30:18 +0100https://ask.sagemath.org/question/8557/solve-a-simple-system-of-non-linear-equations/Answer by maaaaaaartin for <p>Maple can solve a system of equations such as $\sin x + y =0, \sin x - y =0$. However, </p>
<p>var('x y')
solve([sin(x) + y ==0, sin(x) - y==0], [x, y])
produces no useful answer.</p>
<p>Is there any other way to proceed?</p>
https://ask.sagemath.org/question/8557/solve-a-simple-system-of-non-linear-equations/?answer=36614#post-id-36614You can try minimizing `sqrt((sin(x)+y)**2 + (sin(x)-y)**2)` using `sage.minimize`.Tue, 14 Feb 2017 11:51:06 +0100https://ask.sagemath.org/question/8557/solve-a-simple-system-of-non-linear-equations/?answer=36614#post-id-36614Answer by jdc for <p>Maple can solve a system of equations such as $\sin x + y =0, \sin x - y =0$. However, </p>
<p>var('x y')
solve([sin(x) + y ==0, sin(x) - y==0], [x, y])
produces no useful answer.</p>
<p>Is there any other way to proceed?</p>
https://ask.sagemath.org/question/8557/solve-a-simple-system-of-non-linear-equations/?answer=13030#post-id-13030First, even though this particular system can be solved by hand easily enough, giving a nice, tidy family of solutions, I don't suspect that Sage will be able to see that. (Somebody please correct me if I'm wrong on that.) Certainly this is the case if you change your equations a little bit. In general, the best you can hope for is numerical solutions.
Resigning ourselves to that, I found a couple other questions on ASKSAGE with similar issues:
[here](http://ask.sagemath.org/question/319/solve-multidimensional-nonlinear-system-with-scipy)
and
[here](http://ask.sagemath.org/question/975/numerical-solution-of-a-system-of-non-linear).
My answer below is heavily cribbed from the one given by DSM at the first link. He recommends using scipy's 'minimize' function.
from scipy import optimize
Then, given a starting point, e.g. (5,4), we hunt for a nearby solution.
sage: f(x,y) = (sin(x) + y, sin(x) - y)
sage: minimize(norm(f), (5,4), disp = 0)
(6.28318530718, 8.79023942731e-19)
If you loop over a lattice of points, you will *hopefully* find all the solutions in that region:
sage: short = lambda (x,y): (x.n(20), y.n(20)) # To cut down on the number of digits displayed.
sage: for startloc in CartesianProduct([-6,-2 .. 6],[-6,-2 .. 6]):
....: sol = minimize(norm(f), startloc, disp = 0)
....: print startloc," ", short(sol), " ",short(f(*sol))
[-6, -6] (-6.2832, 4.8257e-19) (2.4541e-16, 2.4445e-16)
[-6, -2] (-6.2832, 2.0430e-19) (-6.4304e-16, -6.4345e-16)
[-6, 2] (-6.2832, -2.0430e-19) (-6.4345e-16, -6.4304e-16)
[-6, 6] (-6.2832, -4.8257e-19) (2.4445e-16, 2.4541e-16)
[-2, -6] (-3.1416, -4.7943e-19) (-1.2294e-16, -1.2199e-16)
[-2, -2] (-3.1416, 1.0451e-19) (-1.2236e-16, -1.2257e-16)
[-2, 2] (-3.1416, -1.0451e-19) (-1.2257e-16, -1.2236e-16)
[-2, 6] (-3.1416, 4.7943e-19) (-1.2199e-16, -1.2294e-16)
[2, -6] (3.1416, -4.7943e-19) (1.2199e-16, 1.2294e-16)
[2, -2] (3.1416, 1.0451e-19) (1.2257e-16, 1.2236e-16)
[2, 2] (3.1416, -1.0451e-19) (1.2236e-16, 1.2257e-16)
[2, 6] (3.1416, 4.7943e-19) (1.2294e-16, 1.2199e-16)
[6, -6] (6.2832, 4.8257e-19) (-2.4445e-16, -2.4541e-16)
[6, -2] (6.2832, 2.0430e-19) (6.4345e-16, 6.4304e-16)
[6, 2] (6.2832, -2.0430e-19) (6.4304e-16, 6.4345e-16)
[6, 6] (6.2832, -4.8257e-19) (-2.4541e-16, -2.4445e-16)
Fri, 16 Dec 2011 12:13:55 +0100https://ask.sagemath.org/question/8557/solve-a-simple-system-of-non-linear-equations/?answer=13030#post-id-13030