Lesson 8 of 15

Taylor Series

Taylor Series

The Taylor series of a function (f) centered at (a) is:

f(x)=n=0f(n)(a)n!(xa)nf(x) = \sum_{n=0}^{\infty} \frac{f^{(n)}(a)}{n!} (x - a)^n

When (a = 0), this is called a Maclaurin series.

Key Taylor Series

FunctionMaclaurin series
(e^x)(\sum_{n=0}^{\infty} \frac{x^n}{n!})
(\sin(x))(\sum_{n=0}^{\infty} \frac{(-1)^n x^{2n+1}}{(2n+1)!})
(\cos(x))(\sum_{n=0}^{\infty} \frac{(-1)^n x^{2n}}{(2n)!})

Approximation Error

The Taylor polynomial of degree (N) provides an approximation. The error decreases as (N) increases (within the radius of convergence).

Numerical Taylor Approximation

We can compute Taylor polynomials term by term:

import math

def taylor_exp(x, N):
    return sum(x**n / math.factorial(n) for n in range(N + 1))

Your Task

Implement:

  1. taylor_sin(x, N) -- compute the degree-(N) Taylor approximation of (\sin(x)) using terms up to index (N): (\sum_{n=0}^{N} \frac{(-1)^n x^{2n+1}}{(2n+1)!})
  2. taylor_cos(x, N) -- compute (\sum_{n=0}^{N} \frac{(-1)^n x^{2n}}{(2n)!})
  3. taylor_exp(x, N) -- compute (\sum_{n=0}^{N} \frac{x^n}{n!})
Pyodide loading...
Loading...
Click "Run" to execute your code.