## Computation Of 4th order Approximation For 2nd Order Derivative function Using Taylor Table In MATLAB

Deriving the 4th order approximation for 2nd order derivative of a function using Taylor table for centered, skewed right-sided stencil and skewed left_sided stencil and compare the error results for the same using MATLAB.

Given function,

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

The second_order derivative,  f''(x)  = -2*exp(x)*sin(x)

MATLAB Code for the finite difference schemes is as follows:-

% Forth - order approximation
% Second order ferivative
% Skewed- stencil (forward difference)
% Using nodes = 6

clear all
close all
clc

x = pi;

dx = linspace(pi/10,pi/300,100);

% Analytical function , f(x)     = exp(x)*cos(x)
% Analytical_derivative , f''(x) = -2*exp(x)sin(x)

Analytical_derivative = -(2*exp(x)* sin(x))

%Forward_Matrix_Scheme_for_4th_order_Approximation
% Matrix_form_of_the_Forward_Difference_linear_equation.

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 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];

c = a\b;  % Inverse matrix operation

% The_Coefficients_for_the_Forward_Difference_linear_equation.

% c_1 =  3.7500, c_2 = -12.8333, c_3 = 17.8333, c_4 = -13.0000, c_5 =  5.0833 , c_6 = -0.8333

% Forward_Difference_Scheme(FDS)_using_6_Nodes

% Skewed_right_handed_stencil.

for i = 1:length(dx)

FDS(i)      = ((3.7500*exp(x)*cos(x)) + (-12.8333*exp(x+dx(i))*cos(x+dx(i))) +  (17.8333*exp(x+2*dx(i))*cos(x+2*dx(i))) + (-13.0000*exp(x+3*dx(i))*cos(x+3*dx(i))) + (5.0833*exp(x+4*dx(i))*cos(x+4*dx(i))) + (-0.8333*exp(x+5*dx(i))*cos(x+5*dx(i))))/(dx(i)^2);

FDS_error   = abs(FDS - Analytical_derivative);

end

% END

% Backward_Difference_Scheme_For_4th_order_Approximation
% MAtrix_form_of_the_Backward_Difference_Linear_equation

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

N = [ 0;0;1;0;0;0]

O = M\N   % inverse operation of matrix

% Coefficients_for_the_backward_Difference_linear_equation.

% O_1 = -0.8333, O_2 = 5.0833, O_3 = -13.0000, O_4 = 17.8333, O_5 = -12.8333, O_6 = 3.7500

% Backward_difference_Scheme_using_6_nodes

% skewed_left_sided_sencil

for i = 1:length(dx)

BDS(i)      = ((-0.8333*exp(x-5*dx(i))*cos(x-5*dx(i))) + (5.0833*exp(x-4*dx(i))*cos(x-4*dx(i))) + (-13.0000*exp(x-3*dx(i))*cos(x-3*dx(i))) + (17.8333*exp(x-2*dx(i))*cos(x-2*dx(i))) +(-12.8333*exp(x-dx(i))*cos(x-dx(i))) + (3.7500*exp(x)*cos(x)))/(dx(i)^2)
BDS_error   = abs(BDS - Analytical_derivative);

end

% END

% Central_order_Scheme_4th_order_Approximation
% Matrix_form_for_Central_order_Difference_Scheme_linear_Equation

P = [ 1 1 1 1 1; -2 -1 0 1 2; 2 1/2 0 1/2 2;-8/6 -1/6 0 1/6 8/6; 16/24 1/24 0 1/24 16/24]

Q = [0;0;1;0;0]

R = P\Q    % Inverse_Matrix_Operation

% Coefficients_of_linear_equations

% R_1 = -0.08333, R_2 = 1.3333, R_3 = -2.5000, R_4 = 1.3333, R_5 = -0.08333

% Central_difference_Scheme_using_6_nodes.

% Symmetric_Stencil

for  i = 1:length(dx)

CDS(i)     =((-0.0833*exp(x-2*dx(i))*cos(x-2*dx(i))) + (1.3333*exp(x-dx(i))*cos(x-dx(i))) + (-2.5000*exp(x)*cos(x)) +  (1.3333*exp(x+dx(i))*cos(x+dx(i))) + (-0.0833*exp(x+2*dx(i))*cos(x+2*dx(i))))/(dx(i)^2)

