Chapter 4
Numerical Differentiation and Integration

Per-Olof Persson
persson@berkeley.edu

Department of Mathematics
University of California, Berkeley

Math 128A Numerical Analysis

Numerical Differentiation

Forward and Backward Differences

Inspired by the definition of derivative: \[ \begin{aligned} f'(x_0) = \lim_{h\rightarrow 0} \frac{f(x_0+h)-f(x_0)}{h}, \end{aligned} \] choose a small \(h\) and approximate \[ \begin{aligned} f'(x_0) \approx \frac{f(x_0+h)-f(x_0)}{h} \end{aligned} \] The error term for the linear Lagrange polynomial gives: \[ \begin{aligned} f'(x_0) = \frac{f(x_0+h)-f(x_0)}{h} -\frac{h}{2}f''(\xi) \end{aligned} \] Also known as the forward-difference formula if \(h>0\) and the backward-difference formula if \(h<0\)

General Derivative Approximations

Differentiation of Lagrange Polynomials

Differentiate \[ \begin{aligned} f(x)=\sum_{k=0}^n f(x_k)L_k(x) + \frac{(x-x_0)\cdots (x-x_n)}{(n+1)!} f^{(n+1)}(\xi(x)) \end{aligned} \] to get \[ \begin{aligned} f'(x_j) = \sum_{k=0}^n f(x_k)L'_k(x_j)+ \frac{f^{(n+1)}(\xi(x_j))}{(n+1)!}\prod_{k\ne j} (x_j-x_k) \end{aligned} \] This is the \((n+1)\)-point formula for approximating \(f'(x_j)\).

Commonly Used Formulas

Using equally spaced points with \(h=x_{j+1}-x_j\), we have the three-point formulas \[ \begin{aligned} f'(x_0) &= \frac{1}{2h}[-3f(x_0)+4f(x_0+h)-f(x_0+2h)]+\frac{h^2}{3}f^{(3)}(\xi_0) \\ f'(x_0) &= \frac{1}{2h}[-f(x_0-h)+f(x_0+h)] - \frac{h^2}{6}f^{(3)}(\xi_1) \\ f'(x_0) &= \frac{1}{2h}[f(x_0-2h)-4f(x_0-h)+3f(x_0)] + \frac{h^2}{3}f^{(3)}(\xi_2) \\ f''(x_0) &= \frac{1}{h^2}[f(x_0-h)-2f(x_0)+f(x_0+h)] -\frac{h^2}{12} f^{(4)}(\xi) \end{aligned} \] and the five-point formula \[ \begin{aligned} f'(x_0) &= \frac{1}{12h}[f(x_0-2h)-8f(x_0-h)+8f(x_0+h)-f(x_0+2h)] \\ &+ \frac{h^4}{30} f^{(5)}(\xi) \end{aligned} \]

Optimal \(h\)

Richardson’s Extrapolation

\[ \begin{array}{cccc} \hline O(h) & O(h^2) & O(h^3) & O(h^4) \\ \hline \textbf{1:} N_1(h)\equiv N(h) \\ \textbf{2:} N_1(\frac{h}{2})\equiv N(\frac{h}{2}) & \textbf{3:} N_2(h) \\ \textbf{4:} N_1(\frac{h}{4})\equiv N(\frac{h}{4}) & \textbf{5:} N_2(\frac{h}{2}) & \textbf{6:} N_3(h) \\ \textbf{7:} N_1(\frac{h}{8})\equiv N(\frac{h}{8}) & \textbf{8:} N_2(\frac{h}{4}) & \textbf{9:} N_3(\frac{h}{2}) & \textbf{10:} N_4(h) \end{array} \]

Richardson’s Extrapolation

Numerical Quadrature

Integration of Lagrange Interpolating Polynomials

Select \(\{x_0,\ldots,x_n\}\) in \([a,b]\) and integrate the Lagrange polynomial \(P_n(x)=\sum_{i=0}^n f(x_i)L_i(x)\) and its truncation error term over \([a,b]\) to obtain \[ \begin{aligned} \int_a^b f(x)\, dx = \sum_{i=0}^n a_i f(x_i) + E(f) \end{aligned} \] with \[ \begin{aligned} a_i = \int_a^b L_i(x)\, dx \end{aligned} \] and \[ \begin{aligned} E(f) = \frac{1}{(n+1)!} \int_a^b \prod_{i=0}^n (x-x_i) f^{(n+1)} (\xi(x))\,dx \end{aligned} \]

Trapezoidal and Simpson’s Rules

The Trapezoidal Rule

Linear Lagrange polynomial with \(x_0=a\), \(x_1=b\), \(h=b-a\), gives \[ \begin{aligned} \int_a^b f(x)\,dx = \frac{h}{2}[f(x_0)+f(x_1)]-\frac{h^3}{12} f''(\xi) \end{aligned} \]

Simpson’s Rule

Second Lagrange polynomial with \(x_0=a\), \(x_2=b\), \(x_1=a+h\), \(h=(b-a)/2\) gives \[ \begin{aligned} \int_{x_0}^{x_2} f(x)\, dx = \frac{h}{3}[f(x_0)+4f(x_1)+f(x_2)]- \frac{h^5}{90}f^{(4)}(\xi) \end{aligned} \]

Definition

The degree of accuracy, or precision, of a quadrature formula is the largest positive integer \(n\) such that the formula is exact for \(x^k\), for each \(k=0,1,\ldots, n\).

The Newton-Cotes Formulas

The Closed Newton-Cotes Formulas

Use nodes \(x_i=x_0+ih\), \(x_0=a\), \(x_n=b\), \(h=(b-a)/n\): \[ \begin{aligned} \int_a^b f(x)\,dx &\approx \sum_{i=0}^n a_i f(x_i) \\ a_i=\int_{x_0}^{x_n} L_i(x)\,dx &= \int_{x_0}^{x_n} \prod_{j\ne i} \frac{(x-x_j)}{(x_i-x_j)}\,dx \end{aligned} \] \(n=1\) gives the Trapezoidal rule, \(n=2\) gives Simpson’s rule.

The Open Newton-Cotes Formulas

Use nodes \(x_i=x_0+ih\), \(x_0=a+h\), \(x_n=b-h\), \(h=(b-a)/(n+2)\). Setting \(n=0\) gives the Midpoint rule: \[ \begin{aligned} \int_{x_{-1}}^{x_1} f(x)\,dx = 2hf(x_0)+\frac{h^3}{3} f''(\xi) \end{aligned} \]

Composite Rules

Theorem

Let \(f\in C^2[a,b]\), \(h=(b-a)/n\), \(x_j=a+jh\), \(\mu\in(a,b)\). The Composite Trapezoidal rule for \(n\) subintervals is \[ \begin{aligned} \int_a^b f(x)\,dx &= \frac{h}{2} \left[ f(a)+2\sum_{j=1}^{n-1} f(x_j) + f(b) \right] -\frac{b-a}{12} h^2 f''(\mu) \end{aligned} \]

Theorem

Let \(f\in C^4[a,b]\), \(n\) even, \(h=(b-a)/n\), \(x_j=a+jh\), \(\mu\in(a,b)\). The Composite Simpson’s rule for \(n\) subintervals is \[ \begin{aligned} \int_a^b f(x)\,dx &= \frac{h}{3} \left[ f(a)+2\sum_{j=1}^{(n/2)-1} f(x_{2j}) + 4\sum_{j=1}^{n/2} f(x_{2j-1}) + f(b) \right] \\ &-\frac{b-a}{180} h^4 f^{(4)}(\mu) \end{aligned} \]

Romberg Integration

Romberg Integration

Romberg Integration

MATLAB Implementation

function R = romberg(f, a, b, n)
% Compute integral of f(x) from a to b using Romberg integration.

h = b-a;
R = zeros(n,n);
R(1,1) = h/2 * (f(a) + f(b));
for i = 2:n
    R(i,1) = 1/2 * (R(i-1,1) + h*sum(f(a + ((1:2^(i-2))-0.5)*h)));
    for j = 2:i
        R(i,j) = R(i,j-1) + (R(i,j-1)-R(i-1,j-1)) / (4^(j-1)-1);
    end
    h = h/2;
end

Error Estimation

Adaptive Quadrature

Gaussian Quadrature

Legendre Polynomials

Gaussian Quadrature

Theorem

Suppose \(x_1,\ldots,x_n\) are roots of \(P_n(x)\) and \[ \begin{aligned} c_i = \int_{-1}^1 \prod_{j\ne i}^n \frac{x-x_j}{x_i-x_j}\,dx \end{aligned} \] If \(P(x)\) is any polynomial of degree less than \(2n\), then \[ \begin{aligned} \int_{-1}^1 P(x)\,dx = \sum_{i=1}^n c_i P(x_i) \end{aligned} \]

Computing Gaussian Quadrature Coefficients

MATLAB Implementation

function [x, c] = gaussquad(n)
% Compute Gaussian quadrature points and coefficients.

P = zeros(n+1,n+1);
P([1,2],1) = 1;
for k = 1:n-1
    P(k+2,1:k+2) = ((2*k+1)*[P(k+1,1:k+1) 0] - ...
                    k*[0 0 P(k,1:k)]) / (k+1);
end
x = sort(roots(P(n+1,1:n+1)));

A = zeros(n,n);
for i = 1:n
    A(i,:) = polyval(P(i,1:i),x)';
end
c = A \ [2; zeros(n-1,1)];

Arbitrary Intervals

Transform integrals \(\int_a^b f(x)\,dx\) into integrals over \([-1,1]\) by a change of variables: \[ \begin{aligned} t=\frac{2x-a-b}{b-a} \Leftrightarrow x=\frac{1}{2}[(b-a)t+a+b] \end{aligned} \] Gaussian quadrature then gives \[ \begin{aligned} \int_a^b f(x)\,dx = \int_{-1}^1 f\left(\frac{(b-a)t+(b+a)}{2}\right) \frac{(b-a)}{2}\,dt \end{aligned} \]

Double Integrals

Composite Simpson’s Rule Double Integration

The Composite Simpson’s rule gives \[ \begin{aligned} \int_a^b \left(\int_c^d f(x,y)\,dy\right)\,dx = \frac{hk}{9} \sum_{i=0}^n \sum_{j=0}^m w_{i,j} f(x_i,y_j) + E \end{aligned} \] where \(x_i=a+ih\), \(y_j=c+jk\), \(w_{i,j}\) are the products of the nested Composite Simpson’s rule coefficients (see below), and the error is \[ \begin{aligned} E=-\frac{(d-c)(b-a)}{180}\left[h^4\frac{\partial^4 f}{\partial x^4} (\bar{\eta},\bar{\mu}) + k^4\frac{\partial^4 f}{\partial y^4} (\hat{\eta},\hat{\mu})\right] \end{aligned} \]

\[ \begin{array}{rlllll} d & \bullet 1\ & \bullet 4 & \bullet 2 & \bullet 4 & \bullet 1 \\ \ \\ & \bullet 4 & \bullet 16 & \bullet 8 & \bullet 16 & \bullet 4 \\ \ \\ c & \bullet 1\ & \bullet 4 & \bullet 2 & \bullet 4 & \bullet 1 \\ & a & & & & b \end{array} \]

Non-Rectangular Regions

The same technique can be applied to double integrals of the form \[ \begin{aligned} \int_a^b \int_{c(x)}^{d(x)} f(x,y)\,dy\,dx \end{aligned} \] The step size for \(x\) is still \(h=(b-a)/n\), but for \(y\) it varies with \(x\): \[ \begin{aligned} k(x) = \frac{d(x)-c(x)}{m} \end{aligned} \]

Gaussian Double Integration

Improper Integrals with a Singularity

The improper integral below, with a singularity at the left endpoint, converges if and only if \(0<p<1\) and then \[ \begin{aligned} \int_a^b \frac{1}{(x-a)^p}dx = \left.\frac{(x-a)^{1-p}}{1-p}\right|_a^b =\frac{(b-a)^{1-p}}{1-p} \end{aligned} \] More generally, if \[ \begin{aligned} f(x)=\frac{g(x)}{(x-a)^p},\quad 0<p<1,\quad g\text{ continuous on }[a,b], \end{aligned} \] construct the fourth Taylor polynomial \(P_4(x)\) for \(g\) about \(a\): \[ \begin{aligned} P_4(x)&=g(a)+g'(a)(x-a)+\frac{g''(a)}{2!}(x-a)^2 \\ &+\frac{g'''(a)}{3!}(x-a)^3 + \frac{g^{(4)}(a)}{4!}(x-a)^4 \end{aligned} \]

Improper Integrals with a Singularity

and write \[ \begin{aligned} \int_a^b f(x)\,dx = \int_a^b \frac{g(x)-P_4(x)}{(x-a)^p}dx +\int_a^b \frac{P_4(x)}{(x-a)^p}dx \end{aligned} \] The second integral can be computed exactly: \[ \begin{aligned} \int_a^b \frac{P_4(x)}{(x-a)^p}dx &= \sum_{k=0}^4 \int_a^b \frac{g^{(k)}(a)}{k!}(x-a)^{k-p}\,dx \\ &= \sum_{k=0}^4 \frac{g^{(k)}(a)}{k!(k+1-p)}(b-a)^{k+1-p} \end{aligned} \]

Improper Integrals with a Singularity

For the first integral, use the Composite Simpson’s rule to compute the integral of \(G\) on \([a,b]\), where \[ \begin{aligned} G(x)= \begin{cases} \frac{g(x)-P_4(x)}{(x-a)^p}, & \text{if }a<x\le b \\ 0, & \text{if }x=a \end{cases} \end{aligned} \] Note that \(0<p<1\) and \(P_4^{(k)}(a)\) agrees with \(g^{(k)}(a)\) for each \(k=0,1,2,3,4\), so \(G\in C^4[a,b]\) and Simpson’s rule can be applied.

Singularity at the Right Endpoint

Infinite Limits of Integration

An integral of the form \(\int_a^\infty \frac{1}{x^p}dx\), with \(p>1\), can be converted to an integral with left endpoint singularity at \(0\) by the substitution \[ \begin{aligned} t=x^{-1},\quad dt=-x^{-2}\,dx, \text{ so }dx=-x^2 dt=-t^{-2}dt \end{aligned} \] which gives \[ \begin{aligned} \int_a^\infty \frac{1}{x^p} dx = \int_{1/a}^0 -\frac{t^p}{t^2}dt=\int_0^{1/a}\frac{1}{t^{2-p}}dt \end{aligned} \] More generally, this variable change converts \(\int_a^\infty f(x)\,dx\) into \[ \begin{aligned} \int_a^\infty f(x)\,dx = \int_0^{1/a} t^{-2} f\left(\frac{1}{t}\right)\,dt \end{aligned} \]