Error in second order derivative by central and skewed difference methods
The central, skewed right and skewed left difference approximations are calculated by using Taylor table method as shown below:
1. Central difference
2. Right skewed difference
3. Left skewed difference
Following is the Octave program that compares the error inculcated in these methods as a result of approximations:
clear all
close all
clc
% f(x)=exp(x)*cos(x)
x=pi/3;
dx=linspace(pi/400,pi/4,50);
actual_second_derivative=-2*exp(x)*sin(x);
z=ones(1,50);
actual_second_derivative=actual_second_derivative*z; % Converting into 1x50 matrix
for i=1:length(dx)
% CENTRAL DIFFERENCE APPROXIMATION(cda)
% f\'\'(x)=(-f(x-2dx)+16f(x-dx)-30f(x)+16f(x+dx)-f(x+2dx))/(12*(dx^2))
cda(i)=(-ec(x-2*dx(i))+16*ec(x-dx(i))-30*ec(x)+16*ec(x+dx(i))-ec(x+2*dx(i)))/(12*(dx(i)^2));
error_cda(i)=abs(cda(i)-actual_second_derivative(i));
% SKEWED RIGHT SIDED DIFFERENCE APPROXIMATION(srda)
% f\'\'(x)=(45f(x)-154f(x+dx)+214f(x+2dx)-156f(x+3dx)+61f(x+4dx)-10f(x+5dx))/(12*(dx^2))
srda(i)=(45*ec(x)-154*ec(x+dx(i))+214*ec(x+2*dx(i))-156*ec(x+3*dx(i))+61*ec(x+4*dx(i))-10*ec(x+5*dx(i)))/(12*(dx(i)^2));
error_srda(i)=abs(srda(i)-actual_second_derivative(i));
% SKEWED LEFT SIDED DIFFERENCE APPROXIMATION(slda)
% f\'\'(x)=(45f(x)-154f(x-dx)+214f(x-2dx)-156f(x-3dx)+61f(x-4dx)-10f(x-5dx))/(12*(dx^2))
slda(i)=(45*ec(x)-154*ec(x-dx(i))+214*ec(x-2*dx(i))-156*ec(x-3*dx(i))+61*ec(x-4*dx(i))-10*ec(x-5*dx(i)))/(12*(dx(i)^2));
error_slda(i)=abs(slda(i)-actual_second_derivative(i));
% FORWARD DIFFERENCE APPROXIMATION(fda)
% f\'\'(x)=(f(x+2dx)-2f(x+dx)+f(x))/(dx^2)
fda(i)=(ec(x+2*dx(i))-2*ec(x+dx(i))+ec(x))/(dx(i)^2);
error_fda(i)=abs(fda(i)-actual_second_derivative(i));
% BACKWARD DIFFERENCE APPROXIMATION(bda)
% f\'\'(x)=(f(x)-2f(x-dx)+f(x-2dx))/(dx^2)
bda(i)=(ec(x)-2*ec(x-dx(i))+ec(x-2*dx(i)))/(dx(i)^2);
error_bda(i)=abs(bda(i)-actual_second_derivative(i));
end
plot(dx,actual_second_derivative,\'r\',\'linewidth\',4)
hold on
plot(dx,cda,\'--b\',\'linewidth\',2)
hold on
plot(dx,srda,\'g\',\'linewidth\',2)
hold on
plot(dx,slda,\'c\',\'linewidth\',2)
xlabel(\'dx\',\'FontSize\',14)
ylabel(\'Second order derivative value\',\'FontSize\',14)
legend(\'Actual value\',\'cda\',\'srda\',\'slda\')
legend(\'Location\',\'northeastoutside\')
axis([0 .3 -6 -4.9])
axis manual
figure(2)
plot(dx,error_cda,\'b\',\'linewidth\',2)
hold on
plot(dx,error_srda,\'r\',\'linewidth\',2)
hold on
plot(dx,error_slda,\'g\',\'linewidth\',2)
hold on
plot(dx,error_fda,\'c\',\'linewidth\',2)
hold on
plot(dx,error_bda,\'k\',\'linewidth\',2)
xlabel(\'dx\',\'FontSize\',14)
ylabel(\'Error\',\'FontSize\',14)
legend(\'cda error\',\'srda error\',\'slda error\',\'fda error\',\'bda error\')
legend(\'Location\',\'northeastoutside\')
title(\'ERROR\',\'FontSize\',15,\'FontWeight\',\'bold\')
figure(3)
loglog(dx,error_cda,\'b\',\'linewidth\',2)
hold on
loglog(dx,error_srda,\'r\',\'linewidth\',2)
hold on
loglog(dx,error_slda,\'g\',\'linewidth\',2)
hold on
loglog(dx,error_fda,\'c\',\'linewidth\',2)
hold on
loglog(dx,error_bda,\'k\',\'linewidth\',2)
xlabel(\'dx\',\'FontSize\',14)
ylabel(\'Error\',\'FontSize\',14)
legend(\'cda error\',\'srda error\',\'slda error\',\'fda error\',\'bda error\')
legend(\'Location\',\'northeastoutside\')
title(\'LOGLOG ERROR\',\'FontSize\',15,\'FontWeight\',\'bold\')
The function \"ec\" used in the program is defined as below:
function out=ec(x)
out=exp(x)*cos(x);
end
The central difference method uses the data from both sides of point of interest to calculate the results. On the other hand, left and right skewed difference methods require data only from one side of point of interest to calculate the results. We can thus realise that the later methods are handy when we have the information about only one side of point of interest. For example, fluid flow through a pipe from one end to other. Here, central difference method will not work due to lack of sufficient data.
On the other hand, central difference method is more accurate than skewed methods. This is because central difference method takes data from points that are nearby the point of interest and thus contain more accurate information about the point. Skewed methods take data from points that are very far off from the point of interest and thus contain less accurate information about the point.
FDS and BDS are further less accurate than the former discussed methods as these are based on first order approximations which neglect more terms than the former methods which are based on fourth order approximations.