TAYLOR TABLE METHOD s USE
Problem statement-
Write a program in Matlab to evaluate the second order derivative of the analytical function exp(x)*cos(x) and compare it with the three numerical approximations stated below:
Of 4th order approximations of the 2nd order derivative
1. Central difference
2. Skewed right sided difference
3. Skewed left sided difference
solution
%main code body
%fourth order taylor\'s table challenge
clear all
close all
clc
%function = exp(x).cos(x);
%initiallization of values
x = pi/3;
dx = linspace(0.01,1,10); %grid points
%second order derivative
analytical_second_order_derivative= -2*(exp(x)*sin(x));
%fourth order approximation of second order derivative
%via numerical methods
for i=1:length(dx)
Dx=dx(i);
central_differencing(i)=CDS(x,Dx); % central differencing scheme
error_central_differencing(i)=abs(analytical_second_order_derivative-central_differencing(i));
skewed_right_differencing(i)=skewed_FDS(x,Dx); % Skewed right difference scheme
error_skewed_FDS(i)=abs(analytical_second_order_derivative-skewed_right_differencing(i));
skewed_left_differencing(i)=skewed_BDS(x,Dx); % Skewed left difference scheme
error_skewed_BDS(i)=abs(analytical_second_order_derivative-skewed_left_differencing(i));
end
%plotting graph
loglog(dx,error_central_differencing,\'-kd\',\'markerfacecolor\',\'r\',\'markersize\',7)
hold on
grid on
loglog(dx,error_skewed_FDS,\'-ko\',\'markerfacecolor\',\'y\',\'markersize\',7)
loglog(dx,error_skewed_BDS,\'-ks\',\'markerfacecolor\',\'g\',\'markersize\',7)
xlabel(\'Step Size\')
ylabel(\'Error\')
legend(\'CD\',\'skewed right\',\'skewed left\',\'Location\',\'northwest\')
title(\'ABSOLUTE ERROR B/W SCHEMES\')
%calculating the slope
log_error_central_differencing=log10(error_central_differencing);
log_error_skewed_FDS=log10(error_skewed_FDS);
log_error_skewed_BDS=log10(error_skewed_BDS);
log_dx=log10(dx);
Slope_error_central_differencing=gradient(log_error_central_differencing(5),log_error_central_differencing(6),log_dx(5),log_dx(6));
Slope_error_skewed_FDS=gradient(log_error_skewed_FDS(5),log_error_skewed_FDS(6),log_dx(5),log_dx(6));
Slope_error_skewed_BDS=gradient(log_error_skewed_BDS(5),log_error_skewed_BDS(6),log_dx(5),log_dx(6));
%Printing the slope
fprintf(\'Slope using central difference scheme is: %g\\n\', Slope_error_central_differencing)
fprintf(\'Slope using skewed right sided difference scheme is: %g\\n\', Slope_error_skewed_FDS)
fprintf(\'Slope using skewed left sided difference scheme is: %g\\n\', Slope_error_skewed_BDS)
Various functions used are now described below:
Function body for CDS (central differencing scheme)
% code for central difference scheme (CDS)
function central_differencing=CDS(x,dx)
f_i=func(x);
f_i_minus_1=func(x-dx);
f_i_plus_1=func(x+dx);
f_i_minus_2=func(x-(2*dx));
f_i_plus_2=func(x+(2*dx));
central_differencing=((-30*f_i)+(16*f_i_minus_1)+(16*f_i_plus_1)-f_i_minus_2-f_i_plus_2)/(12*(dx^2));
end
Skewed Forward Differencing Scheme
%func for skewed right differencing(forward differencing) scheme
function skewed_right_differencing=skewed_FDS(x,dx)
f_i=func(x);
f_i_plus_1=func(x+dx);
f_i_plus_2=func(x+(2*dx));
f_i_plus_3=func(x+(3*dx));
f_i_plus_4=func(x+(4*dx));
skewed_right_differencing=((35*f_i)-(104*f_i_plus_1)+(114*f_i_plus_2)-(56*f_i_plus_3)+(11*f_i_plus_4))/(12*(dx^2));
end
Skewed Backward Differencing Scheme
%func for skewed left differencing(backward differencing) scheme
function skewed_left_differencing=skewed_BDS(x,dx)
f_i=func(x);
f_i_minus_1=func(x-dx);
f_i_minus_2=func(x-(2*dx));
f_i_minus_3=func(x-(3*dx));
f_i_minus_4=func(x-(4*dx));
skewed_left_differencing=((35*f_i)-(104*f_i_minus_1)+(114*f_i_minus_2)-(56*f_i_minus_3)+(11*f_i_minus_4))/(12*(dx^2));
end
problem function code to easy the tracking of error if any
%problem function
function f_x= func(x)
f_x=exp(x)*cos(x);
end
code to calculate the slope
%code for slope generation
function slope=gradient(y1,y2,x1,x2)
slope=(y2-y1)/(x2-x1);
end
THE GRAPH
OUTPUT FOR THE SLOPE CALCULATION