Computing first, second and fourth order approximations for first order derivative a given value of dx
%Computing the first, second and fourth order approximations for a given value of dx.
clear all
close all
clc
%analytical function =sin(x)/x^3
%analytical derivative
% f'(x)=(x^3(cos(x))-sin(x)*3*x^2)/x^6 ;
x= pi/3 ;
analytical_derivative=(x^3*(cos(x))-sin(x)*3*x^2)/x^6 ;
% numerical derivative
% forward differencing= (f(x+dx)-f(x))/dx
dx=linspace(pi/4,pi/4000,20);
for i = 1:length(dx)
dx(i)
%Invoking function first_order_approximations(x,dx)
first_order_error(i) = first_order_approximations(x,dx(i));
%Invoking function second_order_approximations(x,dx)
second_order_error(i)= second_order_approximations(x,dx(i));
%Invoking function fourth_order_approximations(x,dx)
fourth_order_error(i)= fourth_order_approximations(x,dx(i));
end
loglog(dx,first_order_error,'color','b')
hold on;
loglog(dx, second_order_error,'color','g')
hold on;
loglog(dx,fourth_order_error,'color','r')
hold off;
xlabel('dx = grid spacing');
ylabel('errors');
h = legend ("firstordererror", "secondordererror","fourthordererror");
legend (h, "location", "northeastoutside");
%First order approximation
%forward_differencing =((sin(x+dx)/(x+dx)^3)-(sin(x)/(x)^3))/dx
%second order approximation
%central_differencing =((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx)
%Fourth order approximation
%Fourth_differencning =(((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(Fourth_differencning - analytical_derivative)
%error_second_order = abs(central_differencing - analytical_derivative)
%error_first_order = abs(forward_differencing - analytical_derivative)
Invoking function first_order_approximations(x,dx) inside the above/main script:
function out = first_order_approximations(x,dx)
analytical_derivative=(x^3*(cos(x))-sin(x)*3*x^2)/x^6 ;
forward_differencing =((sin(x+dx)/(x+dx)^3)-(sin(x)/(x)^3))/dx
%central_differencing =((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx)
%Fourth_differencning =(((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)
out = abs(forward_differencing - analytical_derivative)
%out = abs(central_differencing - analytical_derivative)
%out = abs(Fourth_differencning - analytical_derivative)
endfunction
Invoking function second_order_approximations(x,dx) inside the above/main script:
function secondorder = second_order_approximations(x,dx)
analytical_derivative=(x^3*(cos(x))-sin(x)*3*x^2)/x^6 ;
%forward_differencing =((sin(x+dx)/(x+dx)^3)-(sin(x)/(x)^3))/dx
central_differencing =((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx)
%Fourth_differencning =(((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)
%out = abs(forward_differencing - analytical_derivative)
secondorder = abs(central_differencing - analytical_derivative)
%out = abs(Fourth_differencning - analytical_derivative)
endfunction
Invoking function fourth_order_approximations(x,dx) inside the above/main script:
function fourthorder = fourth_order_approximations(x,dx)
analytical_derivative=(x^3*(cos(x))-sin(x)*3*x^2)/x^6 ;
%forward_differencing =((sin(x+dx)/(x+dx)^3)-(sin(x)/(x)^3))/dx
%central_differencing =((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx)
Fourth_differencing =(((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)
%out = abs(forward_differencing - analytical_derivative)
fourthorder = abs(Fourth_differencing - analytical_derivative)
%out = abs(Fourth_differencning - analytical_derivative)
endfunction
PLOT:
EXPLANATION:
In this problem,first,second/central,fourth order approximation of the first order derivative of the given function has been calculated for a range of'dx' values.
Initialy using linspace command ,dx value is discretised into set of 20 values which lies within a range of pi/4 to pi/4000.
Now this set of dx values are accessed with the help of for loop and pass into set of 3 function which are first_order_approximations(x,dx(i)) , second_order_approximations(x,dx(i)) and fourth_order_approximations(x,dx(i)) repectively as shown in the above scripts.these function will generate error terms for the set of 20 'dx' values.
Later error terms for the first,second and fourth order approximation will be plotted and depicted with the help of legends as shown in the snapshot attached.
OBSERVATIONs:
Error terms will decrease with the decrease in dx values.Fourth order error terms will have least value followed by second and fouth order error.But there is a sudden increase in error values for fouth order approximation which can be seen be plot that lies between dx value of `10^0`to `10^-1`.