3 Numerical Differentiation And Integration

Numerical Method · Unit 3 · 8 hrs

Numerical Differentiation and Integration

Exam-focused notes for Numerical Differentiation and Integration (Numerical Method, CSC212): what the TU syllabus asks and how it has actually been tested, with 11 solved past questions from this unit.

Numerical Differentiation and Integration in short

This chapter turns a function, whether given as a formula or only as a table of values, into an estimate of its derivative or its definite integral, using nothing but arithmetic on sampled values.

Numerical Differentiation and Integration: notes

Differentiating Continuous Functions (Two-Point and Three-Point Formula)

The definition of the derivative, $f'(x) = \lim_{h\to 0}\dfrac{f(x+h)-f(x)}{h}$, is turned into a usable numerical formula by simply fixing $h$ small instead of taking the limit. The two-point forward difference formula is $$f'(x) \approx \frac{f(x+h)-f(x)}{h},$$ and the two-point backward difference formula is $$f'(x) \approx \frac{f(x)-f(x-h)}{h}.$$ Both have error proportional to $h$ (first-order accurate). Averaging them gives the three-point central difference formula, $$f'(x) \approx \frac{f(x+h)-f(x-h)}{2h},$$ whose error is proportional to $h^2$, making it considerably more accurate for the same step size.

