You are on page 1of 4

Computational Hydraulics Carlos Serrano Moreno

1


Homework 4
Use FD to solve the following 1D steady convection-diffusion equation for a concentration :
u
ux
(u) =
u
ux
_B
u
ux
]
Where the distribution of is a function of x. u = 2m/s, p = 1
hg
m
3
, D = 0.01 Kg m s and L = 1m.
Boundary conditions: (u) = 1 and (1) =

Compare your result with the analytical solution:

0

L

0
=
e
pux
D
1
e
puL
D
1

(x) =
0
+(
L

0
)
e
pux
D
1
e
puL
D
1
= 1
e
pux
D
1
e
puL
D
1


First, we must discretize our governing equation to obtain the finite differences scheme that we will use
to solve our problem. In this case we will use a central difference scheme, our equation becomes then:
u
ux
=

I+1

I-1
2x
, anu
u
2

ux
2
=

I+1
2
I
+
I-1
x
2

u

I+1

I-1
2x
B

I+1
2
I
+
I-1
x
2
= u _
u
2x

B
x
2
]
I+1
+_2
B
x
2
]
I
+ _
u
2x

B
x
2
]
I-1
= u
_
u
2x

B
x
2
]
I+1
+ _2
B
x
2
]
I
_
u
2x
+
B
x
2
]
I-1
= u
If we define Pclet number as Pe =
pux
2D
, our equation becomes: (Pe 1)
I+1
+ 2
I
(Pe +1)
I-1
= u If Pclet
number is bigger than 1 we will have oscillations in our solution. Then the stability of our solution will depend on
how many points we will use to discretize our domain.
In order to find the solution to our problem we must solve a linear system of equations and due to the big number
of unknowns we will work with the matrix form: Au=F. Then, the first thing that we must do is to find which is
going to be our Matrix A and the vector F.
If i = 1: 2
1
+ (Pe 1)
2
= (Pe +1)
0
, where:
0
is oui bounuaiy conuition:
0
= 1
Computational Hydraulics Carlos Serrano Moreno

2


If i = 2: (Pe +1)
1
+ 2
2
+ (Pe 1)
3
= u
If i = n-1: (Pe + 1)
n-2
+2
n-1
= (Pe 1)
n
, where:
n
is oui bounuaiy conuition:
n
= u

Then, the system (Au=f) that we have to solve becomes:
l
l
l
l
l
2 Pe 1 u
(Pe + 1) 2 Pe 1
u (Pe +1) 2
u
u
Pe 1

u u
u u
u u

u u u u (Pe + 1) 21
1
1
1
1

l
l
l
l
l

1

n-1
1
1
1
1
1
=
l
l
l
l
l
(Pe + 1)
0
u
u

(Pe 1)
n
1
1
1
1
1
=
l
l
l
l
l
(Pe + 1)
u
u

u
1
1
1
1
1

Then, by using Matlab we can solve this system and in the following plots we will compare our solution with the
analytical:

Figure 1: Comparison between the analytical solution and the one obtained by finite differences of the convection-diffusion equation

As we can see in Figure 1 if we do a coarse discretization of our domain the approximate solution becomes
unstable and we can see how oscillations appear. Then, we have to use a higher number of points so as to
decrease the Pclet number and so, be able to find an appropriate approximation to our problem.
Computational Hydraulics Carlos Serrano Moreno

3



Figure 2: Comparison between the analytical solution and the one obtained by finite differences of the convection-diffusion equation

As it was said before, by increasing the number of points in which our study domain is discretized we obtain a
Pclet number smaller than one and then, the solution provided by our system of equations is almost the same as
the one obtained by using the analytical solution.











Computational Hydraulics Carlos Serrano Moreno

4


Also the code elaborated in Matlab to solve the finite differences scheme is provided:
function homework4
n = 200;
na = 1000;
u = 2;
ro =1;
L=1
D = 0.01;
e = ones(n-1, 1);
h = 1/n;
ha = 1/na;
Pe = ro*u*h/2/D
A = spdiags([-1*(Pe +1)*e 2*e (Pe -1)*e], [-1 0 1], n-1, n-1);
xo = 0:h:1;
xa = 0:ha:1;
x = xo(2:(length(xo)-1));
x = x';
size(A);
b(1:n-1) = 0;
b(1) = Pe +1;
b(n-1) = 0;
c = b';
solution = A\c;
sol(1:n+1) = 0;
sol(1) = 1;
sol(2:n)= solution;
sol(n+1) = 0;
analytical = (1-1*(exp(ro*u*xa/D))/(exp(ro*u*L/D)-1));
plot(xo,sol,'b-');
hold
plot(xa, analytical,'color','red')
end

You might also like