CDS_error  = abs( CDS - Analytical_derivative)
end

% END

% plotting_the_the_error_of_three_schemes_against_step_size

loglog(dx, FDS_error,'g','linewidth',2);

hold on

loglog(dx, BDS_error,'b','linewidth',2);

loglog(dx, CDS_error,'r','linewidth',2);

hold off;

xlabel('Grid Size');
ylabel('Magnitude Of Error');

legend( '4th order FDS', '4th order BDS', '4th order CDS');

Step size vs Error

1. Deriving by a central difference scheme, 4th order approximation for 2nd order derivative.

The central difference approximation of 4th order approximation for 2nd order derivative using 5 nodes is given as :-

where

a,b,c,d and e are the coefficients which to obtained by Taylor series expansion.

Expanding each term using Taylor series and forming a table:-

The Taylor table:

f(i)   (△x) f'(x)     (△x)^2 f''(x)     (△x)^3 f'''(x)    (△x)^4 f''''(x)

a*f(i-2)      a        -2a                2a                  -8a/6              -32a/120

b*f(i-1)      b        -b                  b/2                 -b/2                 -b/120

c*f(i)         c          0                   0                     0                      0

d*f(i+1)    d          d                    d/2                 d/6                  d/120

e*f(i+2)    e          2e                  4e/2                8e/6                32e/120

Here, each column forms a linear equation. Here we have considered 5 nodes for 4th order approximation, hence there are 5 unknowns and 5 equations solved using matrix operation

Equation 1:    a + b + c + d + e = 0;

Equation 2:  -2a + (-b) + 0 + d + 2e = 0;

Equation 3:   2a + (-b/2) + d/2 + 2e = 0;

Equation 4:  -8a/6 + (-b/6) + 0 + d/6 + 8e/6 = 0;

Equation 5:  16a/24 + (b/24) + 0 + d/24 + 16e/24 = 0;

Note** :- The a, b, c, d and e coefficients in the linear equation are denoted as R_1, R_2, R_3, R_4 and R_5 in the matrix operation.

The matrix formed from the above equation is:-

Solving the matrix equation we get the coefficients as:

R =

-0.0833
1.3333
-2.5000
1.3333
-0.0833

>> 

Thus, our equation takes the form as:

2. Deriving by a Forward difference scheme, 4th order approximation for 2nd order derivative ( Right side stencil using 6 nodes)

The forward difference scheme of 4th order approximation for 2nd order derivative is given as :-

where a, b, c, d, e and f are the coefficients which are obtained by taylor series expansion.

Expanding each term in the Taylor table and forming a table:

The reason for considering 6 nodes is that,  higher the  order of approximation, more the differential terms in Taylor series is required and lesser the error is generated. Which means information is required from more number of nodes to reach the exact value.

The thumb rule is:

• First derivative with O(h) accuracy         = minimum number of nodes is 2.
• First derivative with O(h^2) accuracy     = need 3 nodes
• Second derivative with O(h) accuracy     = need 3 nodes.
• Second derivative with O(h^2) accuracy = need 4 nodes.
• Second derivative with O(h^3) accuracy = need 5 nodes.
• Second derivative with O(h^4) accuracy = need 6 nodes.

The Tayor table:

f(i)     (△x) f'(x)     (△x)^2 f''(x)     (△x)^3 f'''(x)     (△x)^4 f''''(x)     (△x)^5f'"(x)

a*f(i)         a          0                  0                      0                       0                         0

b*f(i+1)     b          b                 b/2                  b/6                    b/24                   b/120

c*f(i+2)      c         2c                4c/2                 8c/6                 16c/24                32c/120

d*f(i+3)     d         3d                9d/2                27d/6                81d/24               243d/120

e*f(i+4)     e          4e               16e/2               64e/6               256e/24            1024e/120

f*f(i+5)      f           5f               25f/2               125f/6               625f/24             3125f/120

Here, each column forms a linear equation. Here we have considered 6 nodes for 4th order approximation, hence there are 6 unknowns and 6 equations solved using matrix operation.

Equation 1:  a + b + c + d + e + f = 0

Equation 2:  0 + b + 2c + 3d + 4e + 5f = 0

Equation 3:  0 + b/2 + 4c/2 + 9d/2 + 16e/2 + 25f/2 = 0

Equation 4:  0 + b/6 + 8c/6 + 27d/6 + 64e/6 + 125f/6 = 0

Equation 5:  0 + b/24 + 16c/24 + 81d/24 + 256e/24 + 625f/24 = 0

Equation 6:  0 + b/120 + 32c/120 + 243d/120 + 1024e/120 + 3125f/120 = 0

Note** :- The a, b, c, d, e and f coefficients in the linear equation are denoted as c_1, c_2, c_3, c_4, c_5 and c_6 in the matrix operation.

The matrix formed from the above equation is:

After solving the above matrix, we obtain the coefficients as

c =

3.7500
-12.8333
17.8333
-13.0000
5.0833
-0.8333

>> 

Thus, our general equation takes the form as:

3. Deriving by a Backward difference scheme, 4th order approximation for 2nd order derivative ( Left side stencil using 6 nodes)

The forward difference scheme of 4th order approximation for 2nd order derivative is given as :-

where, a,b,c,d,e and f are the coefficients of the discretized algebric equation.

In the Backward difference scheme, 5 nodes has been chosen in the left hand side of the point of derivative of the function making it a skewed left-handed stencil.

The Taylor table:

f(i)     (△x) f'(x)     (△x)^2 f''(x)     (△x)^3 f'''(x)     (△x)^4 f''''(x)     (△x)^5f'"(x)

a*f(i-5)      a         -5a              25a/2               -125a/6            625a/24          -3125a/120

b*f(i-4)      b         -4b              16b/2               -64b/6             256b/24           -1024b/120

c*f(i-3)       c        -3c               9c/2                 -27c/6              81c/24             -243c/120

d*f(i-2)      d        -2d               4d/2                 -8d/6               16d/24              -32d/120

e*f(i-1)      e         -e                 e/2                   -e/6                 e/24                 -e/120

f*f(i)          f          0                  0                        0                     0                        0

Here, each column forms a linear equation. Here we have considered 6 nodes for 4th order approximation, hence there are 6 unknowns and 6 equations solved using matrix operation.

Equation 1:     a + b + c + d + e + f = 0

Equation 2:   -5a - 4b - 3c - 2d - e - 0 = 0

Equation 3:    25a/2 + 16b/2 + 9c/2 + 4d/2 + e/2 + 0 = 0

Equation 4:  -125a/6  - 64b/6 - 27c/6 - 8d/6 - e/6 + 0 = 0

Equation 5:    625a/24 + 256b/24 + 81c/24 + 16d/24 + e/24 + 0 = 0

Equation 6:   -3215a/120 - 1024b/120 - 243c/120 - 32d/120 - e/120 + 0 = 0

Note** :- The a, b, c, d, e and f coefficients in the linear equation are denoted as O_1, O_2, O_3, O_4, O_5 and O_6 in the matrix operation.

The matrix formed fom the above linear equation is-

After solving the matrix using operation O =M\N in MATLAB, we get the coefficient as

O =

-0.8333
5.0833
-13.0000
17.8333
-12.8333
3.7500

>> 

Thus ,the general equation for the backward difference scheme becomes:-

INFERENCE FROM RESULTS

1. The highest absolute error is obtained in FDS (right_hand_stencil) and BDS(left_hand_stencil) scheme whereas minimum error is obtained from CDS(symmetric__stencil). CDS scheme takes values equally from both side of the stencil and hence a balanced system is taken into account for approximation.
2. The FDS(Right_stencil) and BDS(left_stencil) takes account  nodes ony from one side, and thus an unbalanced system is taken into account for 4th order approximation. When higher order of approximation is taken into account and higher the grid size (dx) is taken, then the error magnitude also increases. hence, CDS is considered better than FDS and BDS.
3. But one major potential drawback of CD scheme is that when a corner stencil is taken into consideration or a node in the boundary is taken, then symmetric stencil is not possible and only skewed schemes are possible. Skewed schemes can be implemented any time and nodes availability which is not possible in CD schemes. Hence Skewed schemes are more useful.

### Meshing of an automotive hood for structural analysis Ankit Chakrabarty · 2019-12-27 04:07:31

Title:- Geometry cleanup and meshing of an automotive hood for structural analysis   Objective:  Perform geometry cleanup of automotive hood (both inner and outer panel). Perform mid-surface extraction of both the panels and Hem both the surfaces . Perf Read more

### Solving A Quasi-1 Dimensional Supersonic Incompressible Isentropic Nozzle Flow Using Maccormack Explict Method In MATLAB Ankit Chakrabarty · 2019-10-14 08:26:31

Abstract: This project presents the Modelling of the  behaviour of  fluid and its properties when it passes through a convergent-divergent nozzle using a Finite Difference Method. However, to simplify the understanding of the dynamics of the flow in the nozz Read more

### Case Study: Thermodynamic Performance Analysis Of a Twin-scroll Turbocharger vs Single-Scroll Turbocharger Using GT-Power. Ankit Chakrabarty · 2019-09-22 18:00:53

Turbocharger: Literature Review: A turbocharger is mechanical rotating device which forcebly induces air at high pressure into the internal combustion engine, in turn increases the power and effeciency (thermal and volumetric effeciency) of the engine. A turb Read more

### Effect Of Spectral Radius And Diagonal Magnification In The Convergence Rate Of An Iterative Solver For A Linear System Using MATLAB. Ankit Chakrabarty · 2019-09-20 17:47:52

(A) Problem defination:- Solve the following Linear equation using Gauss-Jacobi, Gauss-Seidal and SOR iterative method and understand the following parameters:- Spectral radius and the impact of spectral radius on the convergence of the solution. Effect of d Read more

### CASE STUDY:- A Thermodynamic Analysis Of A Tractor EngineFor Use In India With Given Engine Performance Targets. Ankit Chakrabarty · 2019-09-09 06:55:40

CASE STUDY:-  A Thermodynamic Analysis Of A Tractor Engine For Use In India With Given Engine Performance Targets. A Brief Literature Review: A tractor is an engineered vehicle specifically designed to deliver Power and traction/tractive effort to haul heavy equi Read more

### Solving 2-D 2nd Order Linear Transient &Steady  Heat Conduction Equation Using Both Explicit And Implicit Numerical Schemes In MATLAB. Ankit Chakrabarty · 2019-09-06 05:15:36

Solving 2-D 2nd Order Linear Transient &Steady  Heat Conduction Equation Using Both Explicit And Implicit Numerical Schemes In MATLAB.   Explicit Scheme : We say that a scheme is explit when the information at time level "n+1" depends on previous time Read more

### CASE STUDY: Effect of Fuel Burn Rates on Engine Performance Of A 6-Cyl-Turbocharged-DI Engine Using A Non-predictive Combustion model (DI WIEBE) Ankit Chakrabarty · 2019-08-23 20:03:11

CASE STUDY: Effect of Burn Rate on engine performance Of A 6-Cyl-Turbocharged-DI Engine Using A Non-predictive Combustion model (DI WIEBE) Abstract: Analytical functions approximating the burn rate in internal combustion engines are useful and cost-effective to Read more

### Solving 1-D Linear Convection Using First-Order Backward Difference And Forward Difference Numerical Schemes Using MATLAB Ankit Chakrabarty · 2019-08-12 10:58:54

Solving 1-D Linear Convection Using First-Order Backward Difference And Forward Difference method Using MATLAB LITERATURE REVIEW: WHAT IS CONVECTION? Convection is the sum of bulk transport of the  fluid  and brownian/ osmotic dispersion of fluid constituen Read more

### Single Cylinder Spark Plug Engine Calibration at 3600 rpm for Higher Load Applications And Lower NOx And Other Emmisions using GT-Power Ankit Chakrabarty · 2019-07-25 10:55:20

Objective:- (1) Determine the engine parameters running at 1800 rpm and record the same. (2) Further increase the power output obtained at 3600 rpm by 10%.   Model:-  The GT model Of the 1-cylinder engine is shown below:- Observations (@1800rpm):-  Read more