In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Suppose we have the function $$ \frac{1}{1-x} $$

Let's

  1. define the function
  2. plot the function on [-1,1)
In [2]:
def f(x):
    return 1.0 / (1.0 - x)
In [13]:
xx = np.linspace(-1,1,40, endpoint=False)
plt.plot(xx, f(xx), '-', lw=3)
plt.axis([-1,1,0,20])
Out[13]:
[-1, 1, 0, 20]

What happens here? The function is singular at $x=1$.

In [14]:
def taylor(x, k):
    """
    Return the Taylor expasion (about 0) with k terms
    """
    ret = 0
    for i in range(k):
        ret += x**i
    return ret

Now let's plot some expansions:

In [21]:
xx = np.linspace(-1,1,40, endpoint=False)
plt.plot(xx, f(xx), '-', lw=3)

plt.plot(xx, taylor(xx, 1), '-', lw=3, label='1 term')
plt.plot(xx, taylor(xx, 2), '-', lw=3, label='2 term')
plt.plot(xx, taylor(xx, 3), '-', lw=3, label='2 term')



plt.axis([-1,1,0,20])
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend(frameon=False)
Out[21]:
<matplotlib.legend.Legend at 0x109836ef0>

What happens to the approximation of $f(x)$ with a Taylor series about $x=0$ when evaluated at $x=1$?

When evaluated at $x=-1$?

In [ ]: