Steady Convection and Diffusion 1D MATLAB CFD Code
Steady Convection and Diffusion 1D MATLAB CFD Code
PROJECT 1
Submitted by
Roshith Mittakolu
Roll.no:19210144
Discipline : Mechanical
CONTENTS
1)Objective 1
2)Approach for Discretization 1
3)Final form of Discretized equation 2
4)Using TDMA for Solving in MATLAB 3
5)Results 4
6)Conclusions 7
PROBLEM STATEMENT :
write a computational code to solve a steady, 1-D convection-diffusion equation given by
! !$ !&&&
#Γ % = (')$*
!" !" !"
1. Consider a uniform grid with 11 nodes (inclusive of the boundary nodes). Apply
upwind differencing for the convective term and central differencing for the diffusive
term.
2. Repeat part (1) above, except with central differencing for the convective term as well.
3. Plot &$ vs. x separately for part 1 and for part 2. In each of these plots, compare the numerical
solution with the exact analytical solution for Pe = 50. Also, in the part (1) plot, show the exact
solution for Pe = 18. Discuss how the exact solution for Pe = 18 compares with the up winded
numerical solution.
4. Repeat parts (1) through (3) with 41 nodes.
OBJECTIVE:
To find the numerical solution to the given “Steady 1-D convection-diffusion equation” by
applying finite differencing methods. Even though we can calculate the exact solution to the given
governing equation, it may not be possible to find exact solutions to higher order partial
differential equations, then we have solve them by computational methods. The main objective is
to find numerical solution and exact solution and compare them to gain some insight in the
computational techniques used for solving the higher order partial differential equations like
Navier stokes equation.
1
Since we know that for uniform grids, central differencing gives a second order or quadratic
representation for the function to be discretized,hence we will use this for discretizing the
diffusive term for less error in the numerical solution. And for convection term we will use both
backward differencing and central differencing as asked in the question.
Consider a three node stencil with 1 2 34 14 1 2 3 as nodes with 5" as the distance between them.
Discretization of diffusion term by central differencing
!$& $6 + $678
=
!" 5"
2
Tri Diagnol Matrix Algorithm:
Let us consider that after discretizing a partial differential equation we got a algebraic equation of
form
OP QP UPST :VP
M6 = N6 =
87QP RPST 87QP RPST
After getting M6 &4 N6 we can go for forward elimination upto WXℎ node and doing back substitution
from the known value for solving all unknowns of .
3
Results:
Figure 2 : Zoomed in view of fig 1 for distinguishing between Pe=18 exact solution and
Pe=50 Numerical solution
4
Figure 3 :Upwind Differencing of Convective term for 41 nodes
5
Figure 4: Central differencing of convective term for 41 nodes
· From fig.1 we can see that the numerical solution for Pe=50 is having a significant
deviation from the exact analytical solution for Pe=50.
· The numerical solution for Pe=50 is almost same as the exact solution of Pe =18.
· From fig.3 we can see that as the no. of nodes are increasing i.e 11 nodes to 41 nodes the
numerical solution is converging towards exact analytical solution.
· Comparing fig.4 and fig.1 in both cases the number of nodes are same i.e 11, but the
deviation from the exact solution is less in the upwind differencing scheme of convective
term than central differencing scheme for convective term.
· For 41 nodes the central differncing scheme for convective is having less deviation from
the actual solution compared to upwind differencing for the convective term.
Conclusion:
From this exercise we can conclude that, even if central differencing scheme is second order or
quadratic representation for a given function, by intuition it has to give more accurate or less
deviated values from exact solution. But in the case of 11 nodes the upwind or backward
differencing of convective term is giving a better numerical solution than that of central
diiferencing scheme. This is due to the fact that convection term is present in the governing
equation and if that’s the case more weightage has to be given to the nodes which are in the
starting position of the flow where the effect of convection is dominant rather than nodes which
are at the end position of the flow where convection is not dominant. This is the reason we got
6
more deviation from actual solution in the central differencing scheme for convective term
provided that we are considering less number of nodes for numerical estimation. If the nodes are
sufficiently large in number then both the schemes for convective term will give us a fair
numerical estimation of E. But taking large number of nodes i.e discretizing the domain into more
number of parts the computational cost will be more.
So in general we can conclude that upwind differencing will be a much suitable method for
dealing with discretizing the convective terms for a given differential equation.
7
UPWIND DIFFERENCING CODE
clear all
clc
pe=input('Enter peclet no : '); %Input peclet number
n=input('Enter number of nodes : '); %input no.of nodes
L=input('Enter length : '); %input for length
dx=L/(n-1); %distance between two nodes
A=zeros(n-1,1); %intializing A to store co-
effcients of phi(i+1)
B=zeros(n-1,1); %intializing B to store co-
effcients of phi(i-1)
A(2:n-1,1)=(1/(2+pe*dx)); %Storing co efficients of
phi(i+1)
B(2:n-1,1)=(1+pe*dx)/(2+pe*dx); %Storing co efficients of
phi(i-1)
D=zeros(n-1,1); %intializing D to store
constant matrix
phi=zeros(n,1); %intializing phi
phi(1)=0; %giving boundary conditions
phi(n)=1;
D(2)=0;
D(n-1)=phi(n)/(2+pe*dx); %storing constant matrix
c=zeros(n-1,1);
Q=zeros(n-1,1);
A(n-1)=0;
B(2)=0;
%forward elimination
for i=3:n-1
c(i)=A(i)/(1-B(i)*c(i-1));
Q(i)=(B(i)*Q(i-1)+D(i))/(1-B(i)*c(i-1));
end
%Back substitution
for i=n-1:-1:2
phi(i)=c(i)*phi(i+1)+Q(i);
end
%plotting x vs numerical values of phi
x=0:dx:phi(n);
plot(x,phi,'-om');
xlabel('x')
ylabel('phi(x)')
hold on
%plotting exact phi
exphi=(exp(pe*x/L))/(exp(pe)-1);
plot(x,exphi,'-*r');
hold on
%plotting exact graph for pe=18
pe=18;
exphi18=(exp(pe*x/L))/(exp(pe)-1);
plot(x,exphi18,'-*b');legend('Numerical solution for
Pe=50','Exact solution for Pe=50','Exact solution for Pe=18')
8
Central Differencing Code
clear all
pe=input('Enter peclet no : '); %Input peclet number
n=input('Enter number of nodes : '); %input no.of nodes
L=input('Enter length : '); %input for length
dx=L/(n-1); %distance between two
nodes
A=zeros(n-1,1); %intializing A to
store co-effcients of phi(i+1)
B=zeros(n-1,1); %intializing B to
store co-effcients of phi(i-1)
D=zeros(n-1,1); %intializing D to
store constant matrix
phi=zeros(n,1); %intializing phi
c=zeros(n-1,1);
Q=zeros(n-1,1);
A(2:n-1,1)=(2*L-pe*dx)/(4*L);
B(2:n-1,1)=(2*L+pe*dx)/(4*L);
phi(1)=0; %giving boundary conditions
phi(n)=1;
D(2)=((2*L+pe*dx)/(4*L))*phi(1);
D(n-1)=((2*L-pe*dx)/(4*L))*phi(n);
c(2)=A(2);
Q(2)=D(2);
B(2)=0;
A(n-1)=0;
%forward elimination
for i=3:n-1
c(i)=A(i)/(1-B(i)*c(i-1));
Q(i)=(B(i)*Q(i-1)+D(i))/(1-B(i)*c(i-1));
end
%Back substitution
for i=n-1:-1:2
phi(i)=phi(i+1)*c(i)+Q(i);
end
phi;
%plotting x vs numerical values of phi
x=0:dx:phi(n);
plot(x,phi,'-om');
xlabel('x')
ylabel('phi(x)')
hold on
%plotting exact phi
exphi=(exp(pe*x/L))/(exp(pe)-1);
plot(x,exphi,'-*r');
legend('Numerical solution for pe=50','Exact solution for
pe=50')
9