Project

Comparison of Numerical Errors in Second derivative of functions using 4th Order Central Difference and Skewed Left and Right Sided schemes

The entire report and the MATLAB Codes have been attached using google drive.

Please Refer it.

The MATLAB Code and the results are -

%Comparison of the different 4th order schemes for the second derviative of
% f(x) = exp(x)*cos(x)
% f\'(x) = exp(x)*cos(x) - exp(x)*sin(x)
% f\"(x) = -2*exp(x)*sin(x)
clear all
clc
close all
x = pi/3;   %Point at which derivative has to be determined
dx = linspace(pi/40,pi/4000,20);   % Grid Spacing
func = ordercmp(x); %Original Function f(x) at x

%% Analytical calculation of the derivative
SecondDer_an = -2*exp(x)*sin(x);

%% Second Derivative using 4th Order Approximation.
for i = 1:length(dx)
    
    %Using Central differening with uniform spacing
    A_C = [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_C = [0; 0; 1/(dx(i)^2); 0; 0];
    X_C = inv(A_C)*b_C;     %Coefficients used for deriving the equation
    CDS(i) = X_C(1)*ordercmp(x-(2*dx(i)))+ X_C(2)*ordercmp(x-dx(i))+X_C(3)*ordercmp(x)+X_C(4)*ordercmp(x+dx(i))+X_C(5)*ordercmp(x+(2*dx(i)));
    
    %Using Skewed Right Sided Differencing Scheme with uniform spacing
    A_R = [1 1 1 1 1; 0 1 2 3 4; 0 0.5 2 9/2 8; 0 1/6 8/6 27/6 64/6; 0 1/24 16/24 81/24 256/24];
    b_R = [0; 0; 1/(dx(i)^2); 0; 0];
    X_R = inv(A_R)*b_R;
    SRSDS(i) = X_R(1)*ordercmp(x)+X_R(2)*ordercmp(x+dx(i))+X_R(3)*ordercmp(x+(2*dx(i)))+X_R(4)*ordercmp(x+(3*dx(i)))+X_R(5)*ordercmp(x+(4*dx(i)));
    
    %Using Skewed Left Sided Differencing Scheme
    A_L = [1 1 1 1 1; -4 -3 -2 -1 0; 8 9/2 2 1/2 0; -64/6 -27/6 -8/6 -1/6 0; 256/24 81/24 16/24 1/24 0];
    b_L = [0; 0; 1/(dx(i)^2); 0; 0];
    X_L = inv(A_L)*b_L;
    SLSDS(i) = X_L(1)*ordercmp(x-(4*dx(i)))+X_L(2)*ordercmp(x-(3*dx(i)))+X_L(3)*ordercmp(x-(2*dx(i)))+X_L(4)*ordercmp(x-dx(i))+X_L(5)*ordercmp(x);
    
    
    % Using FDS
    
    FDS(i) = (ordercmp(x+(2*dx(i))) - 2*ordercmp(x+dx(i)) + ordercmp(x))/(dx(i)^2);
    
    % Using BDS
    
    BDS(i) = (ordercmp(x) - 2*ordercmp(x-dx(i)) + ordercmp(x-(2*dx(i))))/(dx(i)^2);
    
    %% Absolute Error Calculation and Plotting
    error_CDS(i) = abs(SecondDer_an - CDS(i));
    error_SRSDS(i) = abs(SecondDer_an - SRSDS(i));
    error_SLSDS(i) = abs(SecondDer_an - SLSDS(i));
    error_FDS(i) = abs(SecondDer_an - FDS(i));
    error_BDS(i) = abs(SecondDer_an - BDS(i));
end

Error = [error_CDS;error_SRSDS;error_SLSDS;error_FDS;error_BDS];

figure(1)
semilogy(dx,Error(1:3,:)\',\'o-\');
legend(\'Central Differencing Scheme\',\'Skewed Right Sided Scheme\',\'Skewed Left Sided Scheme\');
ylabel(\'Absolute Error in Numerical Computation\');
xlabel(\'Step size (dx(i))\');
grid on;
title(\'Absolute errors in Second derivative vs Step Size in Numerical Computation(log(Error) vs Step Size)\');
h = annotation(\'textbox\', [0.6 0.3 0.4 0.4], \'String\', \'f(x) = e(x)*cos(x) at x = pi/3\',\'FitBoxToText\',\'on\',\'FontSize\',12);


figure(2)
semilogy(dx,Error\',\'o-\');
legend(\'Central Differencing Scheme\',\'Skewed Right Sided Scheme\',\'Skewed Left Sided Scheme\',\'Forward Differencing Scheme\',\'Backward Differencing Scheme\');
ylabel(\'Absolute Error in Numerical Computation\');
xlabel(\'Step size (dx)\');
grid on;
title(\'Absolute errors in Second derivative vs Step Size in Numerical Computation(log(Error) vs Step Size)\');
h = annotation(\'textbox\', [0.6 0.3 0.4 0.4], \'String\', \'f(x) = e(x)*cos(x) at x = pi/3\',\'FitBoxToText\',\'on\',\'FontSize\',12);

The function \'ordercmp\' used in the main script is -

function out = ordercmp(a)
out = exp(a)*cos(a);
end

From below figures it is very clear that for the given problem, the CDS has the least numerical error while the FDS and BDS have the most. This can be due to two reasons. Firstly, because the points considered in the CDS are symmetric. Secondly, the number of nodes considered in CDS (also in SRSDS and SLSDS) are more than that in the FDS and the BDS. Thus CDS shows the best accuracy.

From figure 2, the order in which the errors are present in the scheme are –

FDS = BDS > SRSDS > SLSDS > CDS

The skewed schemes are useful in the cases when we need to determine the values at end nodes in a domain, where there are not sufficient nodes on the either side to use the CDS.

The link to the detailed derivation is -

https://drive.google.com/file/d/10OZwy2QZsGmuaQ8ljefF1DeXwJzgUNj2/view?usp=sharing

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.