# Numerical Differentiation and Integration

# Numerical Differentiation

# Finite difference formulas

# Two-point forward-difference formula

f′(x)=f(x+h)−f(x)h−h2f′′(c) f^{\prime}(x) = \frac{f(x+h) - f(x)}{h} - \frac{h}{2}f^{\prime\prime}(c)

treating the last term h2f′′(c)\frac{h}{2}f^{\prime\prime}(c) as error, where c is between xx and x+hx+h

if error is O(hn)O(h^n) ,we call the formula an order nn approximation


A second-order formula can be developed if ff is three times continuously differentiable

according to Taylor's Theorem

f(x+h)=f(x)+hf′(x)+h22f′′(x)+h36f′′′(c1)f(x−h)=f(x)−hf′(x)+h22f′′(x)−h36f′′′(c1)f′(x)=f(x+h)−f(x−h)2h−h212f′′′(c1)−h212f′′′(c2) f(x+h) = f(x) + hf'(x) + \frac{h^2}{2}f''(x) + \frac{h^3}{6}f'''(c_1) \\\\ f(x-h) = f(x) - hf'(x) + \frac{h^2}{2}f''(x) - \frac{h^3}{6}f'''(c_1) \\\\ f'(x) = \frac{f(x+h) - f(x-h)}{2h} - \frac{h^2}{12}f'''(c_1) - \frac{h^2}{12}f'''(c_2)

where x−h<c2<x<c1<x+hx-h < c_2 < x < c_1 < x+h

we can use Generalized Intermediate Value Theorem to replace −h212f′′′(c1)−h212f′′′(c2)-\frac{h^2}{12}f'''(c_1) - \frac{h^2}{12}f'''(c_2) with −h26f′′′(c)-\frac{h^2}{6}f'''(c)

# Generalized Intermediate Value Theorem

a1f(xi)+⋯+anf(xi)≤a1f(x1)+⋯+anf(xn)≤a1f(xj)+⋯+anf(xj)f(xi)≤a1f(x1)+⋯+anf(xn)a1+⋯+an≤f(xj) \begin{align} & a_1f(x_i) + \cdots + a_nf(x_i) \le a_1f(x_1) + \cdots + a_nf(x_n) \le a_1f(x_j) + \cdots + a_nf(x_j) \\\\ & f(x_i) \le \frac{a_1f(x_1) + \cdots + a_nf(x_n)} {a_1 + \cdots + a_n} \le f(x_j) \end{align}

by the Intermediate Value Theorem, there is a number cc between xix_i and xjx_j such that

f(c)=a1f(x1)+⋯+anf(xn)a1+⋯+an f(c) = \frac{a_1f(x_1) + \cdots + a_nf(x_n)} {a_1 + \cdots + a_n}

# Three-point centered-difference formula

f′(x)=f(x+h)−f(x−h)2h−h26f′′′(c) f'(x) = \frac{f(x+h) - f(x-h)}{2h} - \frac{h^2}{6}f'''(c)

# Three-point centered-difference formula for second derivative

f(x+h)=f(x)+hf′(x)+h22f′′(x)+h36f′′′(x)+h424f(iv)(c1)f(x−h)=f(x)−hf′(x)+h22f′′(x)−h36f′′′(x)+h424f(iv)(c1)f′′(x)=f(x−h)−2f(x)+f(x+h)h2−h212f(iv)(c) \begin{align} & f(x+h) = f(x) + hf'(x) +\frac{h^2}{2} f''(x) + \frac{h^3}{6}f'''(x) + \frac{h^4}{24}f^{(iv)}(c_1) \\\\ & f(x-h) = f(x) - hf'(x) +\frac{h^2}{2} f''(x) - \frac{h^3}{6}f'''(x) + \frac{h^4}{24}f^{(iv)}(c_1) \\\\ & f''(x) = \frac{f(x-h) - 2f(x) + f(x+h)}{h^2} - \frac{h^2}{12}f^{(iv)}(c) \end{align}

where c between x−hx-h and x+hx+h

# Rounding error

denote the floating point version of the input f(x+h)f(x+h) by f^(x+h)\hat{f}(x+h)

f^(x+h)=f(x+h)+ϵ1f^(x−h)=f(x−h)+ϵ2 \hat{f}(x+h) = f(x+h) + \epsilon_1 \\ \hat{f}(x-h) = f(x-h) + \epsilon_2 \\

