5 Numerical Differentiation

Numerical Methods · Unit 5

Numerical Differentiation

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

Numerical Differentiation in short

Many functions are known only as tables of values, or are too hard to differentiate by hand. Numerical differentiation estimates the derivative from function values at nearby points.

Numerical Differentiation: notes

Derivatives of continuous functions

The derivative is defined by a limit.

$$f'(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}$$

A computer cannot take $h \to 0$ exactly. So we choose a small, fixed step $h$ and drop the limit. The result is an approximation. Taylor series tell us how large the error is.

$$f(x+h) = f(x) + hf'(x) + \frac{h^2}{2}f''(x) + \frac{h^3}{6}f'''(x) + \cdots$$

To calculate the derivative of a continuous function we can do one of three things.

  • Evaluate the function at nearby points and use a finite difference formula.
  • Fit an interpolating polynomial through the points and differentiate the polynomial.
  • Use a divided difference table when the points are unequally spaced.

Two point forward difference formula

Derivation. Start from the Taylor series of $f(x+h)$ and solve for $f'(x)$.

$$\begin{aligned} f(x+h) &= f(x) + hf'(x) + \frac{h^2}{2}f''(\xi) \ hf'(x) &= f(x+h) - f(x) - \frac{h^2}{2}f''(\xi) \ f'(x) &= \frac{f(x+h) - f(x)}{h} - \frac{h}{2}f''(\xi) \end{aligned}$$

Dropping the last term gives the forward difference formula.

$$f'(x_i) \approx \frac{f(x_{i+1}) - f(x_i)}{h}$$

The truncation error is of order $h$, written $O(h)$.

What this unit covers

  • Derivatives of continuous functions
  • Two point forward difference formula
  • Two point backward difference formula
  • Divided difference table for derivatives
  • First and second order derivatives
  • Algorithm and program for numerical differentiation

Two point forward difference formula

2082 · 5 marks

Derive the formula for two points forward difference. Derive the formula for two points backward difference. [2.5+2.5]

Note: The reference notes were not available for this topic. The derivation below follows the standard numerical methods approach as taught in BSc CSIT curriculum. --- (a) Two-Point Forward Difference Formula The forward difference formula approximates the derivative of a function $f(x)$ at a poi...

Open this answer on its own page →

Divided difference table for derivatives

2082 · 5 marks

Find the first and second derivative at $x = 2.5$ of the following data points.

$$\begin{array}{|c|cccccc|}\hline x & 1.5 & 2 & 2.5 & 3 & 3.5 & 4 \ \hline f(x) & 2.375 & 4.5 & 7.625 & 12 & 17.875 & 23 \ \hline \end{array}$$

[5]

Show solved answer

Numerical Differentiation at x = 2.5

Given

$x$1.52.02.53.03.54.0
$f(x)$2.3754.57.6251217.87523
  • Step size: $h = 0.5$
  • Point of interest: $x = 2.5$

Solution

Forward Difference Table

Take $x_0 = 2.5$ so that $p = 0$. First compute all differences carefully.

$x$$f$$\Delta f$$\Delta^2 f$$\Delta^3 f$$\Delta^4 f$$\Delta^5 f$
1.52.3752.1251.00.250-2.5
2.04.53.1251.250.25-2.5
2.57.6254.3751.5-2.25
3.012.05.875-0.75
3.517.8755.125
4.023.0

Checks:

  • $\Delta f$: $2.125, 3.125, 4.375, 5.875, 5.125$
  • $\Delta^2 f$: $1.0, 1.25, 1.5, -0.75$
  • $\Delta^3 f$: $0.25, 0.25, -2.25$
  • $\Delta^4 f$: $0, -2.5$
  • $\Delta^5 f$: $-2.5$

Note: the data comes from $f(x)=x^3+\tfrac{1}{2}x^2-1$ up to $x=3.5$ but the last point $f(4)=23$ breaks the cubic (true cubic value would be $71$). This is why higher differences do not vanish. We proceed with the tabulated data as given.

At $x_0 = 2.5$ (the row starting there): $$\Delta f_0 = 4.375,\ \Delta^2 f_0 = 1.5,\ \Delta^3 f_0 = -2.25,\ \Delta^4 f_0 = -2.5$$

First Derivative (Newton's forward, $p=0$)

$$f'(x_0) = \frac{1}{h}\left[\Delta f_0 - \frac{1}{2}\Delta^2 f_0 + \frac{1}{3}\Delta^3 f_0 - \frac{1}{4}\Delta^4 f_0 + \cdots\right]$$

$$= \frac{1}{0.5}\left[4.375 - \frac{1}{2}(1.5) + \frac{1}{3}(-2.25) - \frac{1}{4}(-2.5)\right]$$

$$= 2\left[4.375 - 0.75 - 0.75 + 0.625\right] = 2(3.5) = 7.0$$

Second Derivative (Newton's forward, $p=0$)

$$f''(x_0) = \frac{1}{h^2}\left[\Delta^2 f_0 - \Delta^3 f_0 + \frac{11}{12}\Delta^4 f_0 - \cdots\right]$$

$$= \frac{1}{(0.5)^2}\left[1.5 - (-2.25) + \frac{11}{12}(-2.5)\right]$$

$$= \frac{1}{0.25}\left[1.5 + 2.25 - 2.2917\right]$$

$$= 4 \times 1.4583 = 5.833$$

Summary

DerivativeValue
$f'(2.5)$$7.0$
$f''(2.5)$$\approx 5.833$

(Interpretation: had the last data point been consistent with a cubic, the $f'=x^2$-related exact values would give $f'(2.5)=21.25$ and $f''(2.5)=15.5$, but working strictly from the tabulated data with truncation at $\Delta^4$ yields the values above.)

Open this answer on its own page →

2078 · 5 marks

The table below gives the values of distance travelled by a car at various time intervals during the initial running. Estimate the velocity and acceleration at time t = 7 sec.

$$\begin{array}{|c|c|c|c|c|c|}\hline \text{Time (t sec)} & 5 & 6 & 7 & 8 & 9 \ \hline \text{Distance (s m)} & 10.0 & 14.5 & 19.5 & 25.5 & 32.0 \ \hline \end{array}$$

[5]

Time $t$ (sec) 5 6 7 8 9 ------------------ Distance $s$ 10.0 14.5 19.5 25.5 32.0 - Uniform spacing $h = 1$ - Required: velocity $\frac{ds}{dt}$ and acceleration $\frac{d^2s}{dt^2}$ at $t = 7$ (The problem header mislabels the second row as "Temp"; from context these are distance values, and the ...

Open this answer on its own page →

Algorithm and program for numerical differentiation

0 · 5 marks

How can we calculate derivatives of continuous functions? Write down algorithm and program for differentiating continuous function using two point forward difference formula. [5]

For a continuous function f(x), the derivative at a point x is defined as: $$f'(x) = \lim{h \to 0} \frac{f(x+h) - f(x)}{h}$$ In numerical methods, we cannot take the limit analytically for arbitrary functions, so we approximate the derivative by choosing a small but finite step size h: $$f'(x) \a...

Open this answer on its own page →