Menu

Executive Programs

Workshops

Projects

Blogs

Careers

Placements

Student Reviews


For Business


More

Academic Training

Informative Articles

Find Jobs

We are Hiring!


All Courses

Choose a category

Loading...

All Courses

All Courses

logo

Loading...
Executive Programs
Workshops
For Business

Success Stories

Placements

Student Reviews

More

Projects

Blogs

Academic Training

Find Jobs

Informative Articles

We're Hiring!

phone+91 9342691281Log in
  1. Home/
  2. Dushyanth Srinivasan/
  3. Week 3.5 - Deriving 4th order approximation of a 2nd order derivative using Taylor Table method

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

1. Central Difference To calculate number of nodes for central differencing, order of accuracy P = Number of nodes in the stencil N - order of derivative Q + 1 we know, P = 4, Q = 2. Solving for N we get N = 5 Let d2fdx2=af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)d2fdx2=af(i−2)+bf(i−1)+cf(i)+df(i+1)+ef(i+2) we have to create a taylor's…

  • CFD
  • MATLAB
  • Dushyanth Srinivasan

    updated on 06 Oct 2021

1. Central Difference

To calculate number of nodes for central differencing,

order of accuracy P = Number of nodes in the stencil N - order of derivative Q + 1

we know, P = 4, Q = 2. Solving for N we get N = 5

Let d2fdx2=af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)

we have to create a taylor's table for the same

  f(i) Δxf′(i) Δx2f′′(i) Δx3f′′′(i) Δx4f′′′′(i)
af(i-2) a -2a 4a/2 -8a/6 16a/24
bf(i-1) b -b b/2 -b/6 b/24
cf(i) c 0 0 0 0
df(i+1) d d d/2 d/6 d/24
ef(i+2) e 2e 4e/2 8e/6 16e/24
  0 0 1 0 0

 

 

 

 

 

 

 

 

 

Adding the vertical columns we get,

a+b+c+d+e=0

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

2a+b2+d2+2e=1

-8a6-b6+d6+8e6=0

16a24+b24+d24+16e24=0

Simplyfying some equations and arranging them into matrix form, we get

[11111-2-10122120122-8-10181610116][abcde]=[00100]

Solving this in MATLAB using the below code,

a=[1 1 1 1 1;-2 -1 0 1 2; 2 0.5 0 0.5 2;-4/3 -1/6 0 1/6 4/3; 2/3 1/24 0 1/24 2/3;]
b = [0 0 1 0 0]'
format rational
inv(a)*b

format rational was used to make sure the result is in p/q form else it may cause rounding errors later on

we get the resultant matrix which provides the solution or values of a, b, c, d and e

a=-112

b=43

c=-52

d=43

e=-112

Adding the 4th vertical column from Taylor's Table and we get: f′′(i)=d2fdx2=af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)Δx2

2. Right Skewed

To calculate number of nodes for right skewed differencing,

order of accuracy P = Number of nodes in the stencil N - order of derivative Q

we know, P = 4, Q = 2. Solving for N we get N = 6

Let d2fdx2=af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5)

now, a taylor's table for the same

  f(i) Δxf′(i) Δx2f′′(i) Δx3f′′′(i) Δx4f′′′′(i) Δx5f′′′′′(i)
af(i) a 0 0 0 0 0
bf(i+1) b b b/2 b/6 b/24 b/120
cf(i+2) c 2c 4c/2 8c/6 16c/24 32c/120
df(i+3) d 3d 9d/2 27d/6 81d/24 243d/120
ef(i+4) e 4e 16e/2 64e/6 256e/24 1024e/120
gf(i+5) g 5g 25g/2 125g/6 625g/24 3125g/120
  0 0 1 0 0 0

 

 

 

 

 

 

 

 

 

Adding the vertical columns we get,

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

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

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

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

b/24+16c/24+81d24+256e/24+625g/24=0

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

Simplyfying some equations and arranging them into matrix form, we get