where ∣ϵ1∣,∣ϵ2∣≈ϵmach|\epsilon_1|,|\epsilon_2|\approx \epsilon_{mach}

f′(x)correct−f′(x)machine=f′(x)−f^(x+h)−f^x−h2h=f′(x)−f(x+h)+ϵ1−(f(x−h)+ϵ2)2h=(f′(x)−f(x+h)−f(x−h)2h)+ϵ2−ϵ12h=(f′(x)correct−f′(x)formula)+errorrounding \begin{align} f'(x)_{correct} - f'(x)_{machine} &= f'(x) - \frac{\hat{f}(x+h) - \hat{f}{x-h}}{2h} \\ &=f'(x) - \frac{f(x+h) + \epsilon_1 - (f(x-h) + \epsilon_2)}{2h} \\ &=(f'(x) - \frac{f(x+h) - f(x-h)}{2h}) + \frac{\epsilon_2 - \epsilon_1}{2h} \\ &=(f'(x)_{correct} - f'(x)_{formula}) + error_{rounding} \end{align}

∣ϵ2−ϵ12h∣≤2ϵmach2h=ϵmachh \left|\frac{\epsilon_2 - \epsilon_1}{2h}\right| \le \frac{2\epsilon_{mach}}{2h} = \frac{\epsilon_{mach}}{h}
E(h)≡h26f′′′(c)+ϵmachh E(h) \equiv \frac{h^2}{6}f'''(c) + \frac{\epsilon_{mach}}{h}

where x−h<c<x+hx-h < c < x+h

the minimum of E(h)E(h) occurs at the solution of 0=E′(h)0 = E'(h)

# Extrapolation

Assume that we are presented with an order nn formula F(h)F(h) for approximating a given quantity Q. The order means that

Q≈F(h)+Khn Q\approx F(h) + Kh^n

where KK is roughly constant over the range of hh in which we are interested. A relevant example is

f′(x)=f(x+h)−f(x−h)2h−f′′′(ch)6h2 f'(x) = \frac{f(x+h) - f(x-h)}{2h} - \frac{f'''(c_h)}{6}h^2

the point chc_h lies between xx and x+hx+h


F(h)+K(h)n−F(h/2)≈12n(F(h)+Khn−F(h))Q−F(h/2)≈12n(Q−F(h)) \begin{align} F(h) + K(h)^n - F(h/2) &\approx \frac{1}{2^n}(F(h) + Kh^n - F(h))\\ Q - F(h/2) &\approx \frac{1}{2^n}(Q - F(h))\\ \end{align}

# Extrapolation for order n formula

Q≈2nF(h/2)−F(h)2n−1 Q \approx \frac{ 2^nF(h/2) - F(h)}{2^n-1}

use this extrapolation formula (sometimes called Richardson extrapolation) to leverage an order nn formula into one of higher order.

# Newton-Cotes Formulas for Numerical Integration

  • (a)
    the region under the line interpolating the data points (0,0)(0,0) and (h,1)(h,1). the region is a triangle of height 1 and base h

    the area is

    ∫0hxhdx=h/2 \int_0^h\frac{x}{h}dx = h/2
  • (b)
    the region under the parabola P(x)P(x) interpolating the data points (−h,0),(0,1)(-h,0),(0,1) and (h,0)(h,0) ,which area is

    ∫−hhx2−h2h2dx=43h \int_{-h}^{h} \frac{x^2-h^2}{h^2}dx = \frac{4}{3}h
  • (c)
    the region between the x-axis and the parabola interpolating the data points (−h,1),(0,0)(-h,1),(0,0) and (h,0)(h,0) , with net positive area

    ∫−h0x2−xh2h2+∫0hx2−xh2h2=13h \int_{-h}^{0}\frac{x^2-xh}{2h^2} + \int_{0}^{h}\frac{x^2-xh}{2h^2} = \frac{1}{3}h

# Trapezoid Rule

∫x0x1f(x)dx=h2(y0+y1)−h312f′′(c) \int_{x_0}^{x_1} f(x)dx = \frac{h}{2}(y_0+y_1) - \frac{h^3}{12}f''(c)

where h=x1−x0h = x_1 - x_0 and cc is between x0x_0 and x1x_1

details

consider the degree 1 interpolating polynomial P1(x)P_1(x) through (x0,y0)(x_0,y_0) and (x1,y1)(x_1,y_1)

