Project

Taylor table method for second order derivative

AIM:

Derive the following 4th order approximations of the second order derivative

1. Central difference

2. Skewed right sided difference

3. Skewed left sided differe

4. After derivating, write a program to evaluate the second order derivative of the analytical function exp(x)*cos(x) and compare it with the 3 numerical approximations that you have derived and plot the absolute error between them.

5. Compare the error with FDS and BDS approximations and explain how skewed scheme is useful.

GIVEN:

f(x)=exp(x)*cos(x).

PROCEDURE:

  • Deriving the fourth order approximations of the second order derivative for central differencing.
  • Central differencinf scheme:
  1. Central differencing scheme uses information from both right and left hand side neighbouring points eqaully or symentrically.
  2. So the points are f(i), f(i+1), f(i+2), f(i-1) and f(i-2), calculation is shown in the below image.

CODE:

clc
close all
clear all
%for central difference

A=[ 1 1 1 1 1 ;
    0 -1 -2 1 2;
    0 1/2 2 1/2 2;
    0 -1/6 -4/3 1/6 4/3;
    0 1/24 2/3 1/24 2/3]
B=[0; 0; 1;0 ;0]
solution= inv(A)*B

check=sum(solution)
  • Skewed right sided sheme:
  1. Skewed right sided scheme uses points from the right side grid points.
  2. So the points will be f(i), f(i+1), f(i+2), f(i+3), f(i+4) and f(i+5), calculation is shown in the below image.

CODE:

clc
close all
clear all
%for right skewed difference

A=[ 1 1 1 1 1 1;
    0 1 2 3 4 5;
    0 1/2 2 9/2 8 25/2 ;
    0 1/6 4/3 27/6 64/6 125/6;
    0 1/24 2/3 81/24 256/24 625/24;
    0 1/120 32/120 243/120 1024/120 3125/120]
B=[0; 0; 1;0 ;0 ;0]
solution= inv(A)*B

check=sum(solution)
  • Skewed left sided scheme:
  1. Skewed right sided scheme uses points from the left side grid points.
  2. So the points will be f(i), f(i-1), f(i-2), f(i-3), f(i-4) and f(i-5), calculation is shown in the below image.

CODE:

clc
close all
clear all
%for left skewed difference

A=[ 1 1 1 1 1 1;
    0 -1 -2 -3 -4 -5;
    0 1/2 2 9/2 8 25/2 ;
    0 (-1/6) (-4/3) (-27/6) (-64/6) (-125/6);
    0 1/24 2/3 81/24 256/24 625/24;
    0 (-1/120) (-32/120) (-243/120) (-1024/120) (-3125/120)]
B=[0; 0; 1;0 ;0 ;0]
solution= inv(A)*B

check=sum(solution)
  • After getting the values for CDS, skewed right and skewed left schemes.We can now write a code to compare and plot the errors.
  • The analytical or exact derivative will be

f\'\'(x)=(-2)*exp(x)*sin(x).

CODE FOR THE MAIN PROGRAM:

CODE FOR CDS, SRS and SLS

%coding in octave

%fourth order approximations
%given function
%f(x)=(exp(x)*cos(x));
x=pi/3;
dx=linspace(pi/40,pi/4000,20);

analytical_derivative= (-2)*exp(x)*sin(x);

for i=1:length(dx)
central_differencing(i)= ((-2.5*epx(x))+(1.334*epx(x-dx(i)))-(0.0833*epx(x-(2*dx(i))))+(1.334*epx(x+dx(i)))-(0.0833*epx(x+(2*dx(i)))))/(dx(i)^2);
skewed_left_differencing(i)=((3.75*epx(x))-(12.833*epx(x-dx(i)))+(17.833*epx(x-(2*dx(i))))-(13*epx(x-(3*dx(i))))+(5.0833*epx(x-(4*dx(i))))-(0.833*epx(x-(5*dx(i)))))/(dx(i)^2);
skewed_right_differencing(i)=((3.75*epx(x))-(12.833*epx(x+dx(i)))+(17.833*epx(x+(2*dx(i))))-(13*epx(x+(3*dx(i))))+(5.0833*epx(x+(4*dx(i))))-(0.833*epx(x+(5*dx(i)))))/(dx(i)^2);
end

%error calculation
central_error=abs(analytical_derivative-central_differencing);
skewed_left_error=abs(analytical_derivative-skewed_left_differencing);
skewed_right_error=abs(analytical_derivative-skewed_right_differencing);

