Numerical Differentiation and Integration

Numerical Differentiation

Basic Problems

  • Problem 1: Derive a formula that approximates the derivative of a function using a linear combination of function values. This is applicable when the function is known.
  • Problem 2: Approximate the value of a derivative of a function defined by discrete data.

Solution Approaches

  • Use Taylor Series Expansion.
  • Pass a polynomial through the given data and differentiate the interpolating polynomial.

Applications

  • Solving Ordinary and Partial Differential Equations.

First Derivative

  • For a function f:[a,b]Rf : [a, b] \rightarrow R, the derivative f(c)f'(c) at a point cc is defined as: f(c)=limh0f(c+h)f(c)hf'(c) = \lim_{h \to 0} \frac{f(c + h) - f(c)}{h}, for all c(a,b)c \in (a, b)
  • Geometrically, f(c)f'(c) represents the slope of the tangent to the curve f(x)f(x) at x=cx = c.

Taylor Series Derivative

  • Taylor's Theorem: If ff has two continuous derivatives, then: f(x<em>0+h)=f(x</em>0)+hf(x<em>0)+h22f(θ)f(x<em>0 + h) = f(x</em>0) + hf'(x<em>0) + \frac{h^2}{2}f''(\theta), where θ(x</em>0,x0+h)\theta \in (x</em>0, x_0 + h)
  • Forward Formula: f(x<em>0)f(x</em>0+h)f(x0)hf'(x<em>0) \approx \frac{f(x</em>0 + h) - f(x_0)}{h}

Absolute Error

  • Error is defined as: true valueapproximate value|\text{true value} - \text{approximate value}|
  • Error bound for the Forward Formula: E<em>D(f)max</em>θ[a,b]h2f(θ)|E<em>D(f)| \leq \max</em>{\theta \in [a, b]} \frac{h}{2} |f''(\theta)|
  • Example: Find the derivative of f(x)=x2f(x) = x^2 at x=1x = 1 with h=0.1h = 0.1 using the Taylor series.

Other Formulae

  • Backward Formula: f(x<em>0)=f(x</em>0)f(x0h)hf'(x<em>0) = \frac{f(x</em>0) - f(x_0 - h)}{h}
  • Central Formula: f(x<em>0)=f(x</em>0+h)f(x0h)2hf'(x<em>0) = \frac{f(x</em>0 + h) - f(x_0 - h)}{2h}
  • Second Derivative Approximation: f(x<em>0)f(x</em>0+h)2f(x<em>0)+f(x</em>0h)h2f''(x<em>0) \approx \frac{f(x</em>0 + h) - 2f(x<em>0) + f(x</em>0 - h)}{h^2}

Derivative for Discrete Data

  • Given data points: (x<em>0,f(x</em>0)),(x<em>1,f(x</em>1)),(x<em>2,f(x</em>2)),,(x<em>n,f(x</em>n))(x<em>0, f(x</em>0)), (x<em>1, f(x</em>1)), (x<em>2, f(x</em>2)), …, (x<em>n, f(x</em>n))
  • Assumption: Data points are equispaced, i.e., x<em>ix</em>i1=hx<em>i - x</em>{i-1} = h
  • Remark 1: Newton's divided difference (or Forward/Backward) formula can be used, depending on the point's location.
  • Remark 2: If data is not equispaced, Lagrange interpolating polynomial can be used.

Newton's Forward Difference Formula

  • Formula: f(x)P<em>n(x)=f(x</em>0)+<em>k=1n(sk)Δkf(x</em>0)f(x) \approx P<em>n(x) = f(x</em>0) + \sum<em>{k=1}^{n} \binom{s}{k} \Delta^k f(x</em>0), where x=x0+shx = x_0 + sh
  • We use P<em>n(x)P<em>n(x) to calculate the derivatives of ff, i.e., f(x)P</em>n(x)f'(x) \approx P'</em>n(x) for all x[x<em>0,x</em>n]x \in [x<em>0, x</em>n]