f(x)=y0x−x1x0−x1+y1x−x0x1−x0+(x−x0)(x−x1)2!f′′(cx)=P(x)+E(x)∫x0x1f(x)dx=∫x0x1P(x)dx+∫x0x1E(x)dx∫x0x1P(x)dx=y0∫x0x1x−x1x0−x1dx+y1∫x0x1x−x0x1−x0dx=y0x1−x02+y1x1−x02=(x1−x0)y0+y12∫x0x1E(x)dx=−(x1−x0)312f′′(c) \begin{align} & f(x) = y_0 \frac{x-x_1}{x_0 -x_1} + y_1 \frac{x-x_0}{x_1 -x_0} + \frac{(x-x_0)(x-x_1)}{2!}f''(c_x) = P(x) + E(x) \\\\ & \int_{x_0}^{x_1} f(x)dx = \int_{x_0}^{x_1}P(x) dx + \int_{x_0}^{x_1} E(x)dx \\\\ & \begin{align} \int_{x_0}^{x_1}P(x)dx &= y_0\int_{x_0}^{x_1}\frac{x-x_1}{x_0-x_1}dx + y_1\int_{x_0}^{x_1}\frac{x-x_0}{x_1-x_0}dx\\ &=y_0\frac{x_1 - x_0}{2} + y_1\frac{x_1 - x_0}{2} = (x_1 - x_0)\frac{y_0+y_1}{2} \end{align} \\\\ & \int_{x_0}^{x_1} E(x)dx = -\frac{(x_1-x_0)^3}{12} f''(c) \end{align}

# Simpson's Rule

∫x0x2f(x)dx=h3(y0+4y1+y2)−h590f(iv)(c) \int_{x_0} ^{x_2} f(x) dx = \frac{h}{3}(y_0+4y_1+y_2) - \frac{h^5}{90}f^{(iv)}(c)

where h=x2−x1=x1−x0h = x_2 - x_1 = x_1 - x_0 and cc is between x0x_0 and x2x_2

details

simpson's rule is similar to trapezoid rule , except that the degree 1 interpolant is replaced by a parabola

# Simpson's 3/8 Rule

∫x0x3f(x)dx≈3h8(y0+3y1+3y2+y3) \int_{x_0}^{x_3} f(x)dx \approx \frac{3h}{8}(y_0 + 3y_1 + 3y_2 + y_3)

# Composite Newton-Cotes formulas

dividing the interval up into several subintervals, applying the rule separately on each one, and then totaling up . The strategy is called composite numerical integration

# Composite Trapezoid Rule

∫abf(x)dx=h2[f(a)+f(b)+2∑i=1m−1f(xi)]−∑i=0m−1h312f′′(ci) \int_a^b f(x)dx = \frac{h}{2}\left[f(a) + f(b) + 2\sum_{i=1}^{m-1}f(x_i)\right] -\sum_{i=0}^{m-1}\frac{h^3}{12}f''(c_i)

the error term can be written

h312∑i=1m−1f′′(ci)=h312mf′′(c) \frac{h^3}{12}\sum_{i=1}^{m-1}f''(c_i) = \frac{h^3}{12}mf''(c)

where mh=(b−a)mh = (b-a) and a<c<ba<c<b and the f′′f'' is continuous on [a,b][a,b]

∫abf(x)dx=h2[y0+ym+2∑i=1m−1yi]−(b−a)h212f′′(c) \int_a^b f(x)dx = \frac{h}{2}\left[y_0 + y_m + 2\sum_{i=1}^{m-1}y_i\right] -\frac{(b-a)h^2}{12}f''(c)

# Composite Simpson's Rule

∫abf(x)dx=h3[y0+y2m+4∑i=1my2i−1+2∑i=1m−1y2i]−(b−a)h4180f(iv)(c) \int_a^b f(x)dx = \frac{h}{3}\left[y_0 + y_{2m} + 4\sum_{i=1}^{m}y_{2i-1}+2\sum_{i=1}^{m-1}y_{2i}\right] - \frac{(b-a)h^4}{180}f^{(iv)}(c)

where cc is between aa and bb , the subintervals is

∫x2ix2i+2f(x)dx=h3[f(x2i)+4f(x2i+1)+f(x2i+2)]−h590f(iv)(ci) \int_{x_{2i}}^{x_{2i+2}}f(x)dx = \frac{h}{3}[f(x_{2i}) + 4f(x_{2i+1}) + f(x_{2i+2})] - \frac{h^5}{90}f^{(iv)}(c_i)