You are on page 1of 12

Take home exam: Numerical Mathematics Special

Subject (wi4012TA)

Max Mustermann

February 18, 2011


Problem description: Laminar flow in a pipe
When considering the steady flow of an incompressible viscous fluid through
a pipe, caused by a pressure drop between the start and the end of the pipe,
it can be found that the velocity of the flow varies within the tube. As
long as there is no turbulence, a nonlinear layered velocity profile is built
up, which ranges from zero velocity at the walls of the pipe to a maximum
near its center. The flow of the fluid in the tube can be described by the
following differential equation:

d du ∆p
(r ) = r r  (0, R) (1)
dr dr VL
where u(r) is the velicity in axial direction, r is the radial direction, v is the
viscosity, ∆p is the pressure drop and L is the length of the tube.

Subtask 1: Checking the analytical solution


For eq. (1) there exists an analytical solution of the form...

1 ∆p 2
u=− (R − r2 )
4 vL
It can be proved easily that this is indeed a solution:

du 1 ∆p
= r
dr 2VL
du 1 ∆p 2
r = r
dr 2VL
d du 2 ∆p ∆p
r( ) = r=r
dr dr 2 vL VL


Subtask 2: Deriving the weak formulation


We use the boundary condition du dr = 0. Figure 1, which shows a skeech of
a velocity flow profile, illustrates why this is an appropriate and physical
boundary condition. The first derivative with respect to r at position r=0
is 0 because the velocity decreases towards the pipes walls on both sides.
Now we derive step-by-step the weak formulation of equation (1).

1. Multiply with an arbitrary, continuous and piecewise differentiable func-


tion v(r) (Testfunction):

1
Figure 1: The velocity profile

d du(r) ∆p
(r v(r)
) = v(r)r
dr dr VL
2. Integrate over the Domain (0 → R):
Z R Z R
d du(r) ∆p
v(r) (r )dr = v(r)r
0 dr dr 0 VL
3. Integrate by parts:

du(r) R
Z R   Z R
dv(r) du(r) ∆p
r dr = v(r)r − v(r)r dr
0 dr dr dr 0 0 VL

4. Substitute Boundary conditions and v(0)=0 to get the weak form:

Z R Z R
dv(r) du(r) ∆p
r dr = − v(r)r dr (2)
0 dr dr 0 VL

Subtask 3: Deriving the element matrix Ke


I define a mesh of N+1 equidistant grid points ri = 0...N with r0 = 0 and
R
rN = R. The gridsize h = N . One element consists of the domain [ri−1 , ri].
The gridding scheme is illustrated in figure 2.
Firstly equation (2) is approximated with Galerkin’s Finite Element
Method. Therefore the following steps have to be done:

2
Figure 2: The gridding scheme

1. Writing the solution u(r) as a linear combination of basis functions.


Numerical approximation: W (r) = W0 µ0 (r)+ N
P +1
i=1 W i i (r).Then we chose
µ
for the testfunction v(r) = µi (r) with i = 1...N + 1, where the µi ’s are
piecewise linear and µi (rj ) = 0 if i 6= j and µi (rj ) = 0 if i = j. After some
manipulations we arrive at the following linear system:

N Z R Z R Z R
X dµj dµi dµ0 dµi ∆p
Wj rdr = − µi rF dr− rdr i {1, ..., N } F =
0 dr dr 0 0 dr dr VL
j=1

2. Writing out this equation element wise for each i and considering the fact
that only contributions from neighbouring elements are non-zero (all other
elements don’t contribute to the global matrix) leads to equations like this
(for brevity only eqns. 1,2, and 20 are shown here):

Equation 1:

Z r1 Z r2  Z r2
W1 µ01 µ01 rdr + µ01 µ01 rdr + W2 µ01 µ02 rdr + 0 + 0 + ... =
r0 r1 r1
Z r1 Z r2 Z r1
− µ1 rF dr − µ1 rF dr − W0 µ00 µ01 rdr
r0 r1 r0

Equation 2:

Z r2 Z r2 Z r3  Z r3
W1 µ01 µ02 rdr+W2 µ02 µ02 rdr + W2 µ02 µ02 rdr +W3 µ02 µ03 rdr + 0 + 0+ ... =
r1 r1 r2 r2
Z r2 Z r3
− µ2 rF dr − µ2 rF dr
r1 r2

Equation 20:

Z r20 Z r20 Z r20


... + 0 + 0+ W19 µ019 µ020 rdr +W20 µ020 µ020 rdr = − µ2 0rF dr
r19 r19 r19