Derivative Calculation Using Newton's Forward Difference

  • First derivative: f(x)P(x)=dP<em>ndx=1hdP</em>ndsf'(x) \approx P'(x) = \frac{dP<em>n}{dx} = \frac{1}{h} \frac{dP</em>n}{ds}
  • Second derivative: f(x)d2P<em>ndx2=1h2d2P</em>nds2f''(x) \approx \frac{d^2P<em>n}{dx^2} = \frac{1}{h^2} \frac{d^2P</em>n}{ds^2}
  • In general: f(K)(x)=1hKdKPndsKf^{(K)}(x) = \frac{1}{h^K} \frac{d^KP_n}{ds^K}
  • Example: Compute the first and second derivatives at x=2x = 2 for the function defined by the following data using Taylor series expansion and Newton's forward divided difference.
    • x: 1, 2, 3, 4
    • f(x): 2, 5, 7, 10

Solution using Taylor Series

  • Here h=1h = 1
  • f(2)=f(2+h)f(2)h=f(3)f(2)1=2f'(2) = \frac{f(2 + h) - f(2)}{h} = \frac{f(3) - f(2)}{1} = 2
  • f(2)=f(2+h)2f(2)+f(2h)h2=f(3)2f(2)+f(1)1=1f''(2) = \frac{f(2 + h) - 2f(2) + f(2 - h)}{h^2} = \frac{f(3) - 2f(2) + f(1)}{1} = 1

Solution using Newton's Forward Difference Formula

  • Difference table:

    • x: 1, 2, 3, 4
    • f(x): 2, 5, 7, 10
    • Δf\Delta f: 3, 2, 3
    • Δ2f\Delta^2 f: -1, 1
    • Δ3f\Delta^3 f: 2
  • f(x)P<em>n(x)=f(x</em>0)+sΔf(x<em>0)+s(s1)2!Δ2f(x</em>0)+s(s1)(s2)3!Δ3f(x0)f(x) \approx P<em>n(x) = f(x</em>0) + s\Delta f(x<em>0) + \frac{s(s - 1)}{2!} \Delta^2 f(x</em>0) + \frac{s(s - 1)(s - 2)}{3!} \Delta^3 f(x_0)

  • f(x)1hdP<em>nds=1h[Δf(x</em>0)+2s12!Δ2f(x<em>0)+3s26s+23!Δ3f(x</em>0)]f'(x) \approx \frac{1}{h} \frac{dP<em>n}{ds} = \frac{1}{h} \left[\Delta f(x</em>0) + \frac{2s - 1}{2!} \Delta^2 f(x<em>0) + \frac{3s^2 - 6s + 2}{3!} \Delta^3 f(x</em>0)\right]

  • Here x=2,x0=1,s=1,h=1x = 2, x_0 = 1, s = 1, h = 1

  • f(2)=312+26=136f'(2) = 3 - \frac{1}{2} + \frac{2}{6} = \frac{13}{6}

  • f(x)1h2d2P<em>nds2=1h2[Δ2f(x</em>0)+(s1)Δ3f(x0)]f''(x) \approx \frac{1}{h^2} \frac{d^2P<em>n}{ds^2} = \frac{1}{h^2} \left[\Delta^2 f(x</em>0) + (s - 1)\Delta^3 f(x_0)\right]

  • f(2)=1f''(2) = -1

  • Example 3: Calculate f(4)(0.15)f^{(4)}(0.15)

    • Given data:
      • x: 0.1, 0.2, 0.3, 0.4, 0.5, 0.6
      • f(x): 0.425, 0.475, 0.400, 0.450, 0.525, 0.575

Numerical Integration

Basics

  • If f:[a,b]Rf : [a, b] \rightarrow R is integrable, we obtain a new function F:[a,b]RF : [a, b] \rightarrow R defined by: F(x)=axf(t)dtF(x) = \int_{a}^{x} f(t)dt, for all x[a,b]x \in [a, b]
  • If ff is a nonnegative function, then abf(x)dx\int_{a}^{b} f(x)dx represents the area under the curve f(x)f(x).