Worked example. For $f(x)=x^2$ at $x=1$ with $h=0.2$ (true derivative $f'(1)=2$): $$f'(1) \approx \frac{f(1.2)-f(1)}{0.2} = 2.2 \ \text{(forward)}, \qquad f'(1) \approx \frac{f(1)-f(0.8)}{0.2} = 1.8\ \text{(backward)},$$ $$f'(1) \approx \frac{f(1.2)-f(0.8)}{0.4} = 2.0\ \text{(central, exact here since } f \text{ is quadratic).}$$ The forward and backward estimates each carry an error of $0.2$ in opposite directions; the central estimate cancels that error to first order (and exactly, for a quadratic), which is why the central formula is preferred whenever $f$ can be evaluated on both sides of $x$.

Differentiating Tabulated Functions by using Newton's Differences

When $f$ is known only from a table of equally spaced values, its Newton forward-difference interpolating polynomial $P_n(x_0+sh)$ can be differentiated term by term with respect to $x$ (using $\frac{ds}{dx}=1/h$) to give the derivative directly from the difference table, without ever writing $f$ as a formula: $$f'(x_0+sh) \approx \frac{1}{h}\left[\Delta f(x_0) + \frac{2s-1}{2}\Delta^2 f(x_0) + \frac{3s^2-6s+2}{6}\Delta^3 f(x_0) + \cdots\right],$$ $$f''(x_0+sh) \approx \frac{1}{h^2}\left[\Delta^2 f(x_0) + (s-1)\Delta^3 f(x_0) + \cdots\right].$$ At the first tabulated point $x_0$ itself ($s=0$) these reduce to $f'(x_0) \approx \Delta f(x_0)/h$ and $f''(x_0) \approx \Delta^2f(x_0)/h^2$.

Worked example. For $f(x)=x^3$ tabulated at $x=0,1,2,3,4$ with $h=1$ ($f = 0,1,8,27,64$), the forward difference table is

$x$$f$$\Delta f$$\Delta^2 f$$\Delta^3 f$
00166
117126
281918
32737
464

At $x=1$ ($x_0=0$, $s=1$): $$f'(1) \approx \frac{1}{1}\left[1 + \frac{2(1)-1}{2}(6) + \frac{3(1)^2-6(1)+2}{6}(6)\right] = 1+3-1 = 3, \qquad f''(1) \approx 6 + (1-1)(6) = 6,$$ matching the exact values $f'(1)=3x^2|{x=1}=3$ and $f''(1)=6x|{x=1}=6$ exactly, because the tabulated data is exactly cubic and three (respectively two) difference terms fully capture a cubic's derivative.

Maxima and minima of Tabulated Functions

A tabulated function's maximum or minimum is found by locating where its interpolating polynomial has zero derivative: differentiate the Newton forward-difference formula with respect to $s$, set the result to zero, and solve for $s$ (hence $x=x_0+sh$).

Worked example. A function is tabulated as

$x$012345
$f(x)$202426262420

Taking $x_0=2$, the first differences are $\Delta f(2)=0$, and the second difference is $\Delta^2f(2) = -2$. Using only up to the quadratic term, $f'(x_0+sh) = \frac{1}{h}\left[\Delta f(x_0) + \frac{2s-1}{2}\Delta^2f(x_0)\right]$; setting this to zero, $$0 + \frac{2s-1}{2}(-2) = 0 ;\Rightarrow; s = 0.5 ;\Rightarrow; x^\ast = 2 + 0.5(1) = 2.5.$$ The interpolated extreme value is $P_2(2.5) = 26 + 0.5(0) + \frac{0.5(-0.5)}{2}(-2) = 26.25$. Since the underlying function here is $f(x)=20+5x-x^2$, the exact vertex is at $x=2.5$ with $f(2.5)=26.25$, so the tabulated-data procedure recovers the true maximum exactly.

What this unit covers

  • Differentiating Continuous Functions (Two-Point and Three-Point Formula)
  • Differentiating Tabulated Functions by using Newton's Differences
  • Maxima and minima of Tabulated Functions
  • Newton-Cote's Quadrature Formulas
  • Trapezoidal rule
  • Multi-Segment Trapezoidal rule
  • Simpson's 1/3 rule
  • Multi-Segment Simpson's 1/3 rule
  • Simpson's 3/8 rule
  • Multi-Segment Simpson's 3/8 rule
  • Gaussian integration algorithm
  • Romberg integration

Differentiating Tabulated Functions by using Newton's Differences

2081 · 5 marks

What is numerical differentiation? The table below gives the values of distance travelled by a vehicle at various time intervals. Estimate the velocity and acceleration at $x=4$.

Time $(x)$124810
Distance $(y)$0152127

[5]

Show solved answer

Numerical Differentiation

Definition

Numerical differentiation is the process of estimating the derivative (rate of change) of a function from a set of tabulated/discrete data values, instead of differentiating a closed-form expression analytically. It is used when the function is either unknown (only data points are given) or too complex to differentiate directly. The derivatives are obtained by differentiating an interpolating polynomial (Newton forward/backward for equal spacing, Newton's divided differences for unequal spacing).

Here velocity $= \dfrac{dy}{dx}$ and acceleration $= \dfrac{d^2y}{dx^2}$.


Given Data

Time $x$124810
Distance $y$0152127

The nodes are unequally spaced, so I use Newton's Divided Difference interpolation.


Step 1: Divided Difference Table

First DD: $$f[x_0,x_1]=\tfrac{1-0}{2-1}=1,\quad f[x_1,x_2]=\tfrac{5-1}{4-2}=2$$ $$f[x_2,x_3]=\tfrac{21-5}{8-4}=4,\quad f[x_3,x_4]=\tfrac{27-21}{10-8}=3$$

Second DD: $$f[x_0,x_1,x_2]=\tfrac{2-1}{4-1}=\tfrac13,\quad f[x_1,x_2,x_3]=\tfrac{4-2}{8-2}=\tfrac13,\quad f[x_2,x_3,x_4]=\tfrac{3-4}{10-4}=-\tfrac16$$

Third DD: $$f[x_0,x_1,x_2,x_3]=\tfrac{\tfrac13-\tfrac13}{8-1}=0,\quad f[x_1,x_2,x_3,x_4]=\tfrac{-\tfrac16-\tfrac13}{10-2}=\tfrac{-\tfrac12}{8}=-\tfrac1{16}$$

Fourth DD: $$f[x_0,\dots,x_4]=\tfrac{-\tfrac1{16}-0}{10-1}=-\tfrac1{144}$$


Step 2: Newton's Divided Difference Polynomial

$$P(x)=(x-1)+\tfrac13(x-1)(x-2)+0-\tfrac1{144}(x-1)(x-2)(x-4)(x-8)$$


Step 3: Velocity = $P'(4)$

Term 1: $\dfrac{d}{dx}(x-1)=1$

Term 2: $\dfrac13\big[(x-2)+(x-1)\big]=\dfrac13(2x-3)$; at $x=4$: $\dfrac13(5)=\dfrac53$

Term 3: coefficient $0\Rightarrow 0$

Term 4: Let $g(x)=(x-1)(x-2)(x-4)(x-8)$. $$g'(x)=(x-2)(x-4)(x-8)+(x-1)(x-4)(x-8)+(x-1)(x-2)(x-8)+(x-1)(x-2)(x-4)$$ At $x=4$, only the term without $(x-4)$ survives: $$g'(4)=(3)(2)(-4)=-24$$ Contribution $=-\dfrac1{144}(-24)=\dfrac{1}{6}$

Velocity: $$P'(4)=1+\tfrac53+0+\tfrac16=\frac{6+10+1}{6}=\frac{17}{6}\approx 2.833$$

$$\boxed{\text{Velocity}\approx 2.83\ \text{units/time}}$$


Step 4: Acceleration = $P''(4)$

Term 1: second derivative $=0$

Term 2: $\dfrac13(2x-3)\Rightarrow \dfrac13(2)=\dfrac23$

Term 3: $0$

Term 4: Need $g''(4)$ where $g(x)=(x-1)(x-2)(x-4)(x-8)$.

Differentiate $g'(x)$ (sum of four triple products). $g''(x)$ is the sum of all products of two factors, each taken from three of the roots. A cleaner way: expand near the surviving structure. Since terms with $(x-4)$ vanish in first derivative, for $g''$ we differentiate:

$$g'(x)=\underbrace{(x-2)(x-4)(x-8)}{A}+\underbrace{(x-1)(x-4)(x-8)}{B}+\underbrace{(x-1)(x-2)(x-8)}{C}+\underbrace{(x-1)(x-2)(x-4)}{D}$$

At $x=4$:

  • $A'=(x-4)(x-8)+(x-2)(x-8)+(x-2)(x-4)$; at 4: $0+(2)(-4)+0=-8$
  • $B'=(x-4)(x-8)+(x-1)(x-8)+(x-1)(x-4)$; at 4: $0+(3)(-4)+0=-12$
  • $C'=(x-2)(x-8)+(x-1)(x-8)+(x-1)(x-2)$; at 4: $(2)(-4)+(3)(-4)+(3)(2)=-8-12+6=-14$
  • $D'=(x-2)(x-4)+(x-1)(x-4)+(x-1)(x-2)$; at 4: $0+0+(3)(2)=6$

$$g''(4)=-8-12-14+6=-28$$

Contribution $=-\dfrac1{144}(-28)=\dfrac{28}{144}=\dfrac{7}{36}$

Acceleration: $$P''(4)=0+\tfrac23+0+\tfrac{7}{36}=\frac{24+7}{36}=\frac{31}{36}\approx 0.861$$

$$\boxed{\text{Acceleration}\approx 0.86\ \text{units/time}^2}$$


Final Results

  • Velocity at $x=4$: $\dfrac{17}{6}\approx 2.83$
  • Acceleration at $x=4$: $\dfrac{31}{36}\approx 0.86$
Open this answer on its own page →

2080 · 5 marks

How can we calculate derivatives of discrete (tabulated) functions? Write down its algorithm. [5]

When the function values are known only at some discrete points (tabulated data) but the function itself is unknown, we cannot differentiate it directly. In such cases, we use numerical differentiation for tabulated functions. The approach is to fit an interpolating polynomial through the known d...

Open this answer on its own page →

Simpson's 3/8 rule

2081 · 5 marks

What is an application of numerical integration? Find the value of the integral using Simpson's 3/8 rule with n=6. $$\int_{1}^{2} \frac{e^x}{x} dx$$ [5]

Show solved answer

Application of Numerical Integration and Simpson's 3/8 Rule

Application of Numerical Integration

Numerical integration computes the approximate value of a definite integral from numerical/tabular values of the integrand. Key application:

  • Evaluating definite integrals when no closed-form (analytical) antiderivative exists (such as $\int e^{x}/x,dx$).
  • Computing area/volume from experimental or tabular data where only discrete points are known.
  • Used in engineering and physics for work, probability, signal processing, etc.

Given Data

$$\int_1^2 \frac{e^x}{x},dx, \qquad a=1,\ b=2,\ n=6$$

Since $n=6$ is a multiple of 3, Simpson's 3/8 rule applies.


Step 1: Step size

$$h = \frac{b-a}{n} = \frac{2-1}{6} = \frac{1}{6} = 0.16667$$

Step 2: Nodes and function values $f(x)=\dfrac{e^x}{x}$

$i$$x_i$$e^{x_i}$$f(x_i)=e^{x_i}/x_i$
01.000002.718282.71828
11.166673.211092.75236
21.333333.793672.84525
31.500004.481692.98779
41.666675.294493.17670
51.833336.255143.41190
62.000007.389063.69453

Step 3: Simpson's 3/8 composite rule

$$I \approx \frac{3h}{8}\Big[f_0 + 3(f_1+f_2+f_4+f_5) + 2f_3 + f_6\Big]$$

Grouping:

  • $f_0 + f_6 = 2.71828 + 3.69453 = 6.41281$
  • $3(f_1+f_2+f_4+f_5) = 3(2.75236+2.84525+3.17670+3.41190) = 3(12.18621)=36.55863$
  • $2f_3 = 2(2.98779) = 5.97558$

Sum $= 6.41281 + 36.55863 + 5.97558 = 48.94702$

Step 4: Coefficient and result

$$\frac{3h}{8} = \frac{3\times 0.16667}{8} = \frac{0.50001}{8} = 0.0625$$

$$I \approx 0.0625 \times 48.94702 = 3.05919$$

$$\boxed{I \approx 3.0592}$$

The exact value is $\approx 3.0591$, so the approximation is accurate to 4 significant figures.

Open this answer on its own page →

Multi-Segment Trapezoidal rule

2080 · 5 marks

Find the following integral using composite trapezoidal rule for using 2 segments (k=2) and 4 segments (k=4). $$\int_{2}^{4} (x^3 + 2) dx$$ [5]

  • Integrand: $f(x) = x^3 + 2$ - Lower limit: $a = 2$ - Upper limit: $b = 4$ - Segment counts: $k = 2$ and $k = 4$ Formula: $$I \approx \frac{h}{2}\left[f(x0) + 2\sum{i=1}^{k-1}f(xi) + f(xk)\right], \quad h = \frac{b-a}{k}$$ $$\int2^4 (x^3+2),dx = \left[\frac{x^4}{4}+2x\right]2^4 = (64+8)-(4+4) =...
Open this answer on its own page →

Multi-Segment Simpson's 1/3 rule

2079 · 5 marks

Simpson's 1/3 Rule Integration

Estimate the integral value of the following function from $x = 1.2$ to $x = 2.4$ using Simpson's 1/3 rule.

$$\begin{array}{c|cccccccc} x & 1.0 & 1.2 & 1.4 & 1.6 & 1.8 & 2.0 & 2.2 & 2.4 & 2.6 \ \hline f(x) & 1.53 & 2.25 & 3.18 & 4.32 & 5.67 & 7.23 & 8.98 & 10.94 & 13.08 \end{array}$$

[5]

$x$ 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 -------------------------------------------------- $f(x)$ 1.53 2.25 3.18 4.32 5.67 7.23 8.98 10.94 13.08 Integration limits: $x = 1.2$ to $x = 2.4$ Step size: $h = 0.2$ $$h = 0.2, \quad n = \frac{2.4 - 1.2}{0.2} = 6 \quad (\text{even, so Simpson's 1/3 appli...

Open this answer on its own page →

Gaussian integration algorithm

2079 · 5 marks

What is Gaussian integration formula? Evaluate the following integration using Gaussian integration three ordinate formula. $$\int_{0}^{1} \frac{\sin x}{x} dx$$ [5]

  • Integral: $\int0^1 \frac{\sin x}{x},dx$ - Limits: $a=0$, $b=1$ - Method: Gauss-Legendre three-ordinate (n = 3) formula Three-point Gauss-Legendre nodes and weights: - $t1 = -\sqrt{3/5} = -0.7745967,\ w1 = 5/9$ - $t2 = 0,\ w2 = 8/9$ - $t3 = +\sqrt{3/5} = +0.7745967,\ w3 = 5/9$ Gauss-Legendre qu...
Open this answer on its own page →

Romberg integration

2078 · 5 marks

Evaluate the following integration using Romberg integration. $$\int_{0}^{1} \frac{\sin^2 x}{x} dx$$ [5]

  • Integrand: $f(x) = \dfrac{\sin^2 x}{x}$ - Limits: $a = 0$, $b = 1$ - Interval length: $b - a = 1$ Removable singularity at $x=0$: $\lim{x\to 0}\frac{\sin^2 x}{x} = \lim{x\to0}\frac{x^2}{x} = 0$, so $f(0)=0$. $x$ $\sin x$ $\sin^2 x$ $f(x)=\sin^2 x/x$ ------------ $0$ $0$ $0$ $0$ $0.25$
Open this answer on its own page →

2075 · 5 marks

Evaluate the following integration using Romberg integration. $$\int_{0}^{1} \frac{\sin x}{x} dx$$ [5]

  • Integrand: $f(x) = \dfrac{\sin x}{x}$ - Limits: $a = 0$, $b = 1$ - Special value at $x=0$: $\lim{x\to 0}\dfrac{\sin x}{x} = 1$, so $f(0)=1$ - Method: Romberg integration - Interval width: $b-a = 1$ I compute the required function values (arguments in radians): $x$ $\sin x$ $f(x)=\sin x/x$ -----...
Open this answer on its own page →

Differentiating Continuous Functions

2077 · 5 marks

Write down algorithm and program for the differentiating continuous function using three point formula. [5]

Differentiating continuous functions is the process of approximating the derivative f'(x) of a function f(x) when the function itself is available. The Three-Point (Central Difference) Formula is: $$f'(xp) = \frac{f(xp + h) - f(xp - h)}{2h}$$ This formula uses one point ahead and one point behind...

Open this answer on its own page →

Simpson's 1/3 rule

2077 · 5 marks

How Simpson's 1/3 rule differs from Trapezoidal rule? Drive the formula for Simpson's 1/3 rule. [5]

Aspect Trapezoidal Rule Simpson's 1/3 Rule --------- Approximation Approximates the integrand using straight lines (linear polynomials) between points Approximates the integrand using parabolas (quadratic polynomials) through three points Accuracy Less accurate; error is of order O(h²) More accur...

Open this answer on its own page →

2075 · 5 marks

Calculate the integral value of the function given below from $x = 1.8$ to $x = 3.4$ using Simpson's 1/3 rule.

$$\begin{array}{c|cccccccc} x & 1.8 & 2.0 & 2.2 & 2.4 & 2.6 & 2.8 & 3.0 & 3.4 \ \hline f(x) & 0.003 & 0.778 & 1.632 & 2.566 & 3.579 & 4.672 & 7.097 & 8.429 \end{array}$$

[5]

X 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.4 -------------------------------------------------------------- F(x) 0.003 0.778 1.632 2.566 3.579 4.672 7.097 8.429 Limits: $x = 1.8$ to $x = 3.4$ Spacing check: - 1.8 to 3.0: step $h = 0.2$ (equally spaced, 6 intervals) - 3.0 to 3.4: step $h = 0.4$ (unequal, sin...

Open this answer on its own page →