Taylor table method and Matlab code
the figure shown below shows the curve for different skewed technique, from the figure it is clearly visible that the central skewed scheme gives the least error of all three schemes and left skewed scheme to give the low magnitude of error to compare to right skewed error.all the schemes follows nearly same trend.
I think central skewed scheme will not work on the starting points and on the end points of a grid.
clear all
close all
clc
x = pi/3;
dx = linspace(pi/4,pi/2000,20)
for i = 1:length(dx)
skewed_right(i) = right_sided_skewed(x,dx(i))
skewed_left(i) = left_sided_skewed(x,dx(i))
central_sided(i) = central_side(x,dx(i))
end
loglog(dx,skewed_right,'r')
hold on
loglog(dx,skewed_left,'c')
hold on
loglog(dx,central_sided,'b')
%title('comparison of fourth approximation of second order derivative of skewed right, skewed left and centra differnce')
legend('right skewed error', 'left skewed error','central skewed error','Location','southeast');
xlabel('dx')
ylabel('Error')
right skewed
function error_skewed = right_sided_skewed(x,dx)
% analytical derivative of exp(x)*cos(x)
analytical_derivative = -2*exp(x)*sin(x)
% values are calculated using pen and paper bt this can be calculated using
% inverse function in matlab
right_side_skewed = ((45*exp(x)*cos(x))-(154*exp(x+dx)*cos(x+dx))+(214*exp(x+(2*dx))*cos(x+(2*dx)))-(156*exp(x+(3*dx))*cos(x+(3*dx))) + (61*exp(x+(4*dx))*cos(x+(4*dx))) - (10*exp(x+(5*dx))*cos(x+(5*dx))))/(12*(dx^2))
error_skewed = abs(right_side_skewed - analytical_derivative)
end
left skewed
function error_skewed_left = left_sided_skewed(x,dx)
% analytical derivative of exp(x)*cos(x)
analytical_derivative = -2*exp(x)*sin(x)
left_side_skewed = ((45*exp(x)*cos(x))-(154*exp(x-dx)*cos(x-dx)) + (214*exp(x-(2*dx))*cos(x-(2*dx)))-(156*exp(x-(3*dx))*cos(x-(3*dx))) + (61*exp(x-(4*dx))*cos(x-(4*dx))) - (10*exp(x-(5*dx))*cos(x-(5*dx))))/(12*(dx^2))
error_skewed_left = abs(left_side_skewed - analytical_derivative)
end
central skewed
function error_central = central_side(x,dx)
% analytical derivative of exp(x)*cos(x)
analytical_derivative = -2*exp(x)*sin(x)
central_sided = ((-30*exp(x)*cos(x))+(16*exp(x-dx)*cos(x-dx))+(16*exp(x+dx)*cos(x+dx))-(exp(x-(2*dx))*cos(x-(2*dx)))-(exp(x+(2*dx))*cos(x+(2*dx))))/(12*(dx^2))
error_central = abs(central_sided - analytical_derivative);
end