Comparing numerical and analytical differentiation.
Consider the function f(x) = x³. Calculate its first derivative at point x = 3 numerically with the forward, backward, and central finite difference formulas and using:
(a) Points x = 2, x = 3, and x = 4.
(b) Points x = 2.75, x = 3, and x = 3.25.
Compare the results with the exact (analytical) derivative.
Analytical differentiation: The derivative of the function is f ‘(x)=3x², and the value of the derivative at x = 3 is f ‘(3) = 3 · 3² = 27.
Numerical differentiation
(a) The points used for numerical differentiation are:
x: 2 3 4
f(x): 8 27 64
Using Eqs. (8.5) through (8.7),
\left.\frac{d f}{d x}\right|_{x=x_i}=\frac{f\left(x_{i+1}\right)-f\left(x_i\right)}{x_{i+1}-x_i} (8.5)
\left.\frac{d f}{d x}\right|_{x=x_i}=\frac{f\left(x_{i+1}\right)-f\left(x_{i-1}\right)}{x_{i+1}-x_{i-1}} (8.7)
the derivatives using the forward, backward, and central finite difference formulas are:
Forward finite difference:
\left.\frac{d f}{d x}\right|_{x=3}=\frac{f(4)-f(3)}{4-3}=\frac{64-27}{1}=37 \quad \text { error }=\left|\frac{37-27}{27} \cdot 100\right|=37.04 \%
Backward finite difference:
\left.\frac{d f}{d x}\right|_{x=3}=\frac{f(3)-f(2)}{3-2}=\frac{27-8}{1}=19 \quad \text { error }=\left|\frac{19-27}{27} \cdot 100\right|=29.63 \%
Central finite difference:
\left.\frac{d f}{d x}\right|_{x=3}=\frac{f(4)-f(2)}{4-2}=\frac{64-8}{2}=28 \quad \text { error }=\left|\frac{28-27}{27} \cdot 100\right|=3.704 \%
(b) The points used for numerical differentiation are:
x: 2.75 3 3.25
f(x): 2.75³ 3³ 3.25³
Using Eqs. (8.5) through (8.7), the derivatives using the forward, backward, and central finite difference formulas are:
Forward finite difference:
\left.\frac{d f}{d x}\right|_{x=3}=\frac{f(3.25)-f(3)}{3.25-3}=\frac{3.25^{3}-27}{0.25}=29.3125 \quad \text { error }=\left|\frac{29.3125-27}{27}\right| \cdot 100=8.565 \%
Backward finite difference:
\left.\frac{d f}{d x}\right|_{x=3}=\frac{f(3)-f(2.75)}{3-2.75}=\frac{27-2.75^{3}}{0.25}=24.8125 \quad \text { error }=\left|\frac{24.8125-27}{27}\right| \cdot 100=8.102 \%
Central finite difference:
\left.\frac{d f}{d x}\right|_{x=3}=\frac{f(3.25)-f(2.75)}{3.25-2.75}=\frac{3.25^{3}-2.75^{3}}{0.5}=27.0625 \quad \text { error }=\left|\frac{27.0625-27}{27}\right| \cdot 100=0.2315 \%
The results show that the central finite difference formula gives a more accurate approximation. This will be discussed further in the next section. In addition, smaller separation between the points gives a significantly more accurate approximation.