import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Suppose we have the function $$ \frac{1}{1-x} $$
Let's
def f(x):
return 1.0 / (1.0 - x)
xx = np.linspace(-1,1,40, endpoint=False)
plt.plot(xx, f(xx), '-', lw=3)
plt.axis([-1,1,0,20])
What happens here? The function is singular at $x=1$.
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:
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)
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$?