Project

Computing Analytical Function Using Taylor table method

Aim: To compute and Compare 4th order approximations of the second order derivative using

1. Central difference method

2. Skewed right sided difference method

3. Skewed left sided difference method

Inputs:

Analytical Function:exp(x)*cos(x)

Source : GNU OCTAVE

Source code:

clear all
close all
clc

% Initial conditions
x = pi/3;
dx = linspace(pi/4, pi/4000, 20);



%Central_difference_scheme

function error = Central_difference_scheme(x, dx)

% analytical function f(x)= exp(x)*cosx
% analytical derivative f\'\'(x) = -2*exp(x)*sin(x)
analytical_derivative =  -2*exp(x)*sin(x);

% numerical derivative
% Central difference scheme of fourth order of second derivative
% Central difference scheme d^2y/dx^2 = (a* f(i-2) + b* f(i-1)  + c* f(i) + d* f(i+1) + e* f(i+2))/(dx^2)
% cofficients a, b, c, d, & e are calculated by using Taylor table method.
% [A][X] = [B];
A = [1 1 1 1 1; -2 -1 0 1 2; 2 0.5 0 0.5 2; -8/6 -1/6 0 1/6 8/6; 16/24 1/24 0 1/24 16/24];
B = [0; 0; 1; 0; 0];
C = linsolve(A,B);
% Fourth order aproximation
central_differencing = ((C(1)*exp(x-2*dx)*cos(x-2*dx)) + (C(2)*exp(x-dx)*cos(x-dx)) + (C(3)*exp(x)*cos(x)) + (C(4)*exp(x+dx)*cos(x+dx)) + (C(5)*exp(x+(2*dx))*cos(x+(2*dx))))/(dx^2);
% error
error = abs(central_differencing - analytical_derivative);
end

%Skewed_right_sided_difference

function error = Skewed_right_sided_difference(x, dx)

% analytical function f(x)= exp(x)*cosx
% analytical derivative f\'\'(x) = -2*exp(x)*sin(x)
analytical_derivative =  -2*exp(x)*sin(x);

% numerical derivative
% Skewed right sided difference of fourth order of second derivative
% Skewed right sided difference d^2y/dx^2 =  (a* f(i) + b* f(i+1) +c*f(i+2) + d* f(i+3) + e* f(i+4))/(dx^2)
% cofficients a, b, c, d, & e are calculated by using Taylor table method.
% [A][X] = [B];
A = [1 1 1 1 1; 0 1 2 3 4; 0 1/2 2 9/2 8; 0 1/6 8/6 27/6 64/6; 0 1/24 16/24 81/24 256/24];
B = [0; 0; 1; 0; 0];
C = linsolve(A,B);
% Fourth order aproximation
right_sided_differencing = ((C(1)*exp(x)*cos(x)) + (C(2)*exp(x+dx)*cos(x+dx)) + (C(3)*exp(x+(2*dx))*cos(x+(2*dx))) + (C(4)*exp(x+(3*dx))*cos(x+(3*dx))) + (C(5)*exp(x+(4*dx))*cos(x+(4*dx))))/(dx^2);
% error
error = abs(right_sided_differencing - analytical_derivative);
end

%Skewed_left_sided_difference

function error = Skewed_left_sided_difference(x, dx)

% analytical function f(x)= exp(x)*cosx
% analytical derivative f\'\'(x) = -2*exp(x)*sin(x)
analytical_derivative =  -2*exp(x)*sin(x);

