Project

Taylor table

Code:

close all
clear all
clc

% f(x) = (e^x)*cos(x)
% f\'\'(x) = -2*e^(x)*sin(x)

% initial definition
x = pi/3;
dx = pi/40;

%analytical
ana_diff = -2*sin(x)*exp(1)^(x);

%central_difference
cent_diff = ((-3*(cos(x-(2*dx))*(exp(1)^(x-(2*dx))))) + (16*(cos(x-(dx))*(exp(1)^(x-(dx))))) - (26*(cos(x)*(exp(1)^(x)))) + (16*(cos(x+(dx))*(exp(1)^(x+(dx))))) - (3*(cos(x+(2*dx))*(exp(1)^(x+(2*dx))))))/(8*(dx^(2)));

%skewed forward diff
for_diff = ((35*(cos(x)*(exp(1)^(x)))) - (104*(cos(x+(dx))*(exp(1)^(x+(dx))))) + (114*(cos(x+(2*dx))*(exp(1)^(x+(2*dx))))) - (56*(cos(x+(3*dx))*(exp(1)^(x+(3*dx))))) + (11*(cos(x+(4*dx))*(exp(1)^(x+(4*dx))))))/(12*(dx^(2)));

%skewed bac diff
bac_diff = ((11*(cos(x-(4*dx))*(exp(1)^(x-(4*dx))))) - (56*(cos(x-(3*dx))*(exp(1)^(x-(3*dx))))) + (114*(cos(x-(2*dx))*(exp(1)^(x-(2*dx))))) - (104*(cos(x-(dx))*(exp(1)^(x-(dx))))) +(35*(cos(x)*(exp(1)^(x)))))/(12*(dx^(2)));

%cent diff error
c_error = abs(ana_diff - cent_diff);
%forward diff error
f_error = abs(ana_diff - for_diff);
%backward diff error
b_error = abs(ana_diff - bac_diff);

%plot
error = [c_error f_error b_error];
bar(c_error,\'b\');
hold on
bar(2,f_error,\'r\');
hold on
bar(3,b_error,\'g\');
legend(\'central difference error\',\'forward difference error\',\'backward difference error\');
xlabel(\'scheme\');
ylabel(\'error\');

t = text(1:length(error),error,num2str(error\'),\'vert\',\'bottom\',\'horiz\',\'center\');
t(1).Color = \'blue\';
t(2).Color = \'red\';
t(3).Color = \'green\';
box off

Bar plot comparing the absolute error:

\"\"

Explanation:

From the bar plots it can be seen that error is much larger for central difference scheme when compared to forward and backward difference schemes.

Further, although the FDS and BDS have very small error, BDS seems to have the least error value. Thus BDS seems to give more accurate value for second order derivative with fourth order approximations.

As said before skewed schemes (FDS and BDS) seem to produce accurate results than CDS. This is because in a skewed scheme, the values are taken from adjacent points in the same direction. Whereas in a centered scheme, the values are taken from adjacent points in opposite directions alternatively. This leads to jumping of points in between leading to unstable solutions.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.