Antiderivative

  • If f=Ff = F', then FF is called an antiderivative of ff.
  • Fundamental Theorem of Calculus: If f:[a,b]Rf : [a, b] \rightarrow R is integrable and has an antiderivative FF, then: abf(x)dx=F(b)F(a)\int_{a}^{b} f(x)dx = F(b) - F(a)

Basic Problems

  • Difficult to find an antiderivative (e.g., f(x)=ex2f(x) = e^{-x^2}).
  • Function is given in discrete form (data points).

Newton-Cotes Methods/Formulae

  • Based on polynomial interpolation.
  • Replace ff by P<em>n(x)P<em>n(x) and evaluate </em>abPn(x)dx\int</em>{a}^{b} P_n(x)dx
  • <em>abf(x)dx</em>abP<em>n(x)dx=</em>ab<em>i=0nL</em>i(x)f(x<em>i)dx=</em>i=0nf(x<em>i)</em>abL<em>i(x)dx=</em>i=0nA<em>if(x</em>i)\int<em>{a}^{b} f(x)dx \approx \int</em>{a}^{b} P<em>n(x)dx = \int</em>{a}^{b} \sum<em>{i=0}^{n} L</em>i(x)f(x<em>i)dx = \sum</em>{i=0}^{n} f(x<em>i) \int</em>{a}^{b} L<em>i(x)dx = \sum</em>{i=0}^{n} A<em>i f(x</em>i)
  • Where A<em>i=</em>abLi(x)dxA<em>i = \int</em>{a}^{b} L_i(x)dx is called the weight.

Types of Newton-Cotes Formulae

  • Quadrature formulae or rule
  • n=1: Trapezoidal Rule
  • n=2: Simpson’s rule
  • n=3: Simpson’s Three-Eighths rule
  • n=4:
  • Generalized formula

Quadrature Formulae

  • If x<em>0,,x</em>nx<em>0, …, x</em>n are distinct numbers in [a,b][a, b]
  • And fCn+1[a,b]f \in C^{n+1}[a, b]. Then, for all x[a,b]x \in [a, b], there exists ξ(x)(a,b)\xi(x) \in (a, b) such that:
    f(x)=P<em>n(x)+f(n+1)(ξ(x))(n+1)!(xx</em>0)(xx<em>1)(xx</em>n)f(x) = P<em>n(x) + \frac{f^{(n+1)}(\xi(x))}{(n + 1)!} (x - x</em>0)(x - x<em>1)…(x - x</em>n)
  • Where P<em>n(x)P<em>n(x) is the interpolating polynomial defined as: P</em>n(x)=<em>i=0nf(x</em>i)Li(x)P</em>n(x) = \sum<em>{i=0}^{n} f(x</em>i)L_i(x)
  • Quadrature Formula:
    f(x)<em>i=0nA</em>if(xi)f(x) \approx \sum<em>{i=0}^{n} A</em>i f(x_i)
  • Error Term:
    E(f)=1(n+1)!<em>abf(n+1)(ξ(x))</em>i=0n(xxi)dxE(f) = \frac{1}{(n + 1)!} \int<em>{a}^{b} f^{(n+1)}(\xi(x)) \prod</em>{i=0}^{n} (x - x_i) dx

Trapezoidal Rule

  • Objective: To approximate F=abf(x)dxF = \int_{a}^{b} f(x)dx
  • First order Lagrange polynomial: n=1,x<em>0=a,x</em>1=b,h=(ba)n = 1, x<em>0 = a, x</em>1 = b, h = (b - a)
  • P<em>n(x)=xx</em>1x<em>0x</em>1f(x<em>0)+xx</em>0x<em>1x</em>0f(x1)P<em>n(x) = \frac{x - x</em>1}{x<em>0 - x</em>1} f(x<em>0) + \frac{x - x</em>0}{x<em>1 - x</em>0} f(x_1)
  • <em>abf(x)dx=</em>x<em>0x</em>1[xx<em>1x</em>0x<em>1f(x</em>0)+xx<em>0x</em>1x<em>0f(x</em>1)]dx+12<em>x</em>0x<em>1f(ξ(x))(xx</em>0)(xx1)dx\int<em>{a}^{b} f(x)dx = \int</em>{x<em>0}^{x</em>1} \left[\frac{x - x<em>1}{x</em>0 - x<em>1} f(x</em>0) + \frac{x - x<em>0}{x</em>1 - x<em>0} f(x</em>1)\right]dx + \frac{1}{2} \int<em>{x</em>0}^{x<em>1} f''(\xi(x))(x - x</em>0)(x - x_1)dx