[111111012345014916250182764125011681256625013224310243125][abcdeg]=[002000]

Solving this in MATLAB using the below code,

a=[1 1 1 1 1 1;0 1 2 3 4 5;0 1 4 9 16 25; 0 1 8 27 64 125; 0 1 16 81 256 625; 0 1 32 243 1024 3125;]
b = [0 0 2 0 0 0]'
format rational
inv(a)*b

format rational was used to make sure the result is in p/q form else it may cause rounding errors later on

we get the resultant matrix which provides the solution or values of a, b, c, d, e and g

a=154

b=-776

c=-1076

d=-13

e=6112

g=-56

Adding the 4th vertical column from Taylor's Table and we get: f′′(i)=d2fdx2=af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5)Δx2

3. Left Skewed

To calculate number of nodes for right skewed differencing,

order of accuracy P = Number of nodes in the stencil N - order of derivative Q

we know, P = 4, Q = 2. Solving for N we get N = 6

d2fdx2=af(i)+bf(i-1)+cf(i-2)+df(i-3)+ef(i-4)+gf(i-5)

now, a taylor's table for the same

  f(i) Δxf′(i) Δx2f′′(i) Δx3f′′′(i) Δx4f′′′′(i) Δx5f′′′′′(i)
af(i) a 0 0 0 0 0
bf(i-1) b -b b/2 -b/6 b/24 -b/120
cf(i-2) c -2c 4c/2 -8c/6 16c/24 -32c/120
df(i-3) d -3d 9d/2 -27d/6 81d/24 -243d/120
ef(i-4) e -4e 16e/2 -64e/6 256e/24 -1024e/120
gf(i-5) g -5g 25g/2 -125g/6 625g/24 -3125g/120
  0 0 1 0 0 0

 

 

 

 

 

 

 

 

 

 

Adding the vertical columns we get,

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

-b-2c-3d-4e-5g=0

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

-b/6-8c/6-27d/6-64e/6-125g/6=0

b/24-16c/24-81d24-256e/24-625g/24=0

b/120-32c/120-243d/120-1024e/120-3125g/120=0

Simplyfying some equations and arranging them into matrix form, we get

[111111012345014916250182764125011681256625013224310243125][abcdeg]=[002000]

Solving this in MATLAB using the below code,

a=[1 1 1 1 1 1;0 1 2 3 4 5;0 1 4 9 16 25; 0 1 8 27 64 125; 0 1 16 81 256 625; 0 1 32 243 1024 3125;]
b = [0 0 2 0 0 0]'
format rational
inv(a)*b

format rational was used to make sure the result is in p/q form else it may cause rounding errors later on

we get the resultant matrix which provides the solution or values of a, b, c, d, e and g

a=154

b=-776

c=-1076

d=-13

e=6112

g=-56

 Adding the 4th vertical column from Taylor's Table and we get: f′′(i)=d2fdx2=af(i)+bf(i-1)+cf(i-2)+df(i-3)+ef(i-4)+gf(i-5)Δx2

Calculation of Derivative

f(x)=excosx

f′(x)=excosx-exsinx - First Derivative

f′′(x)=excosx-exsinx-ecosx-exsinx

f′′(x)=-2exsinx Second Derivative

 Code with explanation

filename: left_skew_difference.m
function error = left_skew_difference (m,analytical_second_derivative,x,dx)

% f(x)= e^x * cos(x)
% f''(x) = -2 e^x sin(x)
% left skew difference: f''(x) = a f(i) + b f(i-1) + c f(i-2)+ d f(i-3)+ e f(i-4)+ g f(i-5)
% where, a = 15/4 b=-77/6 c=-107/6 d= -13 e=61/12 g=-5/6
left_skew_derivative = (m(1)*exp(x)* cos (x) + m(2) * exp(x-dx)* cos (x-dx)+m(3)*exp(x-(2*dx))* cos (x-(2*dx))+m(4) * exp(x-(3*dx))* cos (x-(3*dx)) + m(5)* exp(x-(4*dx))* cos (x-(4*dx)) + m(6)* exp(x-(5*dx))* cos (x-(5*dx)))/(dx^2);
error = abs(left_skew_derivative-analytical_second_derivative);

