Mehmet Emre Tiryaki
Back to Notes

Numerical Differentiation and Integration

Finite Differences

Nodes a=t0<⋯<tn=ba = t_0 < \cdots < t_n = b, ti=a+iht_i = a + ih, h=b−anh = \frac{b - a}{n}. Approximate

f′(x)=lim⁡h→0f(x+h)−f(x)h,f′(ti)≈1h∑k=−pqakf(ti+kh)f'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}, \qquad f'(t_i) \approx \frac{1}{h}\sum_{k=-p}^{q} a_k f(t_i + kh)

Shifting so the point is 0 (s=x−tis = x - t_i, f~(s)=f(s+ti)\tilde{f}(s) = f(s + t_i), dfdx∣x=ti=df~ds∣s=0\frac{df}{dx}\big|_{x = t_i} = \frac{d\tilde{f}}{ds}\big|_{s = 0}), it is enough to approximate f′(0)f'(0).

  • Forward f′(x)≈f(x+h)−f(x)hf'(x) \approx \frac{f(x + h) - f(x)}{h}, p=0p = 0, q=1q = 1, a0=−1a_0 = -1, a1=1a_1 = 1.
  • Backward f′(x)≈f(x)−f(x−h)hf'(x) \approx \frac{f(x) - f(x - h)}{h}, q=0q = 0.
  • Centered f′(x)≈f(x+h)−f(x−h)2hf'(x) \approx \frac{f(x + h) - f(x - h)}{2h}, p=q=1p = q = 1, a−1=−12a_{-1} = -\frac{1}{2}, a0=0a_0 = 0, a1=12a_1 = \frac{1}{2}.

The centered formula is the derivative at 0 of the quadratic interpolant through −h,0,h-h, 0, h,

Q(x)=x(x−h)2h2f(−h)−x2−h2h2f(0)+x(x+h)2h2f(h),Q′(0)=f(h)−f(−h)2hQ(x) = \frac{x(x - h)}{2h^2}f(-h) - \frac{x^2 - h^2}{h^2}f(0) + \frac{x(x + h)}{2h^2}f(h), \qquad Q'(0) = \frac{f(h) - f(-h)}{2h}

For the second derivative,

f′′(0)≈f(−h)−2f(0)+f(h)h2f''(0) \approx \frac{f(-h) - 2f(0) + f(h)}{h^2}

Truncation error τf(h)=f′(0)−1h∑k=−pqakf(kh)\tau_f(h) = f'(0) - \frac{1}{h}\sum_{k=-p}^q a_k f(kh). Forward and backward are O(h)O(h), centered is O(h2)O(h^2):

