Master Taylor and Maclaurin series, convergence, and their use in function approximation.
π Seriesπ Lesson 29 of 100π Free Course
Advertisement
Why It Matters
βΉοΈ Why It Matters
Taylor series are one of the most powerful tools in mathematics, providing a bridge between complicated functions and simple polynomials. They allow us to:
Linearize nonlinear functions locally (the foundation of Newton's method and gradient descent analysis)
Approximate transcendental functions like ex, sinx, and lnx using only arithmetic operations
Analyze function behavior near a point using derivative information alone
Derive fundamental results in physics, engineering, and machine learning
Every time you use np.exp(), np.sin(), or np.log() in code, the underlying computation relies on polynomial approximations rooted in Taylor series. Understanding them gives you insight into numerical computing, optimization, and the behavior of neural networks near initialization.
What is a Taylor Series
DfTaylor Series
A Taylor series represents a function f(x) as an infinite sum of terms calculated from the values of its derivatives at a single point a. If f is infinitely differentiable at a, the Taylor series of f about a is:
f(n)(a)=The nth derivative of f evaluated at the center point a
a=The center (expansion point) of the series
n!=Factorial of n (1Β·2Β·3Β·...Β·n)
(xβa)n=The nth power of the distance from a
βΉοΈ Intuition
Think of a Taylor series as building a polynomial approximation piece by piece:
The 0th termf(a) matches the function value at a
The 1st termfβ²(a)(xβa) matches the slope at a
The 2nd term2!fβ²β²(a)β(xβa)2 matches the curvature at a
Each higher-order term captures finer details of the function's shape
The more terms you include, the better the approximation becomes β and for many functions, the infinite sum converges to the exact function value.
Maclaurin Series
A Maclaurin series is simply a Taylor series centered at a=0. It is the most commonly used form because derivatives at zero are often easier to compute.
Mn+1β=Maximum of $|f^{(n+1)}|$ on the interval between a and x
n=Degree of the polynomial
Python Implementation
Basic Taylor Approximation
import numpy as np
def taylor_exp(x, n_terms=10):
"""Approximate e^x using the Maclaurin series."""
result = 0
for k in range(n_terms):
result += x**k / np.math.factorial(k)
return result
# Compare with numpy
x = 1.0
print(f"Exact: {np.exp(x):.12f}")
print(f"Taylor: {taylor_exp(x, n_terms=10):.12f}")
# Output:
# Exact: 2.718281828459
# Taylor: 2.718281801146
Vectorized Taylor Approximation
import numpy as np
def taylor_sin(x, n_terms=8):
"""Vectorized Maclaurin series for sin(x)."""
result = np.zeros_like(x, dtype=float)
for k in range(n_terms):
sign = (-1)**k
power = 2*k + 1
result += sign * x**power / np.math.factorial(power)
return result
x = np.array([0.1, 0.5, 1.0, 2.0])
print(f"Exact: {np.sin(x)}")
print(f"Taylor: {taylor_sin(x)}")
print(f"Error: {np.abs(np.sin(x) - taylor_sin(x))}")
SymPy Symbolic Taylor Series
from sympy import symbols, series, sin, cos, exp, log, oo
x = symbols('x')
# Taylor series expansions
print("e^x:", series(exp(x), x, 0, n=6))
print("sin(x):", series(sin(x), x, 0, n=8))
print("cos(x):", series(cos(x), x, 0, n=8))
print("ln(1+x):", series(log(1+x), x, 0, n=6))
# Compute specific derivatives
from sympy import factorial
def taylor_coeff(f, x, a, n):
"""Get the nth Taylor coefficient at point a."""
return f.diff(x, n).subs(x, a) / factorial(n)
print(f" coefficient of x^3 in e^x: {taylor_coeff(exp(x), x, 0, 3)}")
Error Analysis
import numpy as np
import matplotlib.pyplot as plt
def taylor_error_analysis():
"""Compare Taylor approximations of different orders."""
x = np.linspace(-3, 3, 200)
exact = np.exp(x)
errors = {}
for n in [3, 5, 7, 10]:
approx = np.zeros_like(x)
for k in range(n + 1):
approx += x**k / np.math.factorial(k)
errors[n] = np.abs(exact - approx)
# Plot
plt.figure(figsize=(10, 6))
for n, err in errors.items():
plt.semilogy(x, err, label=f'n={n}')
plt.xlabel('x')
plt.ylabel('Absolute Error')
plt.title('Taylor Series Error for e^x')
plt.legend()
plt.grid(True)
plt.show()
taylor_error_analysis()
Applications in AI/ML
Linearization of Neural Networks
βΉοΈ Neural Tangent Kernel (NTK)
The Neural Tangent Kernel describes how neural networks behave near initialization. For a network f(x;ΞΈ) with parameters ΞΈ:
In the infinite-width limit, this linearization becomes exact during training, and the network behaves like a linear model in function space.
Gradient Descent Analysis
Taylor series explain why gradient descent works:
The gradientβf is the first-order Taylor approximation
Newton's method uses the second-order Taylor expansion for faster convergence
Momentum methods can be viewed as modifying the Taylor approximation landscape
Taylor Series in Loss Functions
Many loss functions are analyzed using Taylor expansions:
Cross-entropy loss near zero predictions (gradient explosion)
Huber loss as a smooth approximation of MAE
Softmax stability analysis (subtracting the max is equivalent to scaling the Taylor series)
Automatic Differentiation Connection
Automatic differentiation (AD) computes exact derivatives β the same derivatives needed for Taylor series. Modern ML frameworks like PyTorch and JAX use AD to:
Compute gradients for backpropagation
Enable higher-order derivatives for Taylor-based optimization
Implement the Taylor-mode AD for efficient computation of Taylor coefficients
Common Mistakes
Mistake
Why It's Wrong
Correct Approach
Using Taylor series outside the radius of convergence
The series diverges and gives meaningless results
Always check β£xβaβ£<R before using the series
Forgetting the factorial in the denominator
Coefficients are f(n)(a)/n!, not just f(n)(a)
The n! comes from repeated differentiation of xn
Assuming all functions have convergent Taylor series
eβ1/x2 (extended by 0 at origin) has all derivatives = 0 at 0
Check if Rnβ(x)β0 as nββ
Using the wrong center point
Taylor series about a=1 cannot approximate near x=0 well
Choose a close to the x values of interest
Mixing up sin and cos series
sin has odd powers starting with x; cos has even powers starting with 1
Forgetting (β1)n in sin, cos, ln(1+x) leads to completely wrong values
Alternating series have special convergence properties; pay attention to signs
Not checking error bounds
Using an approximation without knowing its accuracy
Always bound β£Rnβ(x)β£ using the Lagrange remainder
Interview Questions
Question 1: Why does ex have the simplest Maclaurin series?
Answer: Because all derivatives of ex are ex itself, and e0=1. So f(n)(0)=1 for all n, giving the clean series βn!xnβ. No alternating signs, no zeros β every term contributes equally.
Question 2: How would you approximate 1.1β without a calculator?
Answer: Use the binomial series (1+x)Ξ± with x=0.1 and Ξ±=1/2:
The exact value is β1.0488088..., so 3 terms give 5 digits of accuracy.
Question 3: Why is the Taylor series for ln(1+x) restricted to β£xβ£<1?
Answer: The coefficients cnβ=n(β1)n+1β satisfy limnββββ£cnβ/cn+1ββ£=1, giving R=1. At x=1, the series 1β1/2+1/3ββ― converges (alternating harmonic series), but at x=β1 it becomes β1β1/2β1/3ββ―, which diverges (harmonic series).
Question 4: What's the relationship between Taylor series and Fourier series?
Answer: Both decompose functions into simpler basis functions:
Taylor series use polynomial basis {1,x,x2,β¦} β best for local approximation
Fourier series use trigonometric basis {1,cos(nx),sin(nx)} β best for periodic functions
Taylor series require differentiability; Fourier series work for discontinuous functions. In higher dimensions, Taylor series extend to multivariate polynomials, while Fourier series extend to the Fourier transform.
Question 5: How do Taylor series relate to the concept of analyticity?
Answer: A function is analytic at a point if its Taylor series converges to the function in some neighborhood. All polynomial, exponential, trigonometric, and rational functions (away from poles) are analytic. However, f(x)=eβ1/x2 (with f(0)=0) is smooth but not analytic β its Taylor series at 0 is identically zero, yet the function is nonzero for xξ =0.
Question 6: Can you use Taylor series to solve differential equations?
Answer: Yes β the power series method assumes y(x)=βn=0ββanβxn, substitutes into the ODE, and solves for the coefficients recursively. For example, solving yβ²=y with y(0)=1 gives anβ=1/n!, recovering ex. This method works for linear ODEs with polynomial coefficients and is the basis for special functions like Bessel functions and Legendre polynomials.
Practice Problems
Problem 1: Derive the Series
πProblem: Derive the Maclaurin Series
Find the Maclaurin series for f(x)=1+x21β and determine its radius of convergence.
π‘Solution
Method 1 (Substitution):
Since 1βu1β=βn=0ββun for β£uβ£<1, substitute u=βx2:
Exact: e2β7.3891, so 4 terms give about 2% error. Adding the (xβ1)4 term (e/24) reduces this dramatically.
Problem 4: Real-World Application
πProblem: Small Angle Approximation
A pendulum has length L=1 m. For small angles, the period is T=2ΟL/gβ. Use Taylor series to find a correction term for the period when the amplitude is ΞΈ0β=30Β°.
π‘Solution
The exact period involves an elliptic integral, but we can use the Taylor expansion of sinΞΈβΞΈβΞΈ3/6+β―.
So the period is about 1.7% longer than the small-angle prediction. For ΞΈ0β=45Β°, the correction is about 3.8%, showing the approximation breaks down for large angles.
Problem 5: Series Manipulation
πProblem: Deriving New Series from Known Ones
Use known Maclaurin series to find the series for f(x)=2exβeβxβ (which is sinhx).