Lesson 10 of 18

Taylor Polynomial

Taylor Polynomial

The Taylor polynomial of degree nn approximates f(x)f(x) near point aa:

P_n(x) = sum_{k=0}^n rac{f^{(k)}(a)}{k!} cdot (x-a)^k

Where f(k)(a)f^{(k)}(a) is the kk-th derivative of ff at aa.

Building Intuition

  • P0(x)=f(a)P_0(x) = f(a) — constant (matches value at aa)
  • P1(x)=f(a)+f(a)(xa)P_1(x) = f(a) + f'(a)(x-a) — linear (also matches slope)
  • P2(x)P_2(x) — also matches curvature
  • Each higher term matches one more derivative at aa

Exact for Polynomials

A degree-nn Taylor polynomial reproduces any degree-nn polynomial exactly:

f(x)=x2f(x) = x^2 at a=0a=0:

P_2(x) = f(0) + f'(0)x + rac{f''(0)}{2!} x^2 = 0 + 0 + rac{2}{2} x^2 = x^2 checkmark

Numerical nth Derivative

Use recursive central differences:

f^(0)(x) = f(x)
f^(n)(x) ≈ (f^(n-1)(x+h) - f^(n-1)(x-h)) / (2h)

This is accurate to O(h2)O(h^2) at each level. Keep nleq4n leq 4 and happrox103h approx 10^{-3} for best results.

Your Task

Implement double taylor_poly(double (*f)(double), double a, double x, int n, double h).

Use recursive nth_deriv and a loop-based factorial. Helper stubs are provided.

TCC compiler loading...
Loading...
Click "Run" to execute your code.