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 [3]:
xx = np.linspace(-1,1,40, endpoint=False)
plt.plot(xx, f(xx), '-', lw=3)
plt.axis([-1,1,0,20])

Out[3]:
[-1, 1, 0, 20]

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

In [4]:
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 [5]:
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='3 term')

plt.axis([-1,1,0,20])
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend(frameon=False)

Out[5]:
<matplotlib.legend.Legend at 0xa69f908>

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 [ ]: