effect of dx on first , second and fourth order error
The code to plot the first order ,second order ,fourth order error for a range of dx and also to check the slope of each curve is as given below:-
clear all
close all
clc
x = pi/3;
dx = linspace(pi/4,pi/400000,200);
for i= 1:length(dx)
first_order_error(i) = first_order_approximations(x,dx(i));
second_order_error(i)= second_order_approximations(x,dx(i));
fourth_order_error(i)=fourth_order_approximations(x,dx(i));
end
figure(1)
loglog(dx,first_order_error,'r')
hold on
loglog(dx,second_order_error,'b')
hold on
loglog(dx,fourth_order_error,'g')
hold off
legend('first order error','second order error','fourth order error')
%slope of first order error
y1 = mean(diff(first_order_error)./diff(dx))
%slope of second order error
y2 = mean(diff(second_order_error)./diff(dx))
%slope of fourth order error
y3 = mean(diff(fourth_order_error)./diff(dx))
The program for defining the first order approximation is written below:-
function out = first_order_approximations(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
first_order_approximation = (sin_x(x+dx)- (sin_x(x)))./dx;
out = abs(first_order_approximation - analytical_derivative);
end
The program for the defining the second order approximations:-
function out = second_order_approximations(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 = (sin_x(x+dx)-sin_x(x-dx))./(2.*dx);
out = abs(second_order_approximation - analytical_derivative);
end
The program for defining the fourth order approximations:-
function out = fourth_order_approximations(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
fourth_order_approximation = (sin_x(x-(2.*dx))-8.*sin_x(x-dx)+8.*sin_x(x+dx)-sin_x(x+(2.*dx)))./(12.*dx);
out = abs(fourth_order_approximation - analytical_derivative);
end
The program for defining the function for which the first,second and fourth order approximation are calculated:-
function out = sin_x(x)
out = sin(x)./x.^3;
end
Output:-
As a result the above code the outputs were plooted on the curves for first, second and fourth order error. plot is below:-
The above graph shows different error values for a wide range of dx. From the graph it is clear that higher the order of approximation lesser the error. Here the fourth order shows the minimum error and maximum error is shown by the first order for a wide range of dx.It means that higher the order of approximation higher the accuracy. But at very high values of dx between 10^-1 to 10^0 a peak is observed which symbolizes the higher error.
The results of slope calculation :-
y1 = 1.2282
y2= 9.3683
y3 = 12.7564
Here y1,y2,y3, shows the slope of first, second, fourth order error curve respectively. It is observed that the value of y2,y3 is much higher than the value of y1,which means that the error drops rapidely for second and fourth order when compared to the first order.