fourth order approximation of an second order derivative
Central Difference method= This method is useful to get high accuracy solutions with a minimum number of iterations when compared to other difference methods however there are some limitations to this method because it cannot apply at the boundary because it takes information from node all surrounded by it. In the graph, we can see that when the dx value is decreasing the error in the central difference is increasing exponentially. When we compare at the high value of dx it shows far better results than other methods the error is less than 1 up to dx value=0.4 when the dx value is decreasing below 0.4 the error is increasing exponentially.
Skewed Right-Hand Difference method= At the high value of dx the error percentage is more in this method when the dx value is getting smaller the accuracy is increasing proportionally here at high dx the obtained value is always higher than the original value as the iteration passes the value gets decreased and will get closer to dx value.
Skewed Left-Hand Difference method= when the dx value is getting smaller the accuracy is increasing proportionally here at high dx the obtained value is always less than the original value as the iteration passes the value gets increased and will get closer to dx value.
clear all
close all
clc
x=pi/4;
dx=linspace(pi/4,pi/40,100);
% for the function exp(x)*cos(x) the second order derivative is
for i=1:length(dx)
Analytical_derivative(i)=-2*(exp(x)*sin(x));
%for central difference method
%f\'\'=-0.083f(i-2t)+1.33f(i-t)-2.5f(i)+1.33f(i+t)-0.833f(i+2t)
Central_difference(i)= (-0.083*(exp(x-2*dx(i))*cos(x-2*dx(i)))+1.33*(exp(x-dx(i))*cos(x-dx(i)))-2.5*(exp(x)*cos(x))+1.33*(exp(x+dx(i))*cos(x+dx(i)))+0.083*(exp(x+2*dx(i))*cos(x+2*dx(i))))/((dx(i))^2);
%for skewed right and left side
%%f\'\'=3.75f(i)-12.83f(i+t)+17.83f(i+2t)-13f(i+3t)+5.08f(i+4t)-0.833f(i+5t)
Skewed_right_sided_difference(i)= ((3.75*(exp(x)*cos(x)))-(12.83*(exp(x+dx(i))*cos(x+dx(i))))+(17.83*(exp(x+2*dx(i))*cos(x+2*dx(i))))-(13*(exp(x+3*dx(i))*cos(x+3*dx(i))))+(5.08*(exp(x+4*dx(i))*cos(x+4*dx(i))))-(0.8333*(exp(x+5*dx(i))*cos(x+5*dx(i)))))/((dx(i))^2);
Skewed_left_sided_difference(i)= ((3.75*(exp(x)*cos(x)))-(12.83*(exp(x-dx(i))*cos(x-dx(i))))+(17.83*(exp(x-2*dx(i))*cos(x-2*dx(i))))-(13*(exp(x-3*dx(i))*cos(x-3*dx(i))))+(5.08*(exp(x-4*dx(i))*cos(x-4*dx(i))))-(0.8333*(exp(x-5*dx(i))*cos(x-5*dx(i)))))/((dx(i))^2);
%%finding the errors
Central_difference_error(i)= Analytical_derivative(i)-Central_difference(i);
skewed_right_sided_difference_error(i)=Analytical_derivative(i)-Skewed_right_sided_difference(i);
Skewed_left_sided_difference_error(i)=Analytical_derivative(i)-Skewed_left_sided_difference(i);
end
figure(i)%%plotting the dx vs errors graphs
plot(dx,Central_difference_error,\'LineWidth\', 1)
hold on
plot(dx, skewed_right_sided_difference_error,\'LineWidth\', 1)
hold on
plot(dx,Skewed_left_sided_difference_error,\'k\',\'LineWidth\', 1)
legend(\'Central_difference_error\',\'skewed_right_sided_difference_error\',\'skewed_left_sided_difference_error\')
xlabel(\'change in dx\');
ylabel(\'error\');