end

----------
filename:right_skew_difference.m
function error = right_skew_difference (m,analytical_second_derivative,x,dx)

% f(x)= e^x * cos(x)
% f''(x) = -2 e^x sin(x)
% right skew difference f''(x) = a f(i) + b f(i+1) + c f(i+2)+ d f(i+3)+ e f(i+4)+ g f(i+5)
% where, a= 15/4, b = -77/6. c = -107/6, d=-13, e = 61/12, g = -5/6
right_skew_derivative = (m(1)*exp(x)* cos (x) + m(2) * exp(x+dx)* cos (x+dx)+m(3)*exp(x+(2*dx))* cos (x+(2*dx))+ m(4)* exp(x+(3*dx))* cos (x+(3*dx)) + m(5)* exp(x+(4*dx))* cos (x+(4*dx)) + m(6)* exp(x+(5*dx))* cos (x+(5*dx)))/(dx^2);
error = abs(right_skew_derivative-analytical_second_derivative);

end
----------
filename:central_difference.m
function error = central_difference (m,analytical_second_derivative,x,dx)

% f(x)= e^x * cos(x)
% f''(x) = -2 e^x sin(x)
% central difference f''(x) = a f(i-2) + b f(i-1) + c f(i)+ d f(i+1)+ e f(i+2)
% where, a = -1/12 b=4/3 c=-5/2 d=4/3 e=-1/12
central_derivative = (m(1)*exp(x-(2*dx))* cos (x-(2*dx)) +m(2)*exp(x-dx)*cos (x-dx) + m(3)*exp(x)* cos (x) + m(4)*exp(x+dx) * cos(x+dx) + m(5)*exp(x+(2*dx))*cos(x+(2*dx)))/(dx^2);
error = abs(central_derivative-analytical_second_derivative);

end
----------
% main program

clear all
close all
clc

format rational % to make sure rounding-off errors are minimised since we are using very small numbers

% calculating the constants for each scheme and storing in m_l,m_r and m_c
a=[1 1 1 1 1 1;0 1 2 3 4 5;0 1 4 9 16 25; 0 1 8 27 64 125; 0 1 16 81 256 625; 0 1 32 243 1024 3125;];
b = [0 0 2 0 0 0]';
m_l = inv(a)*b

a=[1 1 1 1 1 1;0 1 2 3 4 5;0 1 4 9 16 25; 0 1 8 27 64 125; 0 1 16 81 256 625; 0 1 32 243 1024 3125;];
b = [0 0 2 0 0 0]';
m_r = inv(a)*b

a=[1 1 1 1 1;-2 -1 0 1 2; 2 0.5 0 0.5 2;-4/3 -1/6 0 1/6 4/3; 2/3 1/24 0 1/24 2/3;];
b = [0 0 1 0 0]';
m_c = inv(a)*b

% function given f(x)= e^x * cos(x)
x = pi; 
dx = linspace(pi/4000,pi/3,4000); % 4000 values of dx from pi/3 to pi/4000

% analytical second derivative: f''(x) = -2 e^x sin(x)
analytical_second_derivative = -2 * exp(x) * sin(x);

% calculating left skew, right skew and central difference error for x=pi
% for each value of dx
for i = [1:length(dx)]
    
    % numerical differencing - decided to send exact derivative as a parameter so as to not calculate everytime
    
    left_skew_error (i)= left_skew_difference (m_l, analytical_second_derivative,x,dx(i));
    
    right_skew_error(i) = right_skew_difference (m_r, analytical_second_derivative,x,dx(i));
    
    central_error (i)= central_difference (m_c, analytical_second_derivative,x,dx(i));
    
