Cover page for assignments
Name, personal number, program and email for all students behind this submission : Pierre-Alexandre BEAUFORT, 920104-T093, master in mathematical engineering, engineering,
[email protected] Hadrien VAN LIERDE, 920909-T058, master in mathematical engineering,
[email protected] engineering,
[email protected]
Course code, course name and program or course set : DN2221, Applied numerical methods - part 1, master of sciences in engineering
Teacher: Examiner: Lennart EDSBERG Ashraful KADIR Task name: Deadline: Compute Computerr Lab 3 - Numerica Numericall Solution Solution of a Boundar Boundary y Valu Valuee Problem Problem October October 18th 2013
I/We have followed the Code of Honour of the Department of Numerical Analysis when the task was performed.
Signature(s):
Teacher of the course’s signature:
KTH SCI,
Assessment/Rating:
Kungliga Tekniska hogskolan Lindstedtsva Lindstedtsvagen gen 25, 100 44 Stockholm. Stockholm. Tel: 08-790 08-790 80 50. Fax: Fax: 08-723 08-723 17 88. e-mail :
[email protected]
Contents 1 2
A
Diffe Differe renc ncee Form ormula ula . . . . . . . . . . . Numer umeriical cal Resu Result ltss . . . . . . . . . . . . 2.1 2.1 Discr Discreti etiza zatio tion n of the z-in z-inter terv val al . 2.2 Parameter v . . . . . . . . . . . 2.3 2.3 Spur Spurio ious us osci oscill llat atio ions ns . . . . . . Appen pendix . . . . . . . . . . . . . . . . A.1 DN2221_lab3_FD.m . . . . . . .
0
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
1 3 3 4 6 8 8
We consider the problem of computing the temparture in a pipe of length L containing a fluid heated by an electric coil. At steady state, the temperature T (z ) in the pipe is determined by the following equation:
−
d dT dT (k ) + vρC = Q (z ) dz dz dz
(1)
where k is the heat conduction coefficient (constant in this case), v is the fluid velocity in the z-direction through the pipe, ρ is the fluid density and C is is the heat capacity of the fluid. The driving driving function Q(z ), modeling the electric coil is defined as:
0 Q(z ) = 0 sin( Q 0
z −a π b−a
)
if 0 ≤ z ≤ a if a ≤ z ≤ b if b ≤ z ≤ L
We also have the folloxing boundary conditions. At z = = 0, the temperature is T 0 : T (0) = T 0
and at z = = L , heat is leaking out, having temperature T out out :
−k
1
dT (L) = k v (T (L) − T out out ) dz
Diffe Differe renc nce e Form ormula ula
To solve this equation with a difference formula, we need three discretizations.
Discretization of the z-interval: We discretize the interval [0, L] and obtain N + 1 equidistant 1 equidistant points {z i }0≤i≤N . We denote by h the distance between two consecutive points.
Discretization of the Differential equation: We have to compute a discrete approximation of the first and second derivative of T in each point z i 1 . First, let us calculate dT (z i ): dz (z i ) + T (z i+1 ) = T (z i ) + h dT dz dT T (z i−1 ) = T (z i ) − h dz (z i ) +
h2 d2 T 2 dz 2 h2 d2 T 2 dz 2
(z i ) + C 1 h3 (z i ) + C 2 h3
The difference between these two expressions gives: dT T (z i+1 ) − T (z i−1 ) (z i ) = + O (h2 ) 2h dz
1. In the following following equations, equations, C i for 1 ≤ i ≤ 6 denote real constants. The presence of these terms in Taylor expansions is based on the mean value theorem.
1
Let us now give an approximate expression for
d2 T dz 2
(z i ) + T (z i+1 ) = T (z i ) + h dT dz dT T (z i−1 ) = T (z i ) − h dz (z i ) +
(z i ):
h2 d2 T 2 dz 2 h2 d2 T 2 dz 2
3
(z i ) + h6 (z i ) − h6
3
d3 T dz 3 d3 T dz 3
(z i ) + C 3 h4 (z i ) + C 4 h4
The sum of these two expressions gives: d2 T T (z i+1 ) − 2T (z i ) + T (z i−1 ) ( ) = + O(h2 ) z i 2 2 dz h
We may now express the differential equation as the following difference equation: (−
2k k vρC k vρC ) ( ) + ( + ) ( ) + − T z − T z T (z i ) = Q (z i ) i i − 1 +1 2h 2h h2 h2 h2
Discretization of the Boundary conditions: The boundary condition in z = z 0 = 0 simply states T (0) = T 0 which gives: (−
k vρC k v ρC 2 k − − ) + ( + ) ( ) + T T z T (z 1 ) = Q (z 1 ) 0 2 2h 2h h2 h2 h2
dT The boundary condition in z = z N How to discret discretize ize N = L states −k dz (L) = k v (T (L) − T out out ). How this expression ? 2
2
dT h d T 3 T (z N N −1 ) = T (z N N ) − h dz (z N N ) + 2 dz (z N N ) + C 5 h dT 2 d T 3 T (z N N −2 ) = T (z N N ) − 2h dz (z N N ) + 2 h dz (z N N ) + C 6 h 2
2
2
Calculating the difference between the first equation multiplied by four and the second equation, we obtain: dT T (z N N −2 ) − 4T (z N N −1 ) + 3 T (z N N ) (z N + O (h2 ) N ) = 2h dz The boundary condition then becomes:
−k T (z − )−4T (2zh − )+3T (z ) = kv (T (z N N ) − T out out ) 1 ⇔ 2h (−kT (z N N −2 ) + 4 kT (z N N −1 ) + ( −3k − 2hkv )T (z N N )) = −kv T out out N
N
2
N
1
To compute an approximation of the solution to 1, we thus have to solve the following linear system:
−
2k 2
k h2
h
− .. . 0 0
− hk + 2
vρC 2h
2k h2
vρC 2h
− hk
2
0 +
vρC 2h
0 0 ...
..
. − hk − vρC 2h − 2kh 2
2
2k h2 4k 2h
vρC 2h −3k −2hkv 2h
− hk + 2
vρC h
T (z 1 ) Q(z 1) +Q( (z 2+) 2 . .. .. = . Q(z 1 ) k h2
N N −
T (z N N )
−kv T out out
)T 0
2
Nume Nu meri rica call Resu Result ltss
Here we present numerical results we obtain with our MATLAB function (see appendix A.1). A.1). Obviously, our implementation is built from our discretization of the problem (1 (1) we explained in the section 1 section 1.. We use constant parameters with values : L = 10[m] a = 1[m] b = 3[m] Q0 = 50[W · · m−3 ] k = 0.5[ mW · K ] W J −3 kv = 10[ m · K ] ρ = 1[kg · m ] c = 1[ kg · K ] T out T 0 = 400[K ] out = 300[K ]
(2)
except for v [m · s−1 ], the fluid velocity in the z-direction. First, we compute the solution for v = 0 with different discretizations of the z-interval . Then, we observe the effect of the parameter on the numerical results. Finally, we analyze the cases where spurious oscillations occur.
2.1
Discre Discretiz tizati ation on of of the the z-in z-inter terv val
We computed solutions with parameters (2 (2) and v = 0, for N = 10, 20, 40, 80, 80, i.e. for 1 for 111, 21, 41 and 81 points 81 points of discretization for the z-interval of the problem (1 (1). We observe observe on the figure 1 that the shape of the graph becomes sharper. Graph with 11 points
Graph with 21 points
550
600 550
500
500 ] k [ T
450
] k [ T
450
400 400 350
300 0
350
2
4
6
8
300
10
0
2
4
z [m]
Graph with 41 points
] k [ T
600
550
550
500
500 ] k [
450
T
400
350
350
4
10
8
10
450
400
2
8
Graph with 81 points
600
300 0
6 z [m]
6
8
300
10
z [m]
0
2
4
6 z [m]
Figure 1: Solutions of problem (1 (1) with parameters (2 (2) and v = 0 for N = 10, 20, 40, 80. 80. Besides, we can observe from figure 2 figure 2 the the solutions are converging as N is increasing. It means the error of discretization is decreasing when the stepsize of discretization is decreasing too. 3
Graphes for v = 0 [m/s] 600 11 points 21 points 41 points 81 points
550
500
] k [ T
450
400
350
300
0
1
2
3
4
5 z [m]
6
7
8
9
10 10
Figure 2: Solutions of problem (1 (1) with parameters (2 (2) and v = 0 for N = 10, 20, 40, 80 (from 80 (from bottom to top).
2.2 2.2
Par aram amet eter er v
Here, we use a constant number of points of discretization : 41 points points.. We study the effect of the parameter v on the numerical results. We get the results for v = 0.1, 0.5, 1 and 10 and 10 [ m · s−1 ]. We can see on figure 3 the temperatur temperaturee increas increases es a momen moment. t. Then, Then, it tends to be constan constantt along z-direction. z-direction. The higher v is, the longer is the interval during which the temperature is constant. Finally, it decreases according to the boundary condition. This behaviour is sharper on figure 4 Nevertheless, the obtained numerical result for v = 10 has 10 has an oscillation at the end of the graph. Actually, problem (1 (1) is a spurious spurious oscillation oscillation problem. We are going to study this numerical numerical effect in the point 2.3. 2.3.
4
Graph for v = 0.1 [m/s]
Graph for v = 0.5 [m/s]
600
550
550
500
500 ] k [ T
] k [
450
T
400
400 350
350 300
450
0
2
4
6
8
300
10
0
2
4
z [m]
6
8
10
8
10
z [m]
Graph for v = 1 [m/s]
Graph for v = 10 [m/s]
500
440 420
450 400 ] k [ T
] k [
400
T
380 360
350 340 300
0
2
4
6
8
320
10
0
2
4
z [m]
6 z [m]
Figure 3: Solutions of problem (1 (1) with parameters (2 (2) and N = 40 for v = 0.1, 0.5, 1, 10. 10.
Graphes with 41 points 600 v = 0.1 [m/s] v = 0.5 [m/s] v = 1 [m/s] v = 10 [m/s]
550
500
] k [ T
450
400
350
300
0
1
2
3
4
5 z [m]
6
7
8
9
10 10
Figure 4: Solutions of problem (1 (1) with parameters (2 (2) and N = 40 for v = 0.1, 0.5, 1, 10. 10. 5
2.3
Spurio Spurious us oscill oscillati ations ons
From the point 2.2, 2.2, we know the numerical solution oscillates for v = 10 and N = 40. 40. We are going to analyze the behaviour of the numerical solutions with v = 10, 10, for N = 10, 20, 40. 40. Figure 5 displays these different numerical solutions. Graph with 11 points
] k [ T
Graph with 21 points
500
500 500
450
450 ] k [
400
T
350
300
400
350
0
2
4
6
8
300
10
z [m]
0
2
4
6
8
10
z [m]
Graph with 41 points 440 420 400 ] k [ T
380 360 340 320
0
2
4
6
8
10
z [m]
Figure 5: Solutions of problem (1 (1) with parameters (2 (2) and v = 10 f 10 for or N = 10, 20, 40. 40. We observe the oscillation oscillation starts earlier for greater stepsize. Let us try to explain this numerical numerical effect. To understand this phenomenon, we use a model known as the advection-diffusion equation 2 : d2 u du = 0 for 0 ≤ x ≤ 1 with u(0) = 0, 0, u(1) = 1 − 2 + dx dx
Solvin Solvingg this equation equation using using the differen difference ce formula formulass explaine explained d above, above, we obtain obtain a differen difference ce equatio equation. n. We may may obtain obtain the solution solution of this differen difference ce equatio equation. n. To analyze analyze the occurrenc occurrencee h of spurious oscillations, we define P e := 2 , where h is the constant stepsize of the numerical method. According to the solution of the difference equation, we obtain the following results: – if P e < 1, 1 , the numerical solution is smooth – if P e > 1, 1 , the numerical solution oscillates In the case of problem 1, it 1, it is difficult to obtain a general solution to the difference equation obtained with difference formulas. However, we may difine the same constant P e := 2h . Then, displaying the graph of the solution for different values of h h and v , we observe graphically that oscillations appear for P e > 1 and that the solution is smooth for P e < 1 (this result is not 2. For the analysis of the see: adve advectionction-diff diffusi usion on equatio quation n , Introduction to computation and modeling for differential equations, Wiley, 2008, p. 79.
6
EDSBERG
(L.),
accurate accurate because it is only based on the general general behavio behaviour ur of the graph). graph). The followin followingg table table gives the value of P e for different values of the parameters of the problem (recording that, in k this case, := ρcv , if v = 0): v
0.1 0.5 1 10 10 10
N
Pe
40 5 0.025 40 1 0.125 40 0.5 0.25 40 0.05 2.5 20 0.05 5 10 0.05 10
We notice is decreasing while the velocity v is increasi increasing. ng. By definition definition,, we know P e is increasing while is decreasing. Then, P e is increasing while v is increasing too. Furthermore, P e is increasing while h is is increasi increasing ng too. Therefor Therefore, e, the analytica analyticall soluti solution on oscillates more for greater stepsize. Figure 6 Figure 6 displays this numerical behaviour for problem (1 (1) with parameters (2 (2) and v = 10, 10, for N = 10, 20, 40. 40.
Graphes for v = 10 [m/s] 500 11 points 21 points 41 points
480 460 440 420 ] k [ T
400 380 360 340 320 300
0
1
2
3
4
5 z [m]
6
7
8
9
10 10
Figure 6: Solutions of problem (1 (1) with parameters (2 (2) and v = 10 f 10 for or N = 10, 20, 40. 40.
7
1 2 3
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25
26 27 28 29
30 31 32 33
A
Appen pendix
A.1
DN2221_lab3_FD.m
% D N 22 22 2 1 - L ab ab 3 % B e au au f or or t & V an an L i er er d e %%%% % [ Th Th , z ] = D N 2 22 22 1 _l _l a b3 b3 _ FD FD ( N , v ) s o lv lv e s b y f i ni ni t e d i ff ff e re re n ce ce m e th th o d t he he % f o l l o w in in g d i f f e re re n t i a l p r o b l e m : % - ( k* k* T’ T ’ ) ’ + v *c *c *r * r h o *T * T ’ = Q o n z i n [ 0; 0; L] L] % s . t. t. T ( z =0 =0 ) = T 0 a nd nd - k T ’( ’( t =L = L ) = k v *( *( T (z ( z = L) L) - To To ut ut ) % w he he re re Q = Q 0 * si si n (( (( z -a - a ) /( /( b -a - a ) ) on on [ a ; b] b] a n d Q = 0 e ls l s ew e w he h e re re % H er er e w e s ol ol ve ve f or or : % L = 1 0; 0; %[ %[ m ] % a = 1 ; % [ m] m] % b = 3 ; % [ m] m] % Q 0 = 5 0 ;% ;% [ W . m ^ -3 -3 ] % k = . 5 ;% ;% [ W /( /( m . K )] )] % k v = 1 0 ;% ;% [ W /( /( m . K )] )] % r ho ho = 1 ; %[ %[ k g . m ^ -3 -3 ] % c = 1 ; %[ %[ J / ( K. K. k g )] )] % T o ut ut = 3 0 0; 0; % [ K] K] % T 0 = 4 0 0; 0; % [ K] K] %---% % B e s id id e s , t h e f u n c ti ti o n s d i s p la la y s p a r a me me t e r s a b o ut ut o s c il il l a ti ti o n s , i n r e s p ec ec t % t he he m od od el el p ro ro bl bl em e m : - e ps ps il i l on on * f ’’ ’’ + f = 0 % %@PRE: % - N , th th e nu n u mb mb er er o f i nt n t er er va va l ( sc s c al al ar ar ) % - v , t he he f l ui u i d v e lo lo ci c i ty ty i n t h e z - di di re r e ct c t io i o n ( s ca ca la la r ) % %@POST: % - T h , t he he a pp p p ro r o xi x i ma m a te t e d is i s cr c r et e t is is e s ol ol ut u t io io n ( ( N +1 +1 ) x1 x1 v ec ec to to r ) % - z , th t h e d is is cr c r et et e i n te te rv r v al a l ( 1 x (N (N + 1) 1) v e ct c t or or ) %%%% f u n c t i o n [ T h , z ] = D N 2 2 2 1 _l _l a b 3 _ FD FD ( N , v )
34 35 36 37 38 39 40 41 42 43 44 45
%%parameters L = 1 0; 0; % [ m ] a = 1; 1; % [ m ] b = 3; 3; % [ m ] Q 0 = 5 0; 0; %[W.m^-3] k = . 5; 5; % [ W / ( m . K ) ] k v = 1 0; 0; %[W/(m.K)] r ho ho = 1 ; %[kg.m^-3] c = 1; 1; % [ J / ( K . k g ) ] T ou ou t = 3 0 0; 0; % [ K ] T 0 = 4 0 0; 0; % [ K ]
46 47 48 49 50
%discretization z = l i n s p a c e (0,L,N+1); h = L /N /N ;
51 52 53 54 55
% b u i l d in in g d i s c re re t e s y s t em em Q = z e r o s (1,N+1); Q (z ( z > a& a& z
a & z < b ) - a ) / ( b - a ) *
p i );
56 57
b = [ T 0 ( ( 2 * h ^2 ^2 ) * Q (2 (2 : end - 1 ) ) ( 2 * h * kv kv / k ) * T o u t ] ’ ;
58 59 60 61 62
d 0 = [ 1 ( 4 * k * o n es es ( 1 , N -1 -1 ) ) ( 3 + 2* 2* h * k v /k /k ) ] ’; ’; d 1 = [ 0 0 ( h * rh rh o * c* c* v - 2* 2* k ) * o ne ne s (1 (1 , N - 1) 1) ] ’ ; d m i n us us 1 = [ - ( 2 * k + h * rh rh o * c * v ) * o ne ne s ( 1 , N - 1 ) - 4 0 ] ’ ;
63 64 65
A = s p d i a g s ( [ d0 d0 d 1 d m in in u s1 s1 ] , [ 0 1 - 1] 1] , N +1 +1 , N + 1) 1) ; A( end , end - 2) 2) = 1 ;
8
66 67 68 69
% s o l v e d i s c r e te te s y s te te m T h = A \b \b ;
70 71 72 73 74 75 76 77 78
e p s i lo lo n = k / ( v * r h o * c ); ); i f e p s i lo lo n ~ = i nf nf f p r i n t f ( [ ’ e ps ps il i l on on = ’ num2str (epsilon)]) f p r i n t f ( [ ’ , P e = ’ num2str (h/(2*epsilon))]) f p r i n t f ( [ ’ , N = ’ num2str (N)]) f p r i n t f ( [ ’ , v = ’ num2str ( v) ’ \ n ’ ]) en d
79 80 81
en d
9