Project

4TH ORDER APPROXIMATION FOR A 2ND ORDER DERIVATIVE FOR A GIVEN FUNCTION USING TAYLOR S TABLE

\"SKEWED_DIFFERENCING\"

clear all 
close all
clc
x=pi/3;
dx=linspace(pi/4,pi/4000,10);
for i=1:length(dx)
    
    skewed_forward_differencing(i)=skewed_forward(x,dx(i));
    skewed_backward_differencing(i)=skewed_backward(x,dx(i));
    center_differencing_error(i)=center_differencing(x,dx(i));
    
end
figure(1)
loglog(dx,skewed_forward_differencing,\'*\',\'color\',\'b\');
hold on
loglog(dx,skewed_backward_differencing,\'color\',\'r\');
hold on
loglog(dx,center_differencing_error,\'color\',\'k\');
legend(\'Skewed Forward Differencing\',\'Skewed Backward Differencing\',\'Center Differencing\')
xlabel(\'dx\')
ylabel(\'ERROR\')

FUNCTION 1 :
function out= skewed_forward(x,dx)
%forward skewed equations
%a1+b1+c1+d1+e1+h1=0
%b1+2c1+3d1+4e1+5h1=0
%b1/2+2c1+9/2d1+16/2e2+25/2h2=1
%b1/6+8/6c1+27/6d1+64/6e2+125/6h2=0
%b1/24+16/24c1+81/24d1+256/24e1+625/24h1=0
%b1/120+32/120c1+243/120d1+1024/120e1+3125/120h1

a1=[ 1  1      1      1       1        1
    0   1      2      3       4        5
    0   1/2   4/2    9/2     16/2     25/2
    0   1/6   8/6    27/6    64/6      125/6
    0   1/24  16/24  81/24   256/24    625/24
    0   1/120 32/120 243/120 1024/120 3125/120];


c1=[0
   0
   1
   0
   0
   0];
a0=inv(a1)*c1;
%orginal value= -2*e^x*sin(x)

skewed_forward_differencing=a0(1)*exp(x)*cos(x)+a0(2)*exp(x+dx)*cos(x+dx)+a0(3)*exp(x+2*dx)*cos(x+2*dx)+a0(4)*exp(x+3*dx)*cos(x+3*dx)+a0(5)*exp(x+4*dx)*cos(x+4*dx)+a0(6)*exp(x+5*dx)*cos(x+5*dx);
original_value=-2*exp(x)*sin(x);
out=abs(skewed_forward_differencing-original_value);
end

FUNCTION 2 :

function out= skewed_backward(x,dx)
%backward skewed coefficients
%  a1+b1+c1+d1+e1+h1=0
%  -[b1+2c1+3d1+4e1+5h1]=0
%  b1/2+2c1+9/2d1+16/2e2+25/2h2=1
% -[b1/6+8/6c1+27/6d1+64/6e2+125/6h2]=0
%  b1/24+16/24c1+81/24d1+256/24e1+625/24h1=0
% -[b1/120+32/120c1+243/120d1+1024/120e1+3125/120h1]=0

a2=[ 1   1       1        1          1           1
    0   -1      -2       -3         -4          -5
    0   1/2     4/2      9/2       16/2         25/2
    0  -1/6    -8/6     -27/6      -64/6       -125/6
    0   1/24   16/24     81/24     256/24      625/24
    0  -1/120  -32/120  -243/120  -1024/120  -3125/120];


c2=[0
   0
   1
   0
   0
   0];
b0=inv(a2)*c2;
%orginal value= -2*e^x*sin(x)

skewed_backward_differencing=b0(1)*exp(x)*cos(x)+b0(2)*exp(x-dx)*cos(x-dx)+b0(3)*exp(x-2*dx)*cos(x-2*dx)+b0(4)*exp(x-3*dx)*cos(x-3*dx)+b0(5)*exp(x-4*dx)*cos(x-4*dx)+b0(6)*exp(x-5*dx)*cos(x-5*dx);
original_value=-2*exp(x)*sin(x);
out=abs(skewed_backward_differencing-original_value);
end


fUNCTION 3:

function out= center_differencing(x,dx)
% center differenncing coefficients

a3=[ 1    1       1       1         1          
    -2   -1       0       1         2         
     2   1/2      0       1/2       2   
   -4/3  -1/6     0       1/6      4/3  
   16/24  1/24    0      1/24     16/24 ];


c3=[0
   0
   1
   0
   0];
c0=inv(a3)*c3;
%orginal value= -2*e^x*sin(x)

center_differencing_error=c0(1)*exp(x-2*dx)*cos(x-2*dx)+c0(2).*exp(x-dx)*cos(x-dx)+c0(3).*exp(x)*cos(x)+c0(4).*exp(x+dx)*cos(x+dx)+c0(5).*exp(x+2*dx)*cos(x+2*dx);
original_value=-2*exp(x)*sin(x);
out=abs(center_differencing_error-original_value);
end

Problem: Derive the following 4th order approximations of the second-order derivative.

  1. Central difference
  2. Skewed right-sided difference
  3. Skewed left-sided difference

Soln.

1.Central differencing: The stencil for the central differncing can be calculated from :

Number of terms in series= p+q-1 for central differencing

P+q for forward and backward differencing.

p-order of approximation

q-order of derivative.

So as per the problem number of terms in central differencing is 4+2-1=5 terms.So the stencil is taken a i-2,i-1,i,i+1,i+2.

Lets have a hypothesis equation:

d2u/dx2 = af(i-2)+bf(i-1) + cfi+df(i+1)+ef­(i+2)…………………………………….eq-1

d2u/dx2 = a*exp(x-2*dx)*cos(x-2*dx)+b* exp(x-dx)*cos(x-dx)+ c*exp(x)*cos(x)+ d*exp(x+dx)*cos(x+dx)+ e*exp(x+2*dx)*cos(x+2*dx)

now by taylors table

f(x)

f’(x) * Δx

f’’(x) * Δx2

f’’’(x) * Δx3

f’’’ (x) * Δx4

f’’’’ (x) * Δx5

f’’’’’’(x) * Δx6

af(i-2

a

-2a

4a/2!

-8a/3!

16a/4!

-32a/5!

64a/6!

bf(i-1)

b

-b

b/2!

-b/3!

b/4!

-b/5!

d/6!

cfi

c

0

0

0

0

0

0

df(i+1

d

d

d/2!

d/3!

d/4!

d/5!

d/6!

ef­(i+2

e

2e

4e/2!

8e/3!

16e/4!

32e/5!

64e/6!

0

0

1

0

0

a+b+c+d+e = 0

-2a-b+d+2e= 0

2a+b/2+d/2+2e = 1

-8/6a-b/6+d/6+8e/6 = 0

16a/24+b/24+d/24+16e/24 = 0

By solving the above equations we get the coefficients of the hypothesis equation .By solving the hypothesis equation we get the central differencing.

After solving the equations we get

a=-0.0833

b= 1.3333

c= -2.5000

d= 1.3333

e=-0.0833 these values are from matlab after running the code.

We can say that a=e and b=d so the fifth row in taylors table is zero because the coefficients are same but opposite sign.

But the sixth row is not equal to zero let this called as O(…)Δx6

So finally we have two terms in RHS

d2u/dx2 = O(…..)Δx2+O(….)Δx6

Dividing RHS by Δx2

LHS= O(…)Δx2/Δx2 + O(….)Δx6/Δx2

LHS= O(…)+ O(….)Δx4

From above equation we observe that the order of Δx is 4. So this is called 4Th order truncation error or 4th order approximation.

Similarly for

2.Skewed forward differencing: The stencil is i,i+1,i+2,i+3,i+4,i+5

d2u/dx2 = af(i)+bf(i+1) + cf(i+2)+df(i+3)+ef­(i+4)+ hf(i+5)…………………………….eq-1

d2u/dx2 = a*exp(x)*cos(x)+b* exp(x+dx)*cos(x+dx)+ c*exp(x+2dx)*cos(x+2dx)+ d*exp(x+3dx)*cos(x+3dx)+ e*exp(x+4*dx)*cos(x+4*dx)+ h*exp(x+5*dx)*cos(x+5*dx)

From Taylor table the equations are

a+b+c+d+e+h= 0

b+2c+3d+4e+5h=0

b/2+2c+9d/2+16e/2+25h/2=1

b/6+8c/6+27d/6+64e/6+125h/6 = 0

b/24+16c/24+81d/24+256e/24+625h/24 = 0

b/120+32c/120+243d/120+1024e/120+3125h/120 = 0

by solving the equations we get the coefficients in hypothesis equation for 4th order approximation of second derivative.

3.Skewed Backward differencing: The stencil is i-5,i-4,i-3,i-2,i-1,i

d2u/dx2 = af(i)+bf(i-1) + cf(i-2)+df(i-3)+ef­(i-4)+ hf(i-5)…………………………….eq-1

d2u/dx2 = a*exp(x)*cos(x)+b* exp(x-dx)*cos(x-dx)+ c*exp(x-2dx)*cos(x-2dx)+ d*exp(x-3dx)*cos(x-3dx)+ e*exp(x-4*dx)*cos(x-4*dx)+ h*exp(x-5*dx)*cos(x-5*dx)

From Taylor table the equations are

a+b+c+d+e+h= 0

-[b+2c+3d+4e+5h]=0

b/2+2c+9d/2+16e/2+25h/2=1

-[b/6+8c/6+27d/6+64e/6+125h/6 ]= 0

b/24+16c/24+81d/24+256e/24+625h/24 = 0

-[b/120+32c/120+243d/120+1024e/120+3125h/120] = 0

by solving the equations we get the coefficients in hypothesis equation for 4th order approximation of second derivative.

Plot:

Conclusions from plot:

1)The Error in CDS is more accurate than Skewed-FDS and Skewed-BDS.

2)As the dx value decreases all the schemes shows the same error value.

Conclusions:

1.We can say that CDS is more accurate than the skewed FDS and BDS system because in CDS points are taken symmetrically nearer to the required point but in FDS and BDS they are taken far from the required point.

2.But still FDS and BDS are used in few applications like fluid flow in pipe or heat conduction in a boiler wall etc.In this system we cannot use CDS because the flow of energy is in one direction.

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.