Comparing the first, second, and fourth-order approximations of the first order derivative against the analytical or exact derivative using MATLAB
OBJECTIVE:-
Write a program in MATLAB that compares the first, second, and fourth-order approximations of the first derivative against the analytical or exact derivative. For a function, `f(x)=sinx/x^3`; we’ll compute the first derivative at `x=pi/3`; both numerically and analytically.
Define an error parameter as the absolute difference in numerical and exact derivative at `x=pi/3.` Also, plot a bar graph that shows the error and provide appropriate labels and legends on your plot.
PROBLEM FORMULATION & METHODOLOGY:-
We are required to compute the first, second, and fourth-order approximations for the first-order derivative.
First-order approximation:-
Here we can use both ‘forward differencing approximation’ as well as ‘backward differencing approximation’. In a first-order approximation, we’re going to use both the mentioned methods.
1. Forward differencing method:`(df(x))/dx=(f(x+a)-f(x))/(∆x)`
Where`∆x` denotes the spacing b/w two nodes and `f(x)` denotes the function.
2. Backward differencing method:`(df(x))/dx=(f(x)-f(x-a))/(∆x)`
Where, `∆x` denotes the spacing b/w two nodes and `f(x)` denotes the function.
Second-order approximation:-
For second-order approximation, we’ll use the ‘central differencing method’.
`(df(x))/dx=(f(x+a)-f(x-a))/(2∆x)`
Where, `∆x` denotes the spacing b/w two nodes and `f(x)` denotes the function.
Fourth-order approximation:-
For fourth-order approximation, we’ll use the ‘central differencing method’.
`(df(x))/dx=(f(x-2∆x)-8f(x-∆x)+8f(x+∆x)-f(x+2∆x))/(12∆x)`
Where, `∆x` denotes the spacing b/w two nodes and `f(x)` denotes the function.
The process followed in developing the program:-
- First of all, we are going to write programs to make functions for first, second, and fourth-order approximation at a particular point (x, dx). These functions will be useful for calculating errors over the range of ‘dx’. So, by using code ‘function out’ we can make function which if we save in directory will be used by other programs (of same working directory) whenever necessary.
- After creating above functions for first, second and fourth-order error at point (x, dx) we have to save them in the same directory so that these functions can be used in the program for calculating first, second and fourth-order errors over the range of ‘dx’
- So, we are going to create an array for the range of ‘dx’. Then by using ‘for loop’ over the range of ‘dx’ and using the functions first, second and fourth-order error at a given point (that we have already created and saved in the same directory), we can get the errors for a given range of ‘dx’. Then we can able to plot how the error changes as a function of ‘dx’ for first, second, and fourth-order approximations.
Creating functions for different approximation methods
Forward differencing 1st order approximation:-
function error_forward_differencing = first_order_approx(x, dx)
% analytical function = sin(x)/x^3;
% analytical derivative
% f'(x) = (x^3*(cos(x))-sin(x)*3*x^2)/x^6;
analytical_derivative=(x^3*(cos(x))-sin(x)*3*x^2)/x^6;
%numerical derivative;
%forward differencing;
%forward differencing=(f(x+dx)-f(x))/dx
%first order approximation;
forward_differencing=((sin(x+dx)/(x+dx)^3)-(sin(x)/(x)^3))/dx;
%absolute error= numerical derivative-analytical derivative;
error_forward_differencing=abs(forward_differencing-analytical_derivative);
end
Backward differencing 1st order approximation:-
function error_backward_differencing = first_order_approx_backward_differencing(x, dx)
% analytical function, f(x) = sin(x)/x^3;
% analytical derivative
% f'(x) = (x^3*(cos(x))-sin(x)*3*x^2)/x^6;
analytical_derivative=(x^3*(cos(x))-sin(x)*3*x^2)/x^6;
%numerical derivative;
%first order approximation;
%backward differencing =(f(x)-f(x-dx))/dx;
%first order approximation;
backward_differencing=((sin(x)/(x)^3)-(sin(x-dx)/(x-dx)^3))/dx;
%absolute error= numerical derivative-analytical derivative;
error_backward_differencing=abs(backward_differencing-analytical_derivative);
end
Central differencing 2nd order approximation:-
function error_central_differencing = second_order_approx(x, dx)
% analytical function = sin(x)/x^3;
% analytical derivative
% f'(x) = (x^3*(cos(x))-sin(x)*3*x^2)/x^6;
analytical_derivative=(x^3*(cos(x))-sin(x)*3*x^2)/x^6;
%numerical derivative;
%second order approximation;
%central differencing=(f(x+dx)-f(x-dx))/2dx
%second order approximation;
central_differencing=((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx);
%absolute error= numerical derivative-analytical derivative;
error_central_differencing=abs(central_differencing-analytical_derivative);
end
Central differencing 4th order approximation:-
function error_fourth_order_approx=fourth_order_approx_central_differencing(x,dx)
% analytical function, f(x) = sin(x)/x^3;
% analytical derivative
% f'(x) = (x^3*(cos(x))-sin(x)*3*x^2)/x^6;
analytical_derivative=(x^3*(cos(x))-sin(x)*3*x^2)/x^6;
%numerical derivative;
%central differencing fourth order approximation;
%central differencing fourth order approximation=(f(x-2dx)-8f(x-dx)+8f(x+dx)+f(x+2dx))/dx
%fourth order approximation;
central_differencing_4th_order=((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);
%absolute error= numerical derivative-analytical derivative;
error_fourth_order_approx=abs(central_differencing_4th_order-analytical_derivative);
end
Program for calculating first, second and fourth-order error using previously developed function:-
close all
clear all
clc
%variable inputs
x=pi/3;
dx=linspace(pi/40,pi/40000,40);
%givenfunction f(x)=sinx/x^3;
%analytical or exact derivative, f'(x)=(((x^3)*cosx)-((3*x^2)*sinx))/x^6
analytical_derivative=(((x^3)*cos(x))-((3*x^2)*sin(x)))/x^6;
%creating for loop;
for i=1:length(dx)
forward_differencing_1st_order_error(i)=first_order_approx(x, dx(i));
backward_differencing_1st_order_error(i)=first_order_approx_backward_differencing(x, dx(i));
central_differencing_error_2nd_order_error(i)=second_order_approx(x, dx(i));
central_differencing_error_4th_order_error(i)=fourth_order_approx_central_differencing(x,dx(i));
end
% creating plots
figure(1)
subplot(2,2,1)
plot(dx,forward_differencing_1st_order_error,'linewidth',2)
xlabel('dx')
ylabel('error')
grid on
title('Forward Differencing')
subplot(2,2,2)
plot(dx,backward_differencing_1st_order_error,'r','linewidth',2)
xlabel('dx')
ylabel('error')
grid on
title('Backward Differencing')
subplot(2,2,3)
plot(dx,central_differencing_error_2nd_order_error,'g','linewidth',2)
xlabel('dx')
ylabel('error')
grid on
title('Central Difference of 2nd Order Error')
subplot(2,2,4)
plot(dx,central_differencing_error_4th_order_error,'m','linewidth',2)
xlabel('dx')
ylabel('error')
grid on
title ('Central Difference of 4th Order Error')
figure(2)
loglog(dx,forward_differencing_1st_order_error,'linewidth',2)
hold on
grid on
loglog(dx,backward_differencing_1st_order_error,'r','linewidth',2)
hold on
loglog(dx,central_differencing_error_2nd_order_error,'g','linewidth',2)
hold on
loglog(dx,central_differencing_error_4th_order_error,'m','linewidth',2)
xlabel('dx')
ylabel('error')
legend('Forward differencing 1st order error','Backward differencing 1st order error','Central differencing 2nd order error','Central differencing 4th order error')
figure('Name','Variation of Absolute Error w.r.t. dx')
subplot(2,2,1)
bar(dx,forward_differencing_1st_order_error);
title('Forward Difference')
xlabel('dx')
ylabel('error')
subplot(2,2,2)
bar(dx,backward_differencing_1st_order_error)
title('Backward Difference')
xlabel('dx')
ylabel('error')
subplot(2,2,3)
bar(dx,central_differencing_error_2nd_order_error)
title('Central Difference of 2nd Order Error')
xlabel('dx')
ylabel('error')
subplot(2,2,4)
bar(dx,central_differencing_error_4th_order_error)
title('Central Difference of 4th Order Error')
xlabel('dx')
ylabel('error')
RESULTS:-
CONCLUSION:-
From the above graphs, we can observe that, for small values of ‘dx’; the error for the first-order approximation (forward and backward differencing) is significantly greater. While for the same values of ‘dx’ second-order approximation are showing less error as compared to first-order and the least error can be observed in fourth-order approximation. Further, by observing the slope for the curves, we can conclude that for a small change in ‘dx’; error values are rapidly increases/decreases for fourth-order error as compared to first and second order. It can be seen that as the order of approximation increases the accuracy also increases and the error between the numerical and analytical derivative changes significantly.