Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

SymPy is able to solve algebraic equations with one or several variables. solve(equation, variable) takes as first argument an equation that is supposed to be equaled to 0, and as second argument the unknown variable to solve for. In case of multiple equations and variables, solve returns a dictonary containing the results. Note that SymPy also handles complex numbers using the symbol I.

Code Block
languagepy
>>> from sympy import *
>>> solve(x**2 - 1, x)
[-1, 1]

>>> solve([x + 5*y - 2, -3*x + 6*y - 15], [x, y])
{x: -3, y: 1}

>>> solve(Eq(x**4, 1), x)
[-1, 1, -I, I]

nsolve(function,[variables], x0) is a useful tool to solve nonlinear equation system numerically, where x0 is a starting vector close to the solution. If there is only one variable, the second argument may be left out.

Code Block
languagepy
>>> from sympy import *
>>> x = Symbol('x')


>>> nsolve(sin(x), x, 2)
3.14159265358979

>>> nsolve(sin(x), 2)
3.14159265358979

>>> nsolve(sin(x**2)/(pi - x), x, pi.evalf()/2)
1.77245385090552