3
The most practicable way to generate the global stiffness matrix with a
computer algorithm is based on the concept of the element-matrices. This
concept can be used because the individial elements always contribute the
same value (only including a transformation for the increasing r) to the
global stiffness matrix.

So from equations 1 to 20 we can read what each element contributes to the


system. We can define an element matrix Kei for each element i:
R ri dµi−1 dµi−1 R ri dµi−1 dµi !
dr dr rdr dr dr rdr
Kie = Rri−1
ri dµi dµi−1 Rri−1
ri dµi dµi
ri−1 dr dr rdr ri−1 dr dr rdr

Similarly we can compute an element vector fei for each element i:


R ri !
− rRi−1 µ i−1 rF dr
fei = ri
− ri−1 µi rF dr

The next step is to evaluate the integrals containend in the element matrix
and the element vector, with a suitable integration method. As we have
chosen piecewise linear basis functions, like illustrated in figure 3, this can
be done easily. Here this is done on the example of the top left integral of
the element matrix:
Z ri  
dµi−1 dµi−1 1 1 1  2 ri
rdr = − r r
ri−1 dr dr h h 2 i−1

1 2 2
= (r − ri−1 )
2h2 i
Similarly the other matrix elements are computed. We arrive at the following
evaluated element matrix, which can be easily implemented in an computer
algorithm:
 1 2 2 − 2h1 2 (ri2 − ri−1
2 )

i 2h2 (ri − ri−1 )
Ke =
− 2h1 2 (ri2 − ri−1
2 ) 1
2h2 i
(r2 − ri−1
2 )

For the evaluation of the solution’s vectors elements we make use of the
trapezoidal rule to integrate over half a tent functions:
Z ri
h1 2 2
− µi rF dr = −F (ri − ri−1 )
ri−1 2 2
Z ri+1
h1 2
− µi rF dr = −F (ri+1 − ri2 )
ri 2 2
Summing these two up gives the following components of the assembled
solution vector: −F h4 (ri+1
2 − ri2 )

4
Figure 3: Basis functions µj and their derivatives.

Subtask 4: Assembly of the global matrix


To assemble the global matrix from the element matrix the following matlab
code is used (please see appendix for complete programme):

for k=1:1:(N-1)
K(i,j)=K(i,j)+((1/(2*h^2))*((j*h)^2-(h*(j-1))^2));
K(i+1,j+1)=K(i+1,j+1)+((1/(2*h^2))*((j*h)^2-(h*(j-1))^2));
K(i+1,j)=K(i+1,j)+((-1/(2*h^2))*((j*h)^2-(h*(j-1))^2));
K(i,j+1)=K(i,j+1)+((-1/(2*h^2))*((j*h)^2-(h*(j-1))^2));
j=j+1;
i=i+1;
end

The solution vector is assembled using:

for l=2:1:N
f(l)=1/4*((r(l+1))^2-(r(l-1))^2);
end

5
∆p
With the parameters VL = −1, R = 2, h = 0.1 we get a global matrix K:

Columns 1 through 10

0.5 −0.5
 
 −0.5 2.0 −0.5 
 

 −1.5 4.0 −1.5 0 


 −2.5 6.0 −2.5 


 −3.5 8.0 −3.5 


 −4.5 10.0 −4.5 


 −5.5 12.0 −5.5 


 0 −6.5 14.0 −6.5 

 −7.5 16.0 −7.5 
−8.5 18.0 −8.5

Columns 11 through 20

−9.5 20.0 −9.5


 

 −10.5 22.0 −10.5 


 −11.5 24.0 −11.5 0 


 −12.5 26.0 −12.5 


 −13.5 28.0 −13.5 


 −14.5 30.0 −14.5 


 −15.5 32.0 −15.5 


 0 −16.5 34.0 −16.5 

 −17.5 36.0 −17.5 
−18.5 38.0

Solution vector f : Columns 1 through 10



0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

Columns 11 through 20

1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9

The global linear system is given by

Kw = f
and can be solved for examples with the help of Matlabs backslash operator
or a Gauss elimination method.

6
Subtask 5: Numerical simulations and results
The numerical simulations were performed with the already mentioned pa-
rameters. For efficiency reasons matlab sparse matrix storage was used. In
figure 4 both the analytical and the numerical solution are plotted. As one
can see there is an excellent agreement between the two curves. Only in for
smaller r values the numerical solution shows miner deviations. The green
curve shows the absolute error in percent and illustrates how the numerical
solution converges towards the true solution for higher r values. The error
for N=20 has its maximum of 0.6199% at r = 0.

Figure 4: The numerical (red) compared to the analytical (blue) solution


together with the error

Subtask 6: Computation of the flow rate


