Taylor and Power Series
A Taylor series is an infinite sum of terms that are expressed in terms of the function’s derivatives at a single point.
Taylor’s Formula
The Taylor series of a real or complex-valued function that is infinitely differentiable at a real or complex number is the power series:
python
1import numpy as np
2
3def sin_taylor(x, n_terms):
4 res = 0
5 for n in range(n_terms):
6 sign = (-1)**n
7 term = (x**(2*n + 1)) / np.math.factorial(2*n + 1)
8 res += sign * term
9 return res
10
11x_val = np.pi / 4 # 45 degrees
12print(f"sin(pi/4) exact: {np.sin(x_val):.6f}")
13print(f"Taylor (1 term): {sin_taylor(x_val, 1):.6f}")
14print(f"Taylor (3 terms): {sin_taylor(x_val, 3):.6f}")
Radius of Convergence
A power series has a radius of convergence such that the series converges absolutely for and diverges for .
What is the Taylor series of exp(x) centered at 0?
Remainder Term: Taylor’s Theorem
Taylor’s Theorem gives an estimate of the error when a function is approximated by its -th degree Taylor polynomial. The Lagrange form of the remainder is: for some between and .
python
1def exp_error_estimate(x, n):
2 import math
3 # Error for exp(x) is x^(n+1)/(n+1)! * exp(c)
4 # where c is in [0, x]. Max error at c=x.
5 return (abs(x)**(n+1) / math.factorial(n+1)) * math.exp(abs(x))
6
7print(f"Max error for exp(1) with 5 terms: {exp_error_estimate(1, 5):.6f}")