Solving trigonometric equations numerically?

44 views
Skip to first unread message

Philipp Gressly Freimann

unread,
Dec 20, 2019, 5:27:00 AM12/20/19
to sympy
Hello

I want to solve the following equation numerically between -PI and PI:

sin(x) = 0.5x + 0.2

[which is similar to sin(x) - 0.5x - 0.2 = 0]

The graph shows me three solutions. Is there a possibility to solve this equation numerically using sympy?

Thanks in advance

φ

Oscar Benjamin

unread,
Dec 20, 2019, 5:48:03 AM12/20/19
to sympy
You can use nsolve to find numerical solutions:
```
In [10]: nsolve(Eq(sin(x), 0.5*x + 0.2), x, 0)
Out[10]: 0.425436108484597
```
This will find one root at a time starting from an initial guess (I've
used zero).

Initial guesses -1 and +1 give two other roots.
```
In [11]: nsolve(Eq(sin(x), 0.5*x + 0.2), x, -1)
Out[11]: -2.11307244875263

In [12]: nsolve(Eq(sin(x), 0.5*x + 0.2), x, +1)
Out[12]: 1.59919364642736
```

You can get more precise solutions using prec:
```
In [15]: nsolve(Eq(sin(x), 0.5*x + 0.2), x, 0, prec=50)
Out[15]: 0.42543610848459725447179186114511470949330179080539
```

--
Oscar
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/abc402b1-ac1a-4508-95a8-c13a48483654%40googlegroups.com.

Philipp Gressly Freimann

unread,
Dec 21, 2019, 3:17:52 AM12/21/19
to sympy
Hello

Well, thanks a lot. Works great. I did not know the "nsolve" command.

If I am right, there is no command to find all three solutions?

φ
> To unsubscribe from this group and stop receiving emails from it, send an email to sy...@googlegroups.com.

Oscar Benjamin

unread,
Dec 21, 2019, 9:18:38 AM12/21/19
to sympy
There is no function to find all 3 solutions. It would be good to have
one. In general it can be hard even to know how many solutions there
are from a pure numerical algorithm but I think that something based
on interval-Newton would be useful:

https://en.wikipedia.org/wiki/Newton%27s_method#Interval_Newton's_method
https://en.wikipedia.org/wiki/Interval_arithmetic#Interval_Newton_method

On Sat, 21 Dec 2019 at 08:17, Philipp Gressly Freimann
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/711fb61d-5b02-4776-a93e-7cd72dec93c5%40googlegroups.com.

Chris Smith

unread,
Dec 22, 2019, 10:33:29 AM12/22/19
to sympy
Another approach for these problems is to use, as a starting point, solutions to a simpler problem which become initial guess to the more difficult problem. e.g. solving sin(x)=0.2 will give you two solutions and as many others as you want by adding or subtraction 2pi. Then these approximate solutions can be used as initial guesses for nsolve as you change the problem to `sin(x)-(a*x+0.2)` with `a` increasing as quickly as possible from 0 to 0.5 (in your case). This is the "continuation" method.

/c

Philipp Gressly Freimann

unread,
Dec 22, 2019, 1:06:11 PM12/22/19
to sympy
Thanks.
Reply all
Reply to author
Forward
0 new messages