Taylor Table Method and Matlab Code
clear all
close all
clc
% Define Inputs
x = pi/3;
dx = linspace(pi/40,pi/4000,50);
% Error calculation of fourth order approximation of second derivative
% Using CDE,SRSDE,SLSDE calculate the error
for i = 1:length(dx)
analytical_derivative = -(2*exp(x)*sin(x));
central_difference_error(i) = central_approximation(x,dx(i));
skewed_right_side_difference_error(i)= skewed_right_side_approximation(x,dx(i));
skewed_left_side_difference_error(i)= skewed_left_side_approximation(x,dx(i));
end
% Ploting the graph to compare the error
loglog(dx,central_difference_error,\'color\',\'r\')
hold on
loglog(dx,skewed_right_side_difference_error,\'color\',\'b\')
hold on
loglog(dx,skewed_left_side_difference_error,\'color\',\'g\')
legend(\'central difference error\',\'skewed right side difference\',\'skewed left side difference\')
xlabel(\'dx\')
ylabel(\'error\')
function solution = central_approximation(x,dx);
% Analytical function
%f(x) = exp(x)*cos(x)
% f\'\'(x) = -(2*exp(x)*sin(x));
analytical_derivative = -(2*exp(x)*sin(x));
% central_difference =(-0.0833*f(x-2*dx)+1.3333*f(x-dx)-2.5*f(x)+1.3333*f(x+dx)-0.0833*f(x+2*dx))/(dx)^2
% Fourth order approximation of second order derivative using central difference
central_difference =(-0.0833*exp(x-2*dx)*cos(x-2*dx)+1.3333*exp(x-dx)*cos(x-dx)-2.5*exp(x)*cos(x)+1.3333*exp(x+dx)*cos(x+dx)-0.0833*exp(x+2*dx)*cos(x+2*dx))/(dx)^2;
solution = abs(analytical_derivative - central_difference);
end
function solution = skewed_left_side_approximation(x,dx);
% Analytical function
%f(x) = exp(x)*cos(x)
% seccond order derivative
% f\'\'(x) = -(2*exp(x)*sin(x))
analytical_derivative = -(2*exp(x)*sin(x));
% central_difference =(-0.8333*f(x-5*dx)+5.0833*f(x-4*dx)-13*f(x-3*dx)+17.8333*f(x-2*dx)-12.833*f(x-dx)+3.75*(x))/(dx)^2
% Fourth order approximation of second order derivative using central difference
skewed_left_side_difference =(-0.8333*exp(x-5*dx)*cos(x-5*dx)+5.0833*exp(x-4*dx)*cos(x-4*dx)-13*exp(x-3*dx)*cos(x-3*dx)+17.8333*exp(x-2*dx)*cos(x-2*dx)-12.8333*exp(x-dx)*cos(x-dx)+3.75*exp(x)*cos(x))/(dx)^2;
solution = abs(analytical_derivative - skewed_left_side_difference);
end
function solution = skewed_right_side_approximation(x,dx);
% Analytical function
%f(x) = exp(x)*cos(x)
% seccond order derivative
% f\'\'(x) = -(2*exp(x)*sin(x))
analytical_derivative = -(2*exp(x)*sin(x));
% central_difference =(3.75*f(x)-12.3333*f(x+dx)+17.8333*f(x+2*dx)-13*f(x+3*dx)+5.0833*f(x+4*dx))-0.8333*f(x+5*dx))/(dx)^2
% Fourth order approximation of second order derivative using central difference
skewed_right_side_difference =(3.75*exp(x)*cos(x)-12.8333*exp(x+dx)*cos(x+dx)+17.8333*exp(x+2*dx)*cos(x+2*dx)-13*exp(x+3*dx)*cos(x+3*dx)+5.0833*exp(x+4*dx)*cos(x+4*dx)-0.8333*exp(x+5*dx)*cos(x+5*dx))/(dx)^2;
solution = abs(analytical_derivative - skewed_right_side_difference);
end
we plot the graph taking the central approximation(five variable), skewed right side approximation (six variable), a skewed left side approximation (six variable). As we can see in the graph that the error in skewed right side differences maximum when the value of dx is greater but when the value of dx decreases, the error also decreases and come to a constant value.
The error in skewed left side differences maximum when the value of dx is greater but when the value of dx decreases and after some value of dx it again increases and comes to a constant value.The error in the central difference is approximately constant at every value of dx we choose.
we can see that in CDS we require the values of forwarding and backward points and compute it at the same time so the effective points are not considered which are located in corners. whereas in a skewed scheme we can easily calculate the variable at the corners (either for the right or left scheme)