Comparing numerical and analytical differentiation.
Consider the function f(x)=\frac{2^{x}}{x}. Calculate the second derivative at x = 2 numerically with the three-point central difference formula using:
(a) Points x = 1.8, x = 2, and x = 2.2.
(b) Points x = 1.9, x = 2, and x = 2.1.
Compare the results with the exact (analytical) derivative.
Analytical differentiation: The second derivative of the function f(x)=\frac{2^{x}}{x} is:
f^{\prime \prime}(x)=\frac{2^{x}[\ln (2)]^{2}}{x}-\frac{2 \cdot 2^{x} \ln (2)}{x^{2}}+\frac{2 \cdot 2^{x}}{x^{3}}
and the value of the derivative at x = 2 is f ”(2) = 0.574617.
Numerical differentiation
(a) The numerical differentiation is done by substituting the values of the points x = 1.8, x = 2, and x = 2.2 in Eq. (8.29).
f^{\prime \prime}\left(x_i\right)=\frac{f\left(x_{i-1}\right)-2 f\left(x_i\right)+f\left(x_{i+1}\right)}{h^2}+O\left(h^2\right) (8.29)
The operations are done with MATLAB, in the Command Window:
>> xa = [1.8 2 2.2];
>> ya= 2. ^xa. /xa;
>> df = (ya(1) - 2*ya(2) + ya(3))/0.2^2
df =
0.57748177389232
(b) The numerical differentiation is done by substituting the values of the points x = 1.9, x = 2, and x = 2.1 in Eq. (8.29). The operations are done with MATLAB, in the Command Window:
>> xb = [1.9 2 2.1];
>> yb = 2. ^xb. /xb;
>> dfb = (yb(1) - 2*yb(2) + yb(3))/0.1^2
dfb =
0.57532441566441
Error in part (a): \quad error =\frac{0.577482-0.574617}{0.574617} \cdot 100=0.4986 \%
Error in part (b): \quad error =\frac{0.575324-0.574617}{0.574617} \cdot 100=0.1230 \%
The results show that the three-point central difference formula gives a quite accurate approximation for the value of the second derivative.