The flow field has its maximum when r=0. Therefore the maximum velocity
is given by

1 ∆p
umax = − ∗ ∗ R2
4 VL

7
while the general velocity in distance r from the center axis is given by

1 ∆p
u(r) = − ∗ ∗ (R2 − r2 )
4 VL
So the relation between u(r) and umax is given by

u(r) (R2 − r2 ) r2
= and u(r) = umax (1 − )
umax R2 R2
This relation can now be substituted in the Integral formula for the flow
rate Q which is given by
Z R
Q= 2πru(r)dr
0

R R
r2
 2
r4
Z
r umax
Q= 2πrumax (1 − 2 )dr = 2πumax − 2
= A
0 R 2 4R 0 2

where A is the tube area. When you plug in πR2 for A with R=2, then you
get a exact flow rate of

22 π
Q= = 2π
2
Now we want to compute the same flow rate by means of a numerical integra-
tion method. Again we use the trapezoidal rule. The Integral is evaluated
section wise. Subsequently all sections are summed up.

For section i the flowrate Q is given by


   2 ri+1
Wi + Wi+1 r
Qi = 2π =
2 2 ri
 2
ri+1 − ri2
 
Wi + Wi+1
= 2π =
2 2
1 2
= π(Wi + Wi+1 )(ri+1 − ri2 )
2
In Matlab this is expressed as

for p=1:1:N-1
c=c+(-1/2)*pi*(w(p)+w(p+1))*((r(p))^2-(r(p+1))^2);
end

Using N=20 and R=2 results in an absolute numerical error of 1.075 %


compared to the exact value of 2π.

8
Subtask 7: Error estimation
With h = 0.1 we get an approximation of ∼ 6.2156.
With h = 0.05 we get an approximation of ∼ 6.2659.
With h = 0.025 we get an approximation of ∼ 6.2788.

Using Richardson extrapolation we get

W (0.05) − W (0.1) 6.2659 − 6.2156


= = 4.15 ∼ 4
W (0.025) − W (0.05) 6.2788 − 6.2659
This means that when my gridsize is doubled my error increases by the factor
4. This corresponds to an second order Error O(h2 ). All error values are
positive. This is in full agreement with the concave-down function shape.
As we are dealing with a curved function, this relatively high error could
have been expected. Perhaps we would be able to get a lower error with
other numerical integration methods like Simpson’s rule.

9
Appendix: Matlab code
%MATLAB implementation of Nr. 4, 5 and 6
%Ludwig Auer, 02.03.2010
function []=femflow()
close all
clf;

%Constants and variables


N=20;
R=2;
h=R/N;
i=1;
j=1;
r=linspace(0,R,N+1);
K=zeros(N,N);

%Analytical solution
y=(1/4)*(R^2-r.^2);

%Assembly of the Global matrix


for k=1:1:(N-1)
K(i,j)=K(i,j)+((1/(2*h^2))*((j*h)^2-(h*(j-1))^2));
K(i+1,j+1)=K(i+1,j+1)+((1/(2*h^2))*((j*h)^2-(h*(j-1))^2));
K(i+1,j)=K(i+1,j)+((-1/(2*h^2))*((j*h)^2-(h*(j-1))^2));
K(i,j+1)=K(i,j+1)+((-1/(2*h^2))*((j*h)^2-(h*(j-1))^2));
j=j+1;
i=i+1;
end
K(N,N)=K(N-1,N-1)+2;

%Sparsening the Global matrix


Ks=sparse(K);

%Assigning the solution vector f


for l=2:1:N
%f(l)=h*r(l);
f(l)=1/4*((r(l+1))^2-(r(l-1))^2);
end

%Solve the linear system


w=Ks\f’;
w(N+1)=0;

10
%Error calculation
for o=1:1:N
error(o)=(y(o)-w(o))*100/y(o);
end

%Plot everything
plot(w,’r’);
hold on
plot(y);
hold on
plot(error,’g’)
title([’Fluid velocity profiles’]);
xlabel([’N is ’,num2str(N),’ and stepsize h is ’,num2str(h)]);
ylabel([’Fluid velocity in percent’]);
k=legend(’FEM’,’Analytical’,’Error in %’,3);
axis ([1 N 0 1])

%Numerical integration with the trapezoidal rule to determine


%the flow rate Q;
c=0;
for p=1:1:N-1
c=c+(-1/2)*pi*(w(p)+w(p+1))*((r(p))^2-(r(p+1))^2);
end

%Calculating the error of Q


errorq=((2*pi)-c)*100/(2*pi);

%disp(max(error));
%disp(errorq);

11

You might also like