Challenge

Week 3.5 - Deriving 4th order approximation of a 2nd order derivative using Taylor Table method

AIM:

To derive the 4th order approximations of the second-order derivative.

Objective:

In this challenge we have derive the 4th order approximation of the 2nd order derivative.

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

After deriving these schemes, prove that your skewed schemes are fourth-order accurate i.e the thumb rule given below.

Once you have done this, write a program in MATLAB 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.

Provide a plot that compares the absolute error between the above-mentioned schemes. Finally, we have to describe why a skewed scheme is useful? What can a skewed

scheme do that a CD scheme cannot? These all are the terms are helps to solve this challenge.

Right skewed difference table hypothesis function: left skewed difference table hypothesis function:

∆x^2 ( d^2 u)/(dx^2 ) = af(i) + bf(i + 1) + cf(i + 2) + df(i + 3) + ef(i + 4) + hf(i + 5)

calculation of numerical stencil

O = 2, A=4

N = O+A = 2+4 =6

af(i)= a[f(i)]

bf(i + 1)= b[f(i)+ Δxf^' (i)+ (∆x^2 f ^'' (i))/2! + (∆x^2 f^''' (i))/3!+ (∆x^3 f^iv (i))/4! + (∆x ^2 f^v (i))/5!]

cf(i + 2)= c[f(i)+(2Δx) f ^' (i)+ ((2∆x)^2 f^'' (i))/2! + ((2∆x)^3 f^''' (i))/3!+ ((2∆x)^4 f^'v (i))/4!+((2∆x)^5 f^v (i))/5! ]

df(i + 3)= d[f(i)+(3Δx) f^' (i)+((3∆x)^2 f^'' (i))/2!+((3∆x)^3 f ^''' (i))/3!+((3∆x)^4 f ^iv (i))/4!+ ((3∆x)^5 f^v (i))/5! ]

ef(i + 4)= e[f(i)+(4Δx) f ^' (i)+((4∆x)^2 f^'' (i))/2!+((4∆x)^3 f^''' (i))/3!+((4∆x)^4 f ^iv (i))/4!+((4∆x)^5 f ^v (i))/5!]

hf(i + 5) = h[f(i)+(5Δx)f ^' (i)+((5∆x)^2 f^'' (i))/2!+((5∆x)^3 f ^''' (i))/3!+ ((5∆x)^4 f^iv (i))/4!+ ((5∆x)^5 f^v (i) )/5!]

d2f/dx2

f(x)dx

f’(x) dx

f’’(x) dx2

f’’’(x) dx3

fiv(x) dx4

fv(x) dx5

af(x)

a

0

0

0

0

0

bf(x+dx)

b

b

b/2

b/6

b/24

b/120

cf(x+2dx)

c

2c

4c/2

8c/6

16c/24

32c/120

df(x+3dx)

d

3d

9d/2

27d/6

81d/24

243d/120

ef(x+4dx)

e

4e

16e/2

64e/6

256e/24

1024e/120

hf(x+5dx)

h

5h

25h/2

125h/6

625h/24

3125h/120

∑

0

0

1

0

0

0

left skewed difference table hypothesis function:

∆x (d ^2 u )/(dx^2 ) = af(i - 5) + bf(i - 4) + cf(i - 3) + df(i - 2) + ef(i - 1) + hf(i)

Calculation of numerical stencil

O = 2, A=4

N = O+A = 2+4 =6

hf(i)= h[f(i)]

ef(i + 1)= e[f(i)+ Δxf^' (i)+ (∆x^2 f ^'' (i))/2! + (∆x^2 f^''' (i))/3!+ (∆x^3 f^iv (i))/4! + (∆x ^2 f^v (i))/5!]

df(i + 2)= d[f(i)+(2Δx) f ^' (i)+ ((2∆x)^2 f^'' (i))/2! + ((2∆x)^3 f^''' (i))/3!+ ((2∆x)^4 f^'v (i))/4!+((2∆x)^5 f^v (i))/5! ]

cf(i + 3)= c[f(i)+(3Δx) f^' (i)+((3∆x)^2 f^'' (i))/2!+((3∆x)^3 f ^''' (i))/3!+((3∆x)^4 f ^iv (i))/4!+ ((3∆x)^5 f^v (i))/5! ]

bf(i + 4)= b[f(i)+(4Δx) f ^' (i)+((4∆x)^2 f^'' (i))/2!+((4∆x)^3 f^''' (i))/3!+((4∆x)^4 f ^iv (i))/4!+((4∆x)^5 〖f 〗^v (i))/5!]

af(i + 5) = a[f(i)+(5Δx)f ^' (i)+((5∆x)^2 f^'' (i))/2!+((5∆x)^3 f ^''' (i))/3!+ ((5∆x)^4 f^iv (i))/4!+ ((5∆x)^5 f^v (i) )/5!]

This Taylor table is obtained by expanding the function using Taylor’s Series and considering the coefficients

d2f/dx2

f(x)dx

f’(x) dx

f’’(x) dx2

f’’’(x) dx3

fiv(x) dx4

fv(x) dx5

af(x-5dx)

a

-5a

25a/2

-125a/6

625a/24

-3125a/120

bf(x-4dx)

b

-4b

16b/2

-64b/6

256b/24

-1024b/120

cf(x-3dx)

c

-3c

9c/2

-27c/6

81c/24

-243c/120

df(x-2dx)

d

-2d

4d/2

-8d/6

16d/24

-32d/120

ef(x-dx)

e

-e

e/2

-e/6

e/24

-e/120

hf(x)

h

0

0

0

0

0

∑

0

0

1

0

0

0

Central skewed difference table hypothesis function:

