All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Aim: To derive and write a program for in MATLAB for 4th order approximation for second order derivative by following methods Central difference Skewed right sided difference Skewed left sided difference Objective: To evaluate the second order derivative of the function f(x)=exp(x)cosx Using…
Kishoremoorthy SP
updated on 26 Apr 2023
Aim:
To derive and write a program for in MATLAB for 4th order approximation for second order derivative by following methods
Objective:
Theory and derivations:
During solving a CFD problem we may come across partial derivative equation which consists 2nd order derivative. Since due to complexity of the equation we may have to chose numerical approach rather than analytical. But when dealing with numerical approach we try to make the error margin as low as possible.
As the order of approximation get higher, we get higher accuracy. Hence, we are going to derive the three methods of approximation for an analytical function. The methods are following.Central difference: - Here the nodes selected are symmetrical i.e. the data is taken from both sides of point of focus. Using Taylor’s method, we get
∂2f∂x2=af(i−2)+bf(i−1)+cf(i)+df(i+1)+ef(i+2)
Here there are two nodes taken from both side of the point of focus i.e. f(i).
(NOTE: - To select the number of nodes in consideration, we use following expression:
N=p + q -1 for central differencing
N = p + q for skewed differencing
Here N is no. of nodes, p is order of derivative and q is order of approximation.)
Taylor’s table for Central difference
f (X) |
dx f’(x) |
dx2 f’’(x) |
dx3 f’’’(x) |
dx4 f’’’’(x) |
dx5 f’’’’’(x) |
|
af(i-1) |
a |
-2a |
4a/2 |
-8a/6 |
16a/24 |
-32a/120 |
bf(i-2) |
b |
-b |
b/2 |
-b/6 |
b/24 |
-b/120 |
cf(i) |
c |
0 |
0 |
0 |
0 |
0 |
df(i-1) |
d |
d |
d/2 |
d/6 |
d/24 |
d/120 |
ef(i-2) |
e |
2e |
4e/2 |
8e/6 |
16e/24 |
32e/120 |
af(i-2) +bf(i-1) +cf(i) +df(i+1) + ef(i+2) |
0 |
0 |
1 |
0 |
0 |
This table transform into matrix of type AX=B
⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣11111−2−101242120124212345−86−1601686162412401241624⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦⋅⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣abcde⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦=⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣00100⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦ on further solving we get the values of the unknown
i.e. Values = a = -0.0833 b= 1.3333 c= -2.5000 d= 1.3333 e= -0.0833
These coefficients will further used in code to get the value of derivative.
Taylor’s table for Skewed right sided difference:
|
f(x) |
dx f’ (X) |
dx2 f’’ (x) |
dx3 f’’’(x) |
dx4f’’’’(x) |
dx5f’’’’’(x) |
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 |
ff(i+5) |
f |
5f |
25f/2 |
125f/6 |
625f/24 |
3125f/120 |
af(i) + bf(i+1) + cf(i+2) + df(i+3) + ef(i+4) +ff(i+5) |
0 |
0 |
1 |
0 |
0 |
0 |
This table transform into matrix of type AX=B:
⎡⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢⎣1111110123450122921622520168627664612560124162481242562462524011203212024312010241203125120⎤⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥⎦⋅⎡⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢⎣abcdef⎤⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥⎦=⎡⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢
⎢⎣001000⎤⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥
⎥⎦ on further solving we get the values of the
unknown i.e. Values = a = 3.7500 b= -12.8333 c= 17.8333
d= -13.0000 e= 5.0833 f= -0.8333
These coefficients will further used in code to get the value of derivative.
∂2f∂x2=af(i−5)+bf(i−4)+cf(i−3)+df(i−2)+ef(i−1)+ff(i)
Taylor’s table for Skewed right sided difference:
|
f(x) |
dx f’ (X) |
dx2 f’’ (x) |
dx3 f’’’(x) |
dx4f’’’’(x) |
dx5f’’’’’(x) |
af(i-5) |
a |
-5a |
25a/2 |
-125a/6 |
625a/24 |
-3125a/120 |
bf(i-4) |
b |
-4b |
16b/2 |
-64b/6 |
256b/24 |
-1024b/120 |
cf(i-3) |
c |
-3c |
9c/2 |
-27c/6 |
81c/24 |
-243c/120 |
df(i-2) |
d |
-2d |
4d/2 |
-8d/6 |
16d/24 |
-32d/120 |
ef(i-1) |
e |
-e |
e/2 |
-e/6 |
e/24 |
-e/120 |
ff(i) |
f |
0 |
0 |
0 |
0 |
0 |
af(i-5) + bf(i-4) + cf(i-3) + df(i-2) + ef(i-1) +ff(i) |
0 |
0 |
1 |
0 |
0 |
0 |
This table transform into matrix of type AX=B:
⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣111111−5−4−3−2−10252162922120−1256−646−276−86−1606252425624812416241240−3125120−1024120−243120−32120−11200⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦⋅⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣abcdef⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦=⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣001000⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦ on further solving we get the values of the
unknown i.e. Values = a = -0.8333 b= 5.0833 c=13.0000
d= 17.8333 e= -12.8333 f= 3.7500
These coefficients will further used in code to get the value of derivative.
MATLAB code:
clear all
close all
clc
%analytical function defination and value assignment
x=pi/3;
dx=linspace(pi/10,pi/1000,100);
f=@(x)(exp(x)*cos(x));
g=@(x)(-2*exp(x)*sin(x));
d2f=g(x);
func=f(x);
%for central difference scheme
%coefficients taken from taylor's table
P = [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];
C=[0;0;1;0;0];
%as P*a=C i.e. a=inverse(P)*c(fastest way to getby '\')
cf1=P\C;
%for right skewwd differnce scheme
Q= [1 1 1 1 1 1
0 1 2 3 4 5
0 1/2 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];
%as Q*b=s i.e. b=inverse(Q)*s(fastest way to getby '\')
cf2=Q\D;
%for right skewwd differnce scheme
R= [1 1 1 1 1 1
-5 -4 -3 -2 -1 0
25/2 16/2 9/2 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];
E=[0;0;1;0;0;0];
%as n*b=s i.e. b=inverse(n)*s(fastest way to getby '\')
cf3=R\E;
%calculating values of derivative using CDS RSDC,LSDC
%defining variable size befor using in loop
central_diff=zeros(1,100);
err_central=zeros(1,100);
right_sdiff=zeros(1,100);
err_right=zeros(1,100);
left_sdiff=zeros(1,100);
err_left=zeros(1,100);
for i=1:length(dx)
%calculating value and error by central difference scheme
central_diff(i)=(cf1(1)*f(x-2*dx(i))+cf1(2)*f(x-dx(i))+cf1(3)*f(x)+cf1(4)*f(x+dx(i))+cf1(5)*f(x+2*dx(i)))/(dx(i))^2;
err_central(i)=abs(central_diff(i)-d2f);
%calculating value and error by right skewed difference scheme
right_sdiff(i)=(cf2(1)*f(x)+cf2(2)*f(x+dx(i))+cf2(3)*f(x+2*dx(i))+cf2(4)*f(x+3*dx(i))+cf2(5)*f(x+4*dx(i))+cf2(6)*f(x+5*dx(i)))/(dx(i))^2;
err_right(i)=abs(right_sdiff(i)-d2f);
%calculating value and error by left skewed difference scheme
left_sdiff(i)=(cf3(1)*f(x-5*dx(i))+cf3(2)*f(x-4*dx(i))+cf3(3)*f(x-3*dx(i))+cf3(4)*f(x-2*dx(i))+cf3(5)*f(x-dx(i))+cf3(6)*f(x))/(dx(i))^2;
err_left(i)=abs(left_sdiff(i)-d2f);
end
%plotting values of derivative
subplot(2,1,1)
hold on
fplot(d2f,'--')
xlim([-0.05 0.35]);
plot(dx,central_diff,'linewidth',2,'color','r');
plot(dx,right_sdiff,'linewidth',2,'color','b');
plot(dx,left_sdiff,'linewidth',2,'color','g');
legend('analytical value at x=pi/3','central diff.','right skewed diff.','left skewed diff.','location','best')
xlabel('Grid siza(dx)');
ylabel('Value');
title('Comparision b\w value of derivative calculated by diff. order of approximation')
hold off
grid on
%plotting errors in approximation of derivative
subplot(2,1,2)
loglog(dx,err_central,'linewidth',2,'color','r');
hold on
xlim([.002 0.4]);
loglog(dx,err_right,'linewidth',2,'color','b');
loglog(dx,err_left,'linewidth',2,'color','g');
legend('central diff.','right skewed diff.','left skewed diff.','location','best')
xlabel('Grid siza(dx)');
ylabel('Absolute error');
title('Comparision b/w 4th order approximation method for error in calculating d2f/dx2 ');
grid on
Steps involved:
AX=B ==> X=A-1 B
And stored the coefficients in cf1, cf2 and cf3.
Result:
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.
Other comments...
Week 4 Challenge : CFD Meshing for BMW car
AIM: FOR THE GIVE MODEL, CHECK AND SOLVE ALL GEOMETRICAL ERRORS ON HALF PORTION AND ASSIGN APPROPRITATE PIDS. PERFORMS MESHING WITH GIVEN TARGET LENGTH AND ELEMENT QUALITY CRITERIA. AFTER MESHING THE HALF MODEL,DO SYMMETRY TO THE OTHER SIDE. PRODECURE: INITIALLY, OPEN THE GIVEN BMW MODEL IN ANSA SOFTWARE.…
20 Oct 2023 11:25 AM IST
Week 12 - Validation studies of Symmetry BC vs Wedge BC in OpenFOAM vs Analytical H.P equation
Aim: employing the symmetry boundary condition to simulate an axis-symmetric laminar flow through the pipe's constant cross-section. Using both symmetry and wedge boundary conditions, simulate the aforementioned angles—10, 25, and 45 degrees—and evaluate your results using HP equations. Introduction: Hagen-Poiseuille's…
04 May 2023 03:14 PM IST
Week 11 - Simulation of Flow through a pipe in OpenFoam
Aim: Simulate axisymmetric flow in a pipe through foam. Objective: Verify the hydrodynamic length using the numerical result Verify a fully developed flow rate profile with its analytical profile Verify the maximum velocity and pressure drop for fully developed flow Post-process Shear Stress and verify wall shear stress…
04 May 2023 03:04 PM IST
Week 9 - FVM Literature Review
AIM To describe the need for interpolation schemes and flux limiters in Finite Volume Method (FVM). OBJECTIVE To study and understand What is Finite Volume Method(FVM) Write down the major differences between FDM & FVM Describe the need for interpolation schemes and flux limiters in FVM INTRODUCTION …
03 May 2023 05:47 AM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.