% numerical derivative
% Skewed left sided difference of fourth order of second derivative
% Skewed left sided difference d^2y/dx^2 =  (a* f(i) + b* f(i-1) +c* f(i-2)+ d* f(i-3) + e* f(i-4))/(dx^2)
% cofficients a, b, c, d, & e are calculated by using Taylor table method.
% [A][X] = [B];
A = [1 1 1 1 1; 0 -1 -2 -3 -4; 0 1/2 2 9/2 8; 0 -1/6 -8/6 -27/6 -64/6; 0 1/24 16/24 81/24 256/24];
B = [0; 0; 1; 0; 0];
C = linsolve(A,B);
% Fourth order aproximation
left_sided_differencing = ((C(1)*exp(x)*cos(x)) + (C(2)*exp(x-dx)*cos(x-dx)) + (C(3)*exp(x-(2*dx))*cos(x-(2*dx))) + (C(4)*exp(x-(3*dx))*cos(x-(3*dx))) + (C(5)*exp(x-(4*dx))*cos(x-(4*dx))))/(dx^2);
% error
error = abs(left_sided_differencing - analytical_derivative);
end

%Farword_difference_scheme

function error = Farword_difference_scheme(x, dx)

% analytical function f(x)= exp(x)*cosx
% analytical derivative f\'(x) = exp(x)*cosx - exp(x)*sinx
% analytical derivative f\'\'(x) = -2*exp(x)*sin(x)
analytical_derivative =  -2*exp(x)*sin(x);

% numerical derivative
% farword differencing scheme (FDS) of first order of second derivative
% farword_differencing = 2*(f(x+dx)- f(x) - f\'(x)*dx)/(dx^2);

% first order aproximation
farword_differencing = (2*(exp(x+dx)*cos(x+dx) - exp(x)*cos(x) - dx*(exp(x)*cos(x) - exp(x)*sin(x))))/(dx^2);
% error
error = abs(farword_differencing - analytical_derivative);
end


%Backword_difference_scheme

function error = Backword_difference_scheme(x, dx)

% analytical function f(x)= exp(x)*cosx
% analytical derivative f\'(x) = exp(x)*cosx - exp(x)*sinx
% analytical derivative f\'\'(x) = -2*exp(x)*sin(x)
analytical_derivative =  -2*exp(x)*sin(x);

% numerical derivative
% backword differencing scheme (BDS) of first order of second derivative
% backword_differencing = 2*(f(x-dx)- f(x) + f\'(x)*dx)/(dx^2);

% first order aproximation
backword_differencing = (2*(exp(x-dx)*cos(x-dx) - exp(x)*cos(x) + dx*(exp(x)*cos(x) - exp(x)*sin(x))))/(dx^2);
% error
error = abs(backword_differencing - analytical_derivative);
end


for i = 1:length(dx)
    dx(i);
    Central_difference_error(i) = Central_difference_scheme(x, dx(i));
    
    Skewed_right_sided_error(i) = Skewed_right_sided_difference(x, dx(i));
    
    Skewed_left_sided_error(i) = Skewed_left_sided_difference(x, dx(i));
    
    Farword_difference_error(i) = Farword_difference_scheme(x, dx(i));
    
    Backword_difference_error(i) = Backword_difference_scheme(x, dx(i));
end


%plotting

     figure(1)
     loglog(dx, Central_difference_error, \'b\')
     hold on
     loglog(dx, Skewed_right_sided_error, \'r\')
     hold on
     loglog(dx, Skewed_left_sided_error, \'g\')
     hold on
     loglog(dx, Farword_difference_error)
     hold on
     loglog(dx, Backword_difference_error)
     xlabel(\'dx\')
     ylabel(\'Error\')
     legend(\'Central_ difference_ error\',\'Skewed_ right_ sided_ error\',\'Skewed_ left_ sided_ error\',\'Farword_ difference_ error\',\'Backword_ difference_ error\')
     title(\'Comparison of different scheme errors\')
     grid on

Resultant Plots Obtined:

Conclusion:

From the Observation in Resultant plot obtained:

1)From the above Plot we can consider that Central Difference Scheme in Second Order Derivative.Hence to find out more accurate results Central difference Scheme is more preffered

2)From the plot we can easily say that BDS gives less Truncation error than FDS and FDS gives maximum Truncation error among all other Schemes

Comparision between Skewed scheme and Central difference scheme is If we want more accurate results we need CD Scheme Because it gives minimum truncation Error among them.But if you want single or higher order approximation results with different random Discrete points,CD scheme is no longer used in this situation we need Skewed scheme

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.