Discretization Basics (Part 2)
This project computes the first order numerical derivative of a function according to the 1st order, 2nd order and 4th order approximation methods. This numerical derivative is compared with the exact derivative of the function and magnitude of error is obtained.
The first order derivative of function according to first order approximation is given as:
This uses a forward difference scheme. Meaning the derivative is calculated using the information obtained from the point ahead (i+1) of the initial point (i). It is called first order approximation because the error terms are in order of 1.
The first order derivative of function according to second order approximation is given as:
This uses a central difference scheme. Meaning the derivative is calculated using the information obtained from the point ahead (i+1) and the point behind (i-1) of the initial point (i). It is called second order approximation because the error terms are in order of 2.
The first order derivative of function according to fourth order approximation is given as:
This also uses a central difference scheme. Meaning the derivative is calculated using the information obtained from the point ahead (i+2,i+1) and the point behind (i-2,i-1) of the initial point (i). It is called second order approximation because the error terms are in order of 4.
In this project, the function is-
The exact derivative of function is-
The MATLAB program to calculate the error is given below-
discretisation_prac_main.m
clc
clear all
close all
%Function is f(x)=sin(x)/x^3
x=pi/3;
dx=linspace(pi/20,pi/400,20);
for i=1:length(dx)
[error_first_order(i),error_second_order(i),error_fourth_order(i)]=discretisation_prac(x,dx(i));
end
a=zeros([20 3]);
for j=1:length(dx)
a(j,1)=error_first_order(j);
a(j,2)=error_second_order(j);
a(j,3)=error_fourth_order(j);
end
for k=1:length(dx)
b{k}=a(k,:);
end
figure(1)
loglog(dx,error_first_order,'r-')
hold on
loglog(dx,error_second_order,'b-')
hold on
loglog(dx,error_fourth_order,'g-')
xlabel('log(dx)')
ylabel('log(error)')
legend('First order approximation','Second order approximation','Fourth order approximation','Location','best')
title('Effect of step-size on the error in 1st order derivative (on a log-log scale)')
figure(2)
plot(dx,error_first_order,'r-')
hold on
plot(dx,error_second_order,'b-')
hold on
plot(dx,error_fourth_order,'g-')
xlabel('dx')
ylabel('error')
legend('First order approximation','Second order approximation','Fourth order approximation','Location','best')
title('Effect of step-size on the error in 1st order derivative')
figure(3)
bar(dx,error_first_order)
xlabel('dx')
ylabel('error')
title('Effect of step-size on the error in 1st order derivative for 1st order approximation')
figure(4)
bar(dx,error_second_order)
xlabel('dx')
ylabel('error')
title('Effect of step-size on the error in 1st order derivative for 2nd order approximation')
figure(5)
bar(dx,error_fourth_order)
xlabel('dx')
ylabel('error')
title('Effect of step-size on the error in 1st order derivative for 4th order approximation')
discretisation_prac.m
function [error_first_order,error_second_order,error_fourth_order]=discretisation_prac(x,dx)
%f(x)=sin(x)/x^3
%Analytical derivative
analytical_derivative=((x*cos(x))-(3*sin(x)))/x^4;
%Numerical_method_derivative
%first order forward differencing for 1st derivative
first_order=(((sin(x+dx))/((x+dx)^3))-((sin(x))/(x^3)))/dx;
%second order central differencing for 1st derivative
second_order=(((sin(x+dx))/((x+dx)^3))-((sin(x-dx))/((x-dx)^3)))/(2*dx);
%fourth order central differencing for 1st derivative
fourth_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);
error_first_order=abs(first_order-analytical_derivative);
error_second_order=abs(second_order-analytical_derivative);
error_fourth_order=abs(fourth_order-analytical_derivative);
end
CONCLUSION:
(1) As seen from the graphs, the magnitude of error is the highest for first order approximation, whereas it is the lowest for fourth order approximation for a given step-size.
(2) In fourth order approximation, the rate at which error decreases is high, as observed in log-log graph. This is because the error is directely proportional to the order of 4.
(3) The slope of the curves in log-log plot denotes the rate of change of error. It is lowest for 1st order approximation and highest for 4th order approximation. Thus by increasing the order of the error, more accurate results can be obtained.
(4) As seen from the bar graphs,when the step-size is very low, error is very less for fourth order approximation than first or second order approximations.