Trapezoidal Rule Error

  • The term (xx<em>0)(xx</em>1)(x - x<em>0)(x - x</em>1) does not change sign in [x<em>0,x</em>1][x<em>0, x</em>1] therefore applying weighted mean value theorem:
    <em>x</em>0x<em>1f(ξ(x))(xx</em>0)(xx<em>1)dx=f(ξ)</em>x<em>0x</em>1(xx<em>0)(xx</em>1)dx=f(ξ)[x33(x<em>1+x</em>0)x22+x<em>0x</em>1x]<em>x</em>0x1=h36f(ξ)\int<em>{x</em>0}^{x<em>1} f''(\xi(x))(x - x</em>0)(x - x<em>1)dx= f''(\xi) \int</em>{x<em>0}^{x</em>1} (x - x<em>0)(x - x</em>1)dx = f''(\xi) \Big[\frac{x^3}{3} - (x<em>1 + x</em>0) \frac{x^2}{2} + x<em>0x</em>1x \Big]<em>{x</em>0}^{x_1} = - \frac{h^3}{6} f''(\xi)

Trapezoidal Rule Formula

  • <em>abf(x)dx=(xx</em>1)22(x<em>0x</em>1)f(x<em>0)+(xx</em>0)22(x<em>1x</em>0)f(x<em>1)</em>x<em>0x</em>1h312f(ξ)=(x<em>1x</em>0)2(f(x<em>0)+f(x</em>1))h312f(ξ)\int<em>{a}^{b} f(x)dx=\frac{(x - x</em>1)^2}{2(x<em>0 - x</em>1)}f(x<em>0) + \frac{(x - x</em>0)^2}{2(x<em>1 - x</em>0)}f(x<em>1)\Big|</em>{x<em>0}^{x</em>1} − \frac{h^3}{12}f''(\xi) = \frac{(x<em>1 - x</em>0)}{2} (f(x<em>0) + f(x</em>1)) - \frac{h^3}{12}f''(\xi)
  • With h=x<em>1x</em>0h = x<em>1 - x</em>0 the Trapezoidal rule is:
    <em>abf(x)dx=h2(f(x</em>0)+f(x1))h312f(ξ)\int<em>{a}^{b} f(x)dx= \frac{h}{2} (f(x</em>0) + f(x_1)) - \frac{h^3}{12}f''(\xi)
  • Exact for polynomials of degree one or less as the error term involves f(ξ)f''(\xi)

