Project

Computation of higher order approximations using Skewed and Symmetric numerical stencil using Taylor method

Assignment:

1.Central Difference Scheme

For calculating the fourth order approximation of second order derivative we will require 5 stencils,namely(i-2,i-1,i,i+1,i+2)

\"\"

2.Skewed Right sided difference Scheme

For calculating the fourth order approximation of second order derivative we will require 6 stencils,namely(i,i+1,i+2,i+3,i+4,i+5)

\"\"

3.Skewed Left sided difference Scheme

For calculating the fourth order approximation of second order derivative we will require 6 stencils,namely(i,i-1,i-2,i-3,i-4,i-5)

\"\"

Code 1:

clear all;
close all;
clc;
%Given function is
%f(x)=(exp(x)*cos(x))
%Point at which function value is calculated
x=pi/4;
%Grid spacing
dx=linspace(pi/30,pi/400,40);
%Error calculation using functions in a for loop
for i=1:length(dx)
     central_difference_error(i)=taylor_table_central(x,dx(i));
     skewed_right_difference_error(i)=taylor_table_skewed_right(x,dx(i));
     skewed_left_difference_error(i)=taylor_table_skewed_left(x,dx(i));
end
%Figure 1 showing variation of dx with different approximation schemes on a
%log graph
figure(1)
loglog(dx,central_difference_error,\'linewidth\',1.5);
hold on;
loglog(dx,skewed_right_difference_error,\'linewidth\',1.5);
hold on;
loglog(dx,skewed_left_difference_error,\'linewidth\',1.5);
hold on;
grid on;
xlabel(\'log(dx)\');
ylabel(\'log(Absolute Error)\');
title(\'Comparison of errors of 2nd order derivative 4th order approximation using different methods\');
legend(\'Central Difference\',\'Skewed Right sided Difference\',\'Skewed Left sided Difference\');

%Figure 1 showing variation of dx with different approximation schemes on a
%normal graph
figure(2)
plot(dx,central_difference_error,\'linewidth\',1.5);
hold on;
plot(dx,skewed_right_difference_error,\'linewidth\',1.5);
hold on;
plot(dx,skewed_left_difference_error,\'linewidth\',1.5);
hold on;
grid on;
xlabel(\'dx\');
ylabel(\'Absolute Error\');
title(\'Comparison of errors of 2nd order derivative 4th order approximation using different methods\');
legend(\'Central Difference\',\'Skewed Right sided Difference\',\'Skewed Left sided Difference\');

Code 2:

%Function to calculate error for a central difference approximation scheme
function[result]=taylor_table_central(x,dx)

    analytical_derivative=-2*exp(x)*sin(x);
    central_approximation=(((-0.0833)*(exp(x-(2*dx))*cos(x-(2*dx))))+((1.3333)*(exp(x-dx)*cos(x-dx)))+((-2.5000)*(exp(x)*cos(x)))+((1.3333)*(exp(x+dx)*cos(x+dx)))+((-0.0833)*(exp(x+(2*dx))*cos(x+(2*dx)))))/(dx)^2;
    result=abs(analytical_derivative-central_approximation);
end

Code 3:

%Function to calculate error for a central difference approximation scheme
function[result]=taylor_table_skewed_right(x,dx)

    analytical_derivative=-2*exp(x)*sin(x);
    skewed_right_approximation=(((3.7500)*(exp(x)*cos(x)))+((-12.8333)*(exp(x+dx)*cos(x+dx)))+((17.8333)*(exp(x+(2*dx))*cos(x+(2*dx))))+((-13.0000)*(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;
    result=abs(analytical_derivative-skewed_right_approximation);
end

Code 4:

%Function to calculate error for a central difference approximation scheme
function[result]=taylor_table_skewed_right(x,dx)

    analytical_derivative=-2*exp(x)*sin(x);
    skewed_right_approximation=(((3.7500)*(exp(x)*cos(x)))+((-12.8333)*(exp(x+dx)*cos(x+dx)))+((17.8333)*(exp(x+(2*dx))*cos(x+(2*dx))))+((-13.0000)*(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;
    result=abs(analytical_derivative-skewed_right_approximation);
end

Plots:

\"\"

Inference:

1.As seen from the graphs,CDS error is very less compared to both the skewed methods.Also,becuase of faster convergence rates CDS is preferred over skewed methods.

2.CDS cannot be applied at boundary nodes because of unavailability of forward and backward node at the point.Skewed Right sided difference or Skewed Left sided difference can be applied based on flow of information.

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.