clc
x = pi/3;
f_x = exp(x)*cos(x);
clock = 0;
%Defining the for loop for storing the function value for each x value
for n = 40:40:4000
clock = clock+1;
i = clock;
dx=pi/n;
dx_value(i) = dx;
%Analtical Calculation
## f_dash = (exp(x)*cos(x))+(-sin(x)*exp(x));
f_double_dash(i) = (((exp(x)*cos(x))+(-sin(x)*exp(x)))+((-sin(x)*exp(x))+(-cos(x)*exp(x))));
f_double_dash(i)
%central_difference
%Using Taylor's table Method, we found the coordinates
a=-0.083333;
b=1.333333;
c=-2.500000;
d=1.333333;
e=-0.083333;
central_differencing_fourth_order_approx(i) = ((a*exp(x-(2*dx))*cos(x-2*dx))+(b*exp(x-dx)*cos(x-dx))+(c*exp(x)*cos(x))+(d*exp(x+dx)*cos(x+dx))+(e*exp(x+2*dx)*cos(x+2*dx)))/dx^2;
error_central_differencing(i) = abs(central_differencing_fourth_order_approx(i)-f_double_dash(i));
%skewed left-sided difference
%Using Taylor's table Method, we found the coordinates
a=-0.83333;
b=5.08333;
c=-13.00000;
d=17.83333;
e=-12.83333;
g=3.75000;
skewed_left_sided_fourth_order_approx(i)=((a*exp(x-5*dx)*cos(x-5*dx))+(b*exp(x-4*dx)*cos(x-4*dx))+(c*exp(x-3*dx)*cos(x-3*dx))+(d*exp(x-2*dx)*cos(x-2*dx))+(e*exp(x-dx)*cos(x-dx))+(g*exp(x)*cos(x)))/dx^2;
error_skewed_left_sided(i)=abs(skewed_left_sided_fourth_order_approx(i)-f_double_dash(i));
%skewed right-sided
%Using Taylor's table Method, we found the coordinates
a=3.75000;
b=-12.83333;
c=17.83333;
d=-13.00000;
e=5.08333;
g=-0.83333;
skewed_right_sided_fourth_order_approx(i)=((a*exp(x)*cos(x))+(b*exp(x+dx)*cos(x+dx))+(c*exp(x+2*dx)*cos(x+2*dx))+(d*exp(x+3*dx)*cos(x+3*dx))+(e*exp(x+4*dx)*cos(x+4*dx))+(g*exp(x+5*dx)*cos(x+5*dx)))/dx^2;
error_skewed_right_sided(i)=abs(skewed_right_sided_fourth_order_approx(i)-f_double_dash(i));
endfor
%Ploting the value of the analtical, first, second and fourth order to understand the deveation
semilogx(dx_value,f_double_dash);
hold on;
semilogx(dx_value,central_differencing_fourth_order_approx,'r');
hold on;
semilogx(dx_value,skewed_left_sided_fourth_order_approx,'b');
hold on;
semilogx(dx_value,skewed_right_sided_fourth_order_approx,'g');
xlabel('dx');
ylabel('error value');
title('4th order error of second-order derivative using central, skewed right-sided, and skewed left-sided scheme');
legend('f dash analtical', 'Central Difference Method','Skewed Right-sided Error', 'Skwed Left-sided Error');
print -djpg '4th order error of second-order derivative using central, skewed right-sided, and skewed left-sided scheme.jpg'