Book Image

IPython Interactive Computing and Visualization Cookbook

By : Cyrille Rossant
Book Image

IPython Interactive Computing and Visualization Cookbook

By: Cyrille Rossant

Overview of this book

Table of Contents (22 chapters)
IPython Interactive Computing and Visualization Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 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)
  4. The solve() function also accepts systems of equations (here, a linear system):

    In [5...