∆x (d^2 u )/(dx^2 ) = af(i - 2) + bf(i - 1) + cf+ df(i+1) + ef(i+2)

Calculation of numerical stencil

O = 2, A=4

N = O+A -1= 2+4-1 =5

af(i-2)= a[f(i)+2Δxf^' (i)+ (2∆x^2 f ^'' (i))/2!+(2∆x^2 f^''' (i))/3!+(2∆x^3 f^iv (i))/4!]

bf(i-1)= b[f(i)+ Δxf^' (i)+ (∆x^2 f ^'' (i))/2! + (∆x^2 f^''' (i))/3!+ (∆x^3 f^iv (i))/4! ]

cf(i)= c[f(i)]

df(i + 1)= d[f(i)+ Δxf^' (i)+ (∆x^2 f ^'' (i))/2! + (∆x^2 f^''' (i))/3!+ (∆x^3 f^iv (i))/4! ]

ef(i + 2)= c[f(i)+(2Δx) f ^' (i)+ ((2∆x)^2 f^'' (i))/2! + ((2∆x)^3 f^''' (i))/3!+ ((2∆x)^4 f^'v (i))/4! ]

d2f/dx2

f(x)dx

f’(x) dx

f’’(x) dx2

f’’’(x) dx3

fiv(x) dx4

af(x-2dx)

a

-2a

4a/2

-8a/6

16a/24

bf(x-dx)

b

-b

b/2

-b/6

b/24

cf(x)

c

0

0

0

0

df(x+dx)

d

d

d/2

d/6

d/24

ef(x+2dx)

e

2e

4e/2

8e/6

16e/24

∑

0

0

1

0

0

Main code:

 clear all
 close all
 clc
 
 %inputs
 x=pi/3;
 
 %stepsize range
 dx=linspace(pi/4,pi/400,200);
 
 %Analytical derivative
 %f(x) = exp(x)*cos(x)
 %f'(x)=(exp(x)*cos(x)-(exp(x)*sin(x)=exp(x)*(cosx-sinx)
 %Double derivative f" = -2*exp(x)*sin(x)
 %Double derivative f = 2 exp(x) sin(x)
 analytical_derivative = -2*exp(x)*sin(x);
 
 %Skewed right-sided(4th order approimation)
 %Solving for the coefficients
 A=[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];
 B=[0;0;1;0;0;0];
 x_r=inv(A)*B;
 
 %Skewed left-sided(4th order approimation)
 %Solving for the coefficients
 C=[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];
 D=[0;0;1;0;0;0];
 x_l=inv(C)*D;
 
 %central differencing(4th order approimation)
 %Solving for the coefficients
 E=[1, 1, 1, 1, 1;-2, -1, 0, 1, 2;4/2, 1/2, 0, 1/2, 4/2;-8/6, -1/6, 0, 1/6, 8/6;16/24, 1/24, 0, 1/24, 16/24];
 F=[0;0;1;0;0];
 x_c=E^-1*F;
 
 %Anonymous Function
 f=@(x) exp(x).*cos(x);
 
 %numerical derivative
 right_difference=(x_r(1)*f(x)+x_r(2)*f(x+dx)+x_r(3)*f(x+2.*dx)+x_r(4)*f(x+3.*dx)+x_r(5)*f(x+4.*dx)+x_r(6)*f(x+5.*dx))./(dx.^2);
 left_difference=(x_r(1)*f(x)+x_r(2)*f(x-dx)+x_r(3)*f(x-2.*dx)+x_r(4)*f(x-3.*dx)+x_r(5)*f(x-4.*dx)+x_r(6)*f(x-5.*dx))./(dx.^2);
 cent_difference=(x_c(1)*f(x-2.*dx)+x_c(2)*f(x-1.*dx)+x_c(3)*f(x)+x_c(4)*f(x+dx)+x_c(5)*f(x+2.*dx))./(dx.^2);
 
 %absolute error
 error_r=abs(analytical_derivative-right_difference);
 error_l=abs(analytical_derivative-left_difference);
 error_c=abs(analytical_derivative-cent_difference);
 error=[error_r;error_l;error_c];
 figure(1)
 loglog(dx,error_r)
 grid on
 hold on
 loglog(dx,error_l)
 loglog(dx,error_c)
 ylabel('Absolute Error (Log Scale)')
 xlabel('Grid Spacing (dx in Log Scale)')
 legend('Right Differencing','Left Differencing','Central Differencing')

Code explanation:

  • First, we have to direct Numerical simulation (DNS) of turbulent flow.
  • And then we have to find Large Eddy Simulation (LES) of turbulent flow.
  • We have to find also acoustic calculations electromagnetic calculation (Maxwell Equation).
  • In this demonstration we will evaluate the 4th order approximation of different schemes for second derivative of the function exp(x)*cos(x) and plot that will compare the absolute errors between the schemes, comparison of the errors with FDS Abd BDS approximations.
  • Finally, to conclude, how skewed schemes are useful and benefit of it then central scheme MATLAB codes with required functions.

This image represents the fourth order approximation:

Conclusion:

Finally, we got a plot for fourth approximation and 2nd order difference central difference scheme is more accurate than skewed right side and skewed

left side scheme. The higher the order of the method the lower is the error is going to be for the same grid size. But in reality, when the geometry become very complex

and when the flow becomes very complex you cannot use higher order method always, and in order for the simulation to run the commercial codes always go to first order

schemes in order to get a solution.

Why Skewed schemes are useful?

This system which doesn’t consist sufficient information on both side of the point of interest. In such cases we cannot use central differencing scheme. so skewed scheme is useful in such cases. If we don’t have any nodes at the boundary then skewed schemes are useful to approximate to improve the accuracy but when we have internal nodes available then cantered ones are good though.

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.