end

%plotting error vs dx on a loglog plot with appropiate legends and labels
figure(1)
loglog(dx,left_skew_error,'blue')
hold on
loglog(dx,right_skew_error,'red')
hold on
loglog(dx,central_error,'black')
legend('Left Side Skewed Difference','Right Side Skewed Difference','Central Difference')
xlabel ('dx')
ylabel ('Error')

 Output of Code

The output is a graph of the absolute errors of the 3 schemes (left, right and central) to 4000 values of dx from pi/3 to pi/4000. The graph is on a log scale for easier viewing. As expected, central difference scheme has a significantly lower error than the skewed schemes. Right and Left schemes are similar since they are almost the same for a lot of values of dx. All errors decrease with decreasing dx untill a certain point when other errors such as truncation error, roundoff errors creep in and make the solution unstable.

Q. Why a skewed scheme is useful? What can a skewed scheme do that a CD scheme cannot?

Although a central difference scheme provides lower errors for the same values of dx. Skewed schemes are useful at the boundaries of equation, which is very common in CFD (boundary conditions), a CD scheme cannot be applied at boundary conditions while a skewed scheme can be applied with relatively less error.

Leave a comment

Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.

Please  login to add a comment

Other comments...

No comments yet!
Be the first to add a comment

Read more Projects by Dushyanth Srinivasan (45)

Project 2 - Rankine cycle Simulator

Objective:

  In this project, I will be writing code in MATLAB to simulate a Rankine Cycle for the given parameters. A Rankine Cycle is an ideal thermodynamic heat cycle where mechanical work is extracted from the working fluid as it passes between a heat source and heat sink. This cycle or its derivatives is used in steam engines…

calendar

04 Sep 2022 12:52 PM IST

  • MATLAB
Read more

Project 1 - Parsing NASA thermodynamic data

Objective:

In this project, I will be parsing a data file prepared by NASA. The contents of the data file can be used to generated thermodynamic properties such as Specific Heat at Constant Pressure 'C_p' (J/(kg.K)), Enthalpy HorQ (J) and Entropy S (J/(kg.mol)) at various temperatures. The files will be parsed in MATLAB…

calendar

31 Aug 2022 01:07 PM IST

  • MATLAB
Read more

Week 5 - Genetic Algorithm

Objective:

In this project, I will be generating a stalagmite function in MATLAB and find the global maxima of the function using Genetic Algorithm. A stalagmite function is a function which generates Stalactites, which are named after a natural phenomenon where rocks rise up from the floor of due to accumulation of droppings of…

calendar

29 Aug 2022 07:55 AM IST

  • MATLAB
Read more

Week 4.1 - Solving second order ODEs

Objective:

In this project, I will be writing code in MATLAB to solve the motion of a simple pendulum. A simple pendulum motion's depends on Newton's Second Law. The equation which governs the motion of a simple pendulum is (with damping) d2θdt2+bmdθdt+gLsinθ=0 Where, θ is the angular displacement…

calendar

23 Aug 2022 08:06 AM IST

  • MATLAB
Read more

Schedule a counselling session

Please enter your name
Please enter a valid email
Please enter a valid number

Related Courses

coursecardcoursetype

Post Graduate Program in CFD Solver Development

4.8

106 Hours of Content

coursecardcoursetype

Post Graduate Program in Computer Vision for Autonomous Vehicles

4.7

223 Hours of Content

coursecard

Introduction to OpenFOAM Development

4.9

18 Hours of Content

coursecardcoursetype

Post Graduate Program in Battery Technology for Mechanical Engineers

4.8

57 Hours of Content

coursecardcoursetype

Post Graduate Program in Autonomous Vehicles

Recently launched

88 Hours of Content

Schedule a counselling session

Please enter your name
Please enter a valid email
Please enter a valid number

              Do You Want To Showcase Your Technical Skills?
              Sign-Up for our projects.