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

Analyzing real-valued functions


SymPy contains a rich calculus toolbox to analyze real-valued functions: limits, power series, derivatives, integrals, Fourier transforms, and so on. In this recipe, we will show the very basics of these capabilities.

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 and a function (which is just an expression depending on x):

    In [1]: var('x z')
    Out[1]: (x, z) 
    In [2]: f = 1/(1+x**2)
  2. Let's evaluate this function at 1:

    In [3]: f.subs(x, 1)
    Out[3]: 1/2
  3. We can compute the derivative of this function:

    In [4]: diff(f, x)
    Out[4]: -2*x/(x**2 + 1)**2
  4. What is f's limit to infinity? (Note the double o (oo) for the infinity symbol):

    In [5]: limit(f, x, oo)
    Out[5]: 0
  5. Here's how to compute a Taylor series (here, around 0, of order 9). The Big O can be removed with the removeO() method.

    In [6]: series(f, x0=0, n=9)
    Out[6]: 1 - x**2 + x**4 - x**6 + x**8 + O...