MATLAB program to compute the numbercal derivative of a function using first, second and fourth order approximations and compare it with the analytical derivative.
Aim: To write a program in MATLAB that compares the first, second and fourth order approximations of the first derivative against the analytical or the exact derivative.
Objectives:
- To write a program in MATLAB that computes the derivative of fx if fx=sin x ∕x3.
- The first derivative is to be computed at x = pi for dx = pi/30.
- The derivative is to be calculated both numerically and analytically.
- The numerical computation is to be done in first, second and fourth order approximations.
- An error parameter as the absolute difference in numerical and analytical derivative at x = pi/3 is to be computed.
- A bar graph that shows how the various errors compare with each other is to be plotted.
The logic behind the program: The given function whose derivative needs to be computed is as follows:
`fx=sin x ∕x3`
The analytical derivative or the exact derivative is given by:
`(df)/dx=(x^3cos x -3x^2sin x)/ (x^6)`
We are taking the value of x as x = pi/3 and computing the derivative for the same. The result we get in from the analytical derivative will be used as the benchmark to be compared with the results we will get from computing the derivative from the first order, second order and the fourth order approximations and the error will be the absolute difference between the analytical derivative and the numerical derivative.
The numerical derivative according to the first order approximation is given as:
`(df)/dx=(f_(i+1)-f_i)/(Δx)`
The numerical derivative according to the second order approximation is given as:
`(df)/dx=(f_(i+1)-f_(i-1))/(2Δx)`
The numerical derivative according to the third order approximation is given as:
`(df)/dx=(f_(i-2)-8f_(i-1)+8f_(i+1)-f_(i+2))/(12Δx)`
The errors is calculated as the absolute difference between the numerical and analytical derivatives which is written as follows:
Error = |numerical derivative - analytical derivative|
The MATLAB code to do the above computations is as follows:
%MATLAB program to compare numerical and analytical derivatives
clear all
close all
clc
%Initialising the values of x and dx
x = pi/3;
dx = pi/30;
%Computing the analytical derivative
analytic_derivative = (x^3*cos(x) - sin(x)*3*x^2)/(x^6);
%Computing the 1st order derivative and its error
forward_differencing = ((sin(x+dx)/(x+dx)^3) - (sin(x)/x^3))/dx;
error_forward_differencing = abs(analytic_derivative - forward_differencing);
%Computing the 2nd order derivative and its error
central_differencing = ((sin(x+dx)/(x+dx)^3) - (sin(x-dx)/(x-dx)^3))/(2*dx);
error_central_differencing = abs(analytic_derivative - central_differencing);
%Computing the 4th order derivative and its error
fourth_order_derivative = (((sin(x-(2*dx)))/((x-(2*dx))^3))-((8*sin(x-dx))/((x-dx)^3))+((8*sin(x+dx))/((x+dx)^3))-((sin(x+(2*dx)))/((x+(2*dx))^3)))/(12*dx);
error_fourth_order = abs(analytic_derivative - fourth_order_derivative);
%Printing the value of the analytical derivative
fprintf('%s %f nn','The analytical derivative = ',analytic_derivative)
%Printing the value of the 1st order derivative and its error
fprintf('%s %f n','The 1st order derivative = ',forward_differencing)
fprintf('%s %f nn','The 1st order error = ',error_forward_differencing)
%Printing the value of the 1st order derivative and its error.
fprintf('%s %f n','The 2nd order derivative = ',central_differencing)
fprintf('%s %f nn','The 2nd order error = ',error_central_differencing)
%Printing the values of the 4th order derivative and its error.
fprintf('%s %f n','The 4th order derivative = ',fourth_order_derivative)
fprintf('%s %f nn','The 4th order error = ',error_fourth_order)
%Plotting a bar graph for all the three errors to visualize comparison.
bar([1 2 3],[error_forward_differencing error_central_differencing error_fourth_order]);
bar_name = {'1st order approximation'; '2nd order approximation'; '4th order approximation'};
set(gca, 'xticklabel',bar_name);
xlabel('Orders of approximations');
ylabel('Error value');
The results of the above program are as follows:


Conclusion: We have successfully computed the analytical derivative and the first order derivative using the 1st order, 2nd order and the 4th order approximations for the given function and also calculated and compared their respective errors.