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...

  1. Let's define a few symbols:
    In [2]: var('x y z a')
    Out[2]: (x, y, z, a)
  2. We use the solve() function to solve equations (the right-hand side is 0 by default):
    In [3]: solve(x**2 - a, x)
    Out[3]: [-sqrt(a), sqrt(a)]
  3. We can also solve inequalities. Here, we need to use the solve_univariate_inequality() function ...

Get IPython Interactive Computing and Visualization Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.