loglog(dx,central_error,\'--r\',\'lineWidth\',2)
grid on
hold on
loglog(dx,skewed_left_error,\'b\',\'lineWidth\',2)
hold on
loglog(dx,skewed_right_error,\':g\',\'lineWidth\',2)

legend(\'central error\',\'skewed left error\',\'skewed right error\')
title(\'Comparison of error from different schemes\')
xlabel(\'dx values\')
ylabel(\'Error values\')

CODING FOR CDS, SRS, SLS, FDS and BDS:

%fourth order approximations
%given function
%f(x)=(exp(x)*cos(x));
x=pi/3;
dx=linspace(pi/40,pi/4000,20);

analytical_derivative= (-2)*exp(x)*sin(x);

for i=1:length(dx)
central_differencing(i)= ((-2.5*epx(x))+(1.334*epx(x-dx(i)))-(0.0833*epx(x-(2*dx(i))))+(1.334*epx(x+dx(i)))-(0.0833*epx(x+(2*dx(i)))))/(dx(i)^2);
skewed_left_differencing(i)=((3.75*epx(x))-(12.833*epx(x-dx(i)))+(17.833*epx(x-(2*dx(i))))-(13*epx(x-(3*dx(i))))+(5.0833*epx(x-(4*dx(i))))-(0.833*epx(x-(5*dx(i)))))/(dx(i)^2);
skewed_right_differencing(i)=((3.75*epx(x))-(12.833*epx(x+dx(i)))+(17.833*epx(x+(2*dx(i))))-(13*epx(x+(3*dx(i))))+(5.0833*epx(x+(4*dx(i))))-(0.833*epx(x+(5*dx(i)))))/(dx(i)^2);
Forward_difference(i)= ((epx(x+dx(i)))-(epx(x)))/dx(i);
backward_difference(i)= ((epx(x))-(epx(x-dx(i))))/dx(i);
end

%error calculation
central_error=abs(analytical_derivative-central_differencing);
skewed_left_error=abs(analytical_derivative-skewed_left_differencing);
skewed_right_error=abs(analytical_derivative-skewed_right_differencing);
forward_error=abs(analytical_derivative-Forward_difference);
backward_error=abs(analytical_derivative-backward_difference);

loglog(dx,central_error,\'--r\',\'lineWidth\',2)
grid on
hold on
loglog(dx,skewed_left_error,\'b\',\'lineWidth\',2)
hold on
loglog(dx,skewed_right_error,\':g\',\'lineWidth\',2)
hold on
loglog(dx,forward_error,\'linewidth\',2)
hold on 
loglog(dx,backward_error,\'linewidth\',2)

legend(\'central error\',\'skewed left error\',\'skewed right error\',\'forward_error\',\'backward_error\')
title(\'Comparison of error from different schemes\')
xlabel(\'dx values\')
ylabel(\'Error values\')

Here we took a function code because to reduce the complexity of the program which is given below

FUNCTION CODE:

function out =epx(x)
out=exp(x)*cos(x);
end
  • Calculating values from \'for\' loop and error values values are calculated for the skewed right, skewed left and CDS.
  • To calculate error terms for them we use \'absolute\' command.
  • Now we can plot to compare them.
  • Here we have used loglog plot because the values we got are very small.
  • Plot between dx and error terms.
  • For FDS and BDS just add the formulas and respective error terms in the program and plot them with loglog comand.
  • xlabel is used to give x-axis (dx values).
  • ylabel is used to give y-axis (error values).
  • legend command is used to denote the curves we got in the plot.
  • title command is used to give title to the appropriate plot.

RESULTS AND EXPLANATION:

  • By comparing the central, skewed right and skewed left schemes , we can see that central differencing scheme has higher error that skewed right and skewed left schemes.
  • Because the grid points are only 5, one is the center point and two are from left and right neighbouring points symentrically (p+q=5)
  • But skewed right and skewed left similar error trend because here p+q=6, where p-order of equation, q-accuracy required.
  • So if we have less number of points we will have higher error compared to higher number of points scheme.
  • Also we have to keep in mind that it also depends on the direction of entity under consideration for example

COMPARED WITH FDS&BDS:

  • Here the slopes of the CDS, skewed right and skewed left schemes are higher compared to the FDS and BDS.
  • Because the number of points taken are very low (i.,e one from center and one from the neighbouring right and left repectively).
  • So the error will be more for FDS and BDS for a second order derivative.

USES OF SKEWED SCHEMES:

  • Right skewed are used where the information travel from the source to system like flow over aerofoil wings.right

  • Left skewed are used when information travel from the sytem to the source like in pumps, compressor etc.

  • Depending upon these type of situations skewed schemes will give more accuracy than central differencing schemes.
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.