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.
Getting ready
We first need to import SymPy. We also initialize pretty printing in the notebook (see the first recipe of this chapter).
How to do it...
- Let's define a few symbols:
In [2]: var('x y z a') Out[2]: (x, y, z, a)
- We use the
solve()
function to solve equations (the right-hand side is0
by default):In [3]: solve(x**2 - a, x) Out[3]: [-sqrt(a), sqrt(a)]
- We can also solve inequalities. Here, we need to use the
solve_univariate_inequality()
function to solve this univariate inequality in the real domain:In [4]: x = Symbol('x') solve_univariate_inequality(x**2 > 4, x) Out[4]: Or(x < -2, x > 2)
- The
solve()
function also accepts systems of equations (here...