Simpson's 1/3 Rule

  • Objective: To approximate F=abf(x)dxF = \int_{a}^{b} f(x)dx
  • Second order Lagrange polynomial: n=2,x<em>0=a,x</em>2=b,x1=a+h,h=(ba)/2n = 2, x<em>0 = a, x</em>2 = b, x_1 = a + h, h = (b - a)/2
  • <em>abf(x)dx=</em>x<em>0x</em>2Pn(x)dx+<em>x</em>0x<em>2(xx</em>0)(xx<em>1)(xx</em>2)f(ξ(x))6dx\int<em>{a}^{b} f(x)dx = \int</em>{x<em>0}^{x</em>2} Pn(x)dx + \int<em>{x</em>0}^{x<em>2} (x - x</em>0)(x - x<em>1)(x - x</em>2) \frac{f'''(\xi(x))}{6}dx
  • Third Taylor polynomial about x<em>1x<em>1: f(x)=f(x</em>1)+f(x<em>1)(xx</em>1)+f(x<em>1)(xx</em>1)22+f(x<em>1)(xx</em>1)36+f(4)(ξ(x))24(xx1)4f(x) = f(x</em>1) + f'(x<em>1)(x - x</em>1) + f''(x<em>1)\frac{(x - x</em>1)^2}{2} + f'''(x<em>1)\frac{(x - x</em>1)^3}{6} + \frac{f^{(4)}(\xi(x))}{24}(x - x_1)^4
  • Integration of the Taylor polynomial:
    <em>x</em>0x<em>2f(x)dx=2hf(x</em>1)+h33f(x<em>1)+f(4)(ξ</em>1)60h5\int<em>{x</em>0}^{x<em>2} f(x)dx = 2hf(x</em>1) + \frac{h^3}{3} f''(x<em>1) + \frac{f^{(4)}(\xi</em>1)}{60}h^5
  • Replacing f(x<em>1)f''(x<em>1) by its approximation: </em>x<em>0x</em>2f(x)dx=h3[f(x<em>0)+4f(x</em>1)+f(x<em>2)]h512(13f(4)(ξ</em>2)15f(4)(ξ1))\int</em>{x<em>0}^{x</em>2} f(x)dx = \frac{h}{3} [f(x<em>0) + 4f(x</em>1) + f(x<em>2)] - \frac{h^5}{12} (\frac{1}{3} f^{(4)}(\xi</em>2) - \frac{1}{5} f^{(4)}(\xi_1) )

Simpson's 1/3 Rule Formula

  • <em>x</em>0x<em>2f(x)dx=h3[f(x</em>0)+4f(x<em>1)+f(x</em>2)]h590f(4)(ξ)\int<em>{x</em>0}^{x<em>2} f(x)dx=\frac{h}{3} [f(x</em>0) + 4f(x<em>1) + f(x</em>2)] - \frac{h^5}{90} f^{(4)}(\xi)

Simpson's 3/8 Rule

  • Objective: To approximate F=abf(x)dxF = \int_{a}^{b} f(x)dx

  • Third order Lagrange polynomial: n=3,x<em>0=a,x</em>3=a+3h=b,x<em>1=a+h,x</em>2=a+2h,h=(ba)/3n = 3, x<em>0 = a, x</em>3 = a + 3h = b, x<em>1 = a + h, x</em>2 = a + 2h, h = (b - a)/3

  • <em>x</em>0x<em>3f(x)dx=3h8[f(x</em>0)+3f(x<em>1)+3f(x</em>2)+f(x3)]3h580f(4)(ξ)\int<em>{x</em>0}^{x<em>3} f(x)dx = \frac{3h}{8} [f(x</em>0) + 3f(x<em>1) + 3f(x</em>2) + f(x_3)] - \frac{3h^5}{80} f^{(4)}(\xi), with ξ(a,b)\xi \in (a, b)

  • Example 4: Using Trapezoidal, Simpson 1/3 and 3/8 rules, find <em>02x4dx\int<em>{0}^{2} x^4 dx and </em>02sin(x)dx\int</em>{0}^{2} sin(x) dx and find the upper bound for the error.

Composite Rules

  • If the interval [a,b][a, b] is large, the error using Trapezoidal or Simpson's rule will be large.
  • Idea: Divide [a,b][a, b] into equal subintervals and apply Trapezoidal or Simpson's rules in each subinterval to reduce error.

Composite Simpson's Rule

  • With h=(ba)/nh = (b - a)/n and xj=a+jhx_j = a + jh, for j=0,1,,nj = 0, 1, …, n
  • <em>abf(x)dx=</em>j=1n/2<em>x</em>2j2x<em>2jf(x)dx=</em>j=1n/2h3[f(x<em>2j2)+4f(x</em>2j1)+f(x<em>2j)]h590f(4)(ξ</em>j)\int<em>{a}^{b} f(x)dx = \sum</em>{j=1}^{n/2} \int<em>{x</em>{2j-2}}^{x<em>{2j}} f(x)dx = \sum</em>{j=1}^{n/2} \frac{h}{3} [f(x<em>{2j-2}) + 4f(x</em>{2j-1}) + f(x<em>{2j})] - \frac{h^5}{90} f^{(4)}(\xi</em>j)
  • <em>abf(x)dx=h3f(x</em>0)+2<em>j=1(n/2)1f(x</em>2j)+4<em>j=1n/2f(x</em>2j1)+f(x<em>n)h590</em>j=1n/2f(4)(ξj)\int<em>{a}^{b} f(x)dx= \frac{h}{3} f(x</em>0) + 2 \sum<em>{j=1}^{(n/2)-1} f(x</em>{2j}) + 4\sum<em>{j=1}^{n/2} f(x</em>{2j-1}) + f(x<em>n) − \frac{h^5}{90} \sum</em>{j=1}^{n/2} f^{(4)}(\xi_j)

Composite Simpson's Rule Error

  • Error: E(f)=h590<em>j=1n/2f(4)(ξ</em>j)E(f) = - \frac{h^5}{90} \sum<em>{j=1}^{n/2} f^{(4)}(\xi</em>j) for ξ<em>j(x</em>2j2,x2j)\xi<em>j \in (x</em>{2j-2}, x_{2j}) and j=1,,n/2j = 1, …, n/2
  • If fC4[a,b]f \in C^4[a, b], there exists μ(a,b)\mu \in (a, b) such that:
    • E(f)=(ba)180h4f(4)(μ)E(f) = - \frac{(b - a)}{180} h^4 f^{(4)}(\mu)

Composite Simpson's Rule Theorem

  • Let fC4[a,b]f \in C^4[a, b] and n be even, h=(ba)/nh = (b - a)/n, x<em>j=a+jhx<em>j = a + jh, for j=0,1,,nj = 0, 1, …, n. There exists a μ(a,b)\mu \in (a, b) for which the Composite Simpson’s rule for n subintervals can be written with its error term as: </em>abf(x)dx=h3f(a)+2<em>j=1(n/2)1f(x</em>2j)+4<em>j=1n/2f(x</em>2j1)+f(b)(ba)180h4f(4)(μ)\int</em>{a}^{b} f(x)dx=\frac{h}{3} f(a) + 2 \sum<em>{j=1}^{(n/2)-1} f(x</em>{2j}) + 4\sum<em>{j=1}^{n/2} f(x</em>{2j-1}) + f(b) − \frac{(b - a)}{180} h^4 f^{(4)}(\mu)

Composite Trapezoidal Rule Theorem

  • Let fC2[a,b]f \in C^2[a, b] and h=(ba)/n,x<em>j=a+jhh = (b - a)/n, x<em>j = a + jh, for j=0,1,,nj = 0, 1, …, n. There exists a μ(a,b)\mu \in (a, b) for which the Composite Trapezoidal rule for n subintervals can be written with its error term as: </em>abf(x)dx=h2f(a)+2<em>j=1(n1)f(x</em>j)+f(b)(ba)12h2f(μ)\int</em>{a}^{b} f(x)dx=\frac{h}{2} f(a) + 2 \sum<em>{j=1}^{(n-1)} f(x</em>{j}) + f(b) − \frac{(b - a)}{12} h^2 f''(\mu)

Composite Midpoint Rule Theorem

  • Let fC2[a,b]f \in C^2[a, b] and nn an even number, h=(ba)/(n+2),x<em>j=a+(j+1)hh = (b - a)/(n + 2), x<em>j = a + (j + 1)h, for j=1,0,1,,n+1j = -1, 0, 1, …, n + 1. There exists a μ(a,b)\mu \in (a, b) for which the Composite Midpoint rule for n+2n + 2 subintervals can be written with its error term as: </em>abf(x)dx=2h<em>j=0n/2f(x</em>2j)+(ba)6h2f(μ)\int</em>{a}^{b} f(x)dx= 2h\sum<em>{j=0}^{n/2} f(x</em>{2j}) + \frac{(b - a)}{6} h^2 f''(\mu)

  • Example 6: Determine values of n that will ensure an approximation error of less than 0.00002 when approximating 0πsin(x)dx\int_{0}^{\pi} sin(x)dx employing:

    • (a) Composite Trapezoidal rule
    • (b) Composite Simpson’s rule
  • For each case, approximate the integral with the smallest possible n.