Ask Your Question
4

Solve a simple system of non-linear equations

asked 2011-12-15 09:30:18 +0200

jllb gravatar image

updated 2023-05-19 22:03:13 +0200

FrédéricC gravatar image

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?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
3

answered 2011-12-16 12:13:55 +0200

jdc gravatar image

First, 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 and here. 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)
edit flag offensive delete link more
0

answered 2017-02-14 11:51:06 +0200

maaaaaaartin gravatar image

You can try minimizing sqrt((sin(x)+y)**2 + (sin(x)-y)**2) using sage.minimize.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2011-12-15 09:30:18 +0200

Seen: 4,436 times

Last updated: Feb 14 '17