Project

Taylor Table

From the graph, its seen that the FDS and BDS methods give a higher error compared with the skewed methods.In a skewed scheme, its taken into account more than one data points for calculating the data at any particular point.This helps us to get more accurate values.Sometimes when evaluating a point the left side function of the point might have a greater effect on the evaluated value or right side might have a greater effect on the evaluated value.Only in such cases where the function changes drastically about the point to be evaluated can a skewed scheme be useful.Hence it is important to identify the nature of the given function and then decide what scheme needs to be employed for the most accurate results.

Slope using skewed left sided difference scheme is approximately: 4
Slope using central difference scheme is approximately: 4
Slope using skewed left sided difference scheme is approximately: 4

function [f_out] = skew_right(x,dx)

%Analytical function = exp(x)*cos(x)

%Analytical derivative = -2*exp(x)*sin(x)
a_d = -2*exp(x)*sin(x);

%Writing individual numerator terms
T1 = 35*func(x);
T2 = -104*func(x + dx);
T3 = 114*func(x + 2*dx);
T4 = -56*func(x + 3*dx);
T5 = 11*func(x + 4*dx);

%Numerical derivative - Skewewd right sided difference scheme
srsds = (T1 + T2 + T3 + T4 + T5)/(12*dx^2);

%Global Truncation Error
f_out = abs(srsds - a_d);

end

% new file
function [f_out1] = central(x,dx)

%Analytical function = exp(x)*cos(x)

%Analytical derivative = -2*exp(x)*sin(x)
a_d = -2*exp(x)*sin(x);

%Numerical derivative - Skewewd right sided difference scheme
cds = ((16*(func(x+dx) + func(x-dx))) - (func(x+(2*dx)) + func(x-(2*dx))) - (30*func(x)))/(12*dx^2);

%Global Truncation Error
f_out1 = abs(cds - a_d);

end

% new file
function [f_out2] = skew_left(x,dx)

%Analytical function = exp(x)*cos(x)

%Analytical derivative = -2*exp(x)*sin(x)
a_d = -2*exp(x)*sin(x);

%Writing individual numerator terms
t1 = 11*func(x - 4*dx);
t2 = -56*func(x - 3*dx);
t3 = 114*func(x - 2*dx);
t4 = -104*func(x - dx);
t5 = 35*func(x);

%Numerical derivative - Skewewd left sided difference scheme
s_d = (t1 + t2 + t3 + t4 + t5)/(12*dx^2);

%Global Truncation Error
f_out2 = abs(s_d - a_d);

end

%new file
function [out] = func(a)

out = exp(a)*cos(a);

end

%new file
function slope = gradient(y2,y1,x2,x1)

slope = (y2 - y1)/(x2 - x1);

end


%%Problem setup
x = pi/3;  %Current value of x
dx = linspace(0.01,1,10); %Array for dx values

%%For loop - Calculating error for each value of dx
for i = 1:length(dx)
     error_1(i) = skew_right(x,dx(i));  %Calling a function - skew_right which will calculate fourth order error for SRSDS
     error_2(i) = central(x,dx(i)); %Calling a function - central which will calculate fourth order error for CDS
     error_3(i) = skew_left(x,dx(i)); %Calling a function - skew_left which will calculate fourth order error for SLSDS
end

 
%%Displaying and saving the plot generated
figure(1)
loglog(dx, error_1)
hold on
loglog(dx, error_2)
loglog(dx, error_3)
xlabel(\'step size\',\'FontSize\',12)
ylabel(\'Global Truncation Error\')
title(\'Fourth order approximations of second orderd derivative\')
axis(\'tight\')
h =  legend(\'FDS\',\'CDS\',\'BDS\',\'location\',\'northwest\');
set(h, \'FontSize\',12);
saveas(gca, \'comparing_schemes.png\')

%%Slope calculation to determine the order of accuracy
%Applying log to calculate the slope
r = log10(dx);
s = log10(error_1);
t = log10(error_2);
u = log10(error_3);
%Calling a function - gradient to calculate slope using 5th and 6th points of each scheme
g = gradient(s(6),s(5),r(6),r(5));
g1 = gradient(t(6),t(5),r(6),r(5));
g2 = gradient(u(6),u(5),r(6),r(5));
%%Printing the slope
fprintf(\'Slope using skewed left sided difference scheme is approximately: \\n\', g)
fprintf(\'Slope using central difference scheme is approximately: \\n\', g1)
fprintf(\'Slope using skewed left sided difference scheme is approximately: \\n\', g2)

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.