Solving equations and inequalities
SymPy offers several ways to solve linear and nonlinear equations and systems of equations. Of course, these functions do not always succeed in finding closed-form exact solutions. In this case, we can fall back to numerical solvers and obtain approximate solutions.
How to do it...
- Let's define a few symbols:
>>> from sympy import * init_printing() >>> var('x y z a')
- We use the
solve()
function to solve equations (the right-hand side is 0 by default):>>> solve(x**2 - a, x)
- We can also solve inequalities. Here, we need to use the
solve_univariate_inequality()
function to solve this univariate inequality in the real domain:>>> x = Symbol('x') solve_univariate_inequality(x**2 > 4, x)
- The
solve()
function also accepts systems of equations (here, a linear system):>>> solve([x + 2*y + 1, x - 3*y - 2], x, y)
- Nonlinear systems are also handled:
>>> solve([x**2 + y**2 - 1, x**2 -...