τf(h)=f′(0)−12h[(f(0)+hf′(0)+12h2f′′(0)+16h3f′′′(0)+O(h4))−(f(0)−hf′(0)+12h2f′′(0)−16h3f′′′(0)+O(h4))]=−h26f′′′(0)+O(h3)\tau_f(h) = f'(0) - \frac{1}{2h}\Big[ \big(f(0) + hf'(0) + \tfrac{1}{2}h^2 f''(0) + \tfrac{1}{6}h^3 f'''(0) + O(h^4)\big) - \big(f(0) - hf'(0) + \tfrac{1}{2}h^2 f''(0) - \tfrac{1}{6}h^3 f'''(0) + O(h^4)\big) \Big] = -\frac{h^2}{6}f'''(0) + O(h^3)

Coefficient tables Centered, coefficients of f(−4h),…,f(−h)f(-4h), \ldots, f(-h) (a0=0a_0 = 0 and ak=−a−ka_k = -a_{-k} for the positive side), and forward, coefficients of f(0),…,f(4h)f(0), \ldots, f(4h),

order−4h−3h−2h−h2−124112−236−160320−3481280−410515−45order0h2h3h4h1−112−322−123−1163−32134−25124−343−14\begin{array}{c|cccc} \text{order} & -4h & -3h & -2h & -h \\ \hline 2 & & & & -\frac{1}{2} \\ 4 & & & \frac{1}{12} & -\frac{2}{3} \\ 6 & & -\frac{1}{60} & \frac{3}{20} & -\frac{3}{4} \\ 8 & \frac{1}{280} & -\frac{4}{105} & \frac{1}{5} & -\frac{4}{5} \end{array} \qquad \qquad \begin{array}{c|ccccc} \text{order} & 0 & h & 2h & 3h & 4h \\ \hline 1 & -1 & 1 & & & \\ 2 & -\frac{3}{2} & 2 & -\frac{1}{2} & & \\ 3 & -\frac{11}{6} & 3 & -\frac{3}{2} & \frac{1}{3} & \\ 4 & -\frac{25}{12} & 4 & -3 & \frac{4}{3} & -\frac{1}{4} \end{array}

Backward formulas come from f^(x)=f(−x)\hat{f}(x) = f(-x), which flips the nodes and the sign. E.g. 4th order centered and 2nd order backward,

f′(0)≈1h[112f(−2h)−23f(−h)+23f(h)−112f(2h)],f′(0)≈1h[12f(−2h)−2f(−h)+32f(0)]f'(0) \approx \frac{1}{h}\Big[ \tfrac{1}{12}f(-2h) - \tfrac{2}{3}f(-h) + \tfrac{2}{3}f(h) - \tfrac{1}{12}f(2h) \Big], \qquad f'(0) \approx \frac{1}{h}\Big[ \tfrac{1}{2}f(-2h) - 2f(-h) + \tfrac{3}{2}f(0) \Big]

Numerical Integration

I=∫abf(x) dx≈Q=h∑i=0nwif(ti)=h[w0f(t0)+w1f(t1)+⋯+wnf(tn)]I = \int_a^b f(x)\, dx \approx Q = h\sum_{i=0}^n w_i f(t_i) = h[w_0 f(t_0) + w_1 f(t_1) + \cdots + w_n f(t_n)]

with weights wiw_i independent of ff. Get an interpolant and integrate it; with piecewise polynomial interpolants these are the Newton-Cotes rules. With the piecewise linear interpolant ∑if(ti)Hi(x)\sum_i f(t_i)H_i(x),

I≈∫ab∑i=0nf(ti)Hi(x) dx=∑i=0nf(ti)∫abHi(x) dx,wi=h−1∫abHi(x) dx={1i=1,…,n−112i=0,nI \approx \int_a^b \sum_{i=0}^n f(t_i)H_i(x)\, dx = \sum_{i=0}^n f(t_i) \int_a^b H_i(x)\, dx, \qquad w_i = h^{-1}\int_a^b H_i(x)\, dx = \begin{cases} 1 & i = 1, \ldots, n - 1 \\ \frac{1}{2} & i = 0, n \end{cases}

which is the trapezoid rule

Tf(n)=h[12f(t0)+f(t1)+f(t2)+⋯+f(tn−1)+12f(tn)]T_f(n) = h\Big[ \tfrac{1}{2}f(t_0) + f(t_1) + f(t_2) + \cdots + f(t_{n-1}) + \tfrac{1}{2}f(t_n) \Big]

Its error is bounded by the interpolant's,

∣I−Tf(n)∣=∣∫ab[f(x)−p(x)] dx∣≤(b−a)max⁡x∈[a,b]∣f(x)−p(x)∣=O(h2)|I - T_f(n)| = \Big| \int_a^b [f(x) - p(x)]\, dx \Big| \le (b - a)\max_{x \in [a, b]} |f(x) - p(x)| = O(h^2)

Euler-Maclaurin

I=Tf(n)−h212[f′(b)−f′(a)]+h4720[f′′′(b)−f′′′(a)]+O(h6)=Tf(n)−∑k=1∞B2kh2k(2k)![f(2k−1)(b)−f(2k−1)(a)]I = T_f(n) - \frac{h^2}{12}[f'(b) - f'(a)] + \frac{h^4}{720}[f'''(b) - f'''(a)] + O(h^6) = T_f(n) - \sum_{k=1}^\infty \frac{B_{2k}h^{2k}}{(2k)!}\big[f^{(2k-1)}(b) - f^{(2k-1)}(a)\big]

with B2kB_{2k} the Bernoulli numbers. Unless f′(b)=f′(a)f'(b) = f'(a), the error is 2nd order.

Extrapolation

If A0=A(h)+c1h+c2h2+c3h3+⋯A_0 = A(h) + c_1 h + c_2 h^2 + c_3 h^3 + \cdots, combine A(h)A(h) at different hh to cancel the leading error terms. For the trapezoid rule only even powers appear, so in terms of nn

I=Tf(n)+c2n−2+c4n−4+⋯=Tf(2n)+14c2n−2+116c4n−4+⋯I = T_f(n) + c_2 n^{-2} + c_4 n^{-4} + \cdots = T_f(2n) + \tfrac{1}{4}c_2 n^{-2} + \tfrac{1}{16}c_4 n^{-4} + \cdots

and eliminating the n−2n^{-2} term,

Sf(2n)=13[4Tf(2n)−Tf(n)],I=Sf(2n)+O(n−4)=Sf(2n)+b4n−4+b6n−6+⋯S_f(2n) = \frac{1}{3}[4T_f(2n) - T_f(n)], \qquad I = S_f(2n) + O(n^{-4}) = S_f(2n) + b_4 n^{-4} + b_6 n^{-6} + \cdots

which is Simpson's rule, 4th order. Repeating with I=Sf(4n)+116b4n−4+164b6n−6+⋯I = S_f(4n) + \frac{1}{16}b_4 n^{-4} + \frac{1}{64}b_6 n^{-6} + \cdots,

Rf(4n)=115[16Sf(4n)−Sf(2n)]R_f(4n) = \frac{1}{15}[16S_f(4n) - S_f(2n)]

is 6th order.