Week 3.5 - Deriving 4th order approximation of a 2nd order derivative using Taylor Table method
close all
clear all
clc
%right side skued scheme
%analatical fig. = exp(x)*cox(x)
dxx=linspace(pi/40,pi/4,40);
x=pi/4;
i=@(x) exp(x).*cos(x);
a_d= -2.*exp(x).*sin(x);
%right side skued
a=15/4;
b=-77/6;
c=107/6;
d=-13;
e=61/12;
g=-5/6;
% central diff.
a1=-1/12;
b1=4/3;
c1=-5/2;
d1=4/3;
e1=-1/12;
%backward diff
a2=15/4;
b2=-77/6;
c2=107/6;
d2=-13;
e2=61/12;
g2=-5/6;
for j=1:length(dxx)
dx=dxx(j)
right_side= ((a*i(x))+b*(i(x+dx))+c*(i(x+(2*dx)))+d*(i(x+(3*dx)))+e*(i(x+(4*dx)))+g*(i(x+(5*dx))))/(dx^2);
central_diff= ((a1*i(x-(2*dx)))+b1*(i(x-dx))+c1*(i(x))+d1*(i(x+dx))+e1*(i(x+(2*dx))))/(dx^2);
backward_side= ((a2*i(x))+b2*(i(x-dx))+c2*(i(x-(2*dx)))+d2*(i(x-(3*dx)))+e2*(i(x-(4*dx)))+g2*(i(x-(5*dx))))/(dx^2);
error(j)= abs(right_side-a_d);
error_centeral(j)= abs(central_diff-a_d);
error_back(j)= abs(backward_side-a_d)
end
loglog(dxx,error,'->')
hold on
loglog(dxx,error_centeral,'->','Color','r')
hold on
loglog(dxx,error_back,'->','color','g')
hold off
xlabel('dx')
ylabel('error amplitude')
legend('Right side skued differencing','central differencing','Left side skued differencing')

As the graph shows that the central diff. has the least error and as well as the value of dx digresses the error value also decreases