philologon
philologon

Reputation: 2105

Use sympy to solve a transcendental equation

Experienced with Python. New to Sympy.

I have a transcendental equation, f(x) = sin(x) - x.

If y = f(x), I want to solve for x knowing y.

I think Sympy can do this, but I have no experience with it. Can someone explain what I should do?

(The question Transcendental Equation has answers for hand-rolling the iterative approach, which is my back-up.)

Here is what I have tried:

from sympy import *
x = symbols('x')
solve(Eq(sin(x) - x))  # Exception raised here

# NotImplementedError: multiple generators [x, sin(x)]
# No algorithms are implemented to solve equation -x + sin(x)

I recognize this does not even communicate that I have a known value for y. As you can see, I don't understand what to do at all.

This would be an iterative solution. Is there a way to get sympy to do this, or should I be using a different Python package for iterative solutions?

All help is appreciated.

Upvotes: 2

Views: 2060

Answers (1)

BPL
BPL

Reputation: 9863

What about using nsolve? ie:

>>> from sympy import *
>>> x = symbols('x')
>>> nsolve(sin(x)-x, x, 1)

It seems it uses mpmath.findroot behind the curtains.

Upvotes: 3

Related Questions