Taylor table method and Matlab code
clear all
close all
clc
x=pi/3;
dx=linspace (0.001,0.1,20);
%analytical derivative
%analytical equation=exp(x)*cos(x)
analytical_derivative=(-2*exp(x)*sin(x));
for i=1:length(dx)
central_difference(i)=central_difference_taylor_series(x,dx(i));
forward_right_differencing(i)=forward_differencing_taylor_series(x,dx(i));
backward_left_differencing(i)=backward_differencing_taylor_series(x,dx(i));
%to find the error in three approximation
Central_error(i)=abs(central_difference(i)-analytical_derivative);
Right_skewed_error(i)=abs(forward_right_differencing(i)- analytical_derivative);
Left_skewed_error(i)=abs(backward_left_differencing(i)- analytical_derivative);
end
%plotting
figure(i)
loglog(dx,Central_error,\'b\',\'linewidth\',1);
hold on
loglog(dx,Right_skewed_error,\'g\',\'linewidth\',1);
hold on
loglog(dx,Left_skewed_error,\'r\',\'linewidth\',1);
legend(\'central difference\',\'right skewed difference\',\'left skewed difference\');
xlabel(\'Grid size\');
ylabel(\'absolute error\');
title(\'error between three case\')
saveas(gca,\'error between three case.jpg\');
%central differencing
function central_difference=central_difference_taylor_series(x,dx)
a=-1/12;
b=4/3;
c=-5/2;
d=4/3;
e=-1/12;
f_i_minus_2 =(exp(x-(2*dx))*cos(x-(2*dx)));
f_i_minus_1=(exp(x-dx)*cos(x-dx));
f_i=(exp(x)*cos(x));
f_i_plus_1=(exp(x+dx)*cos(x+dx));
f_i_plus_2=(exp(x+(2*dx))*cos(x+(2*dx)));
central_difference=((a*(f_i_minus_2))+(b*(f_i_minus_1))+(c*(f_i))+(d*(f_i_plus_1))+(e*(f_i_plus_2)))/(dx^2);
end
% left_skewed
function left_differencing=backward_differencing_taylor_series(x,dx)
a=-5/6;
b=61/12;
c=-13;
d=107/6;
e=-77/6;
g=15/4;
f_i_minus_5=(exp(x-(5*dx))*cos(x-(5*dx)));
f_i_minus_4=(exp(x-(4*dx))*cos(x-(4*dx)));
f_i_minus_3=(exp(x-(3*dx))*cos(x-(3*dx)));
f_i_minus_2=(exp(x-(2*dx))*cos(x-(2*dx)));
f_i_minus_1=(exp(x-dx)*cos(x-dx));
f_i=(exp(x)*cos(x));
left_differencing=((a*(f_i_minus_5))+(b*(f_i_minus_4))+(c*(f_i_minus_3))+(d*(f_i_minus_2))+(e*(f_i_minus_1))+(g*(f_i)))/(dx^2);
end
%right skewed
function forward_right_differencing=forward_differencing_taylor_series(x,dx)
a=15/4;
b=-77/6;
c=107/6;
d=-13;
e=61/12;
g=-5/6;
f_i=(exp(x)*cos(x));
f_i_plus_1=(exp(x+dx)*cos(x+dx));
f_i_plus_2=(exp(x+(2*dx))*cos(x+(2*dx)));
f_i_plus_3=(exp(x+(3*dx))*cos(x+(3*dx)));
f_i_plus_4=(exp(x+(4*dx))*cos(x+(4*dx)));
f_i_plus_5=(exp(x+(5*dx))*cos(x+(5*dx)));
forward_right_differencing=((a*(f_i))+(b*(f_i_plus_1))+(c*(f_i_plus_2))+(d*(f_i_plus_3))+(e*(f_i_plus_4))+(g*(f_i_plus_5)))/(dx^2);
end
RESULT-:
- As the value of dx increases, the error is also increased.
The error in the central scheme is very less as compared to the error observed in the skewed scheme this is due to the fact that using the central scheme reduces the truncation error that was produced in the skewed scheme.
WHY THE SKEWED SCHEME IS USEFUL?
ANS-Because in some complex systems the numerical stencil won\'t be symmetric points is available and at this point, the only skewed scheme can help. and the advantage of using a skewed system is seen near the boundaries where there is no access to further points over the boundary i.e asymmetric.so at this point, the skewed system can be used to improve overall accuracy. While in internal points centered schemes can be used for good accuracy.