numerical solution of a boundary value problem

11

Click here to load reader

Upload: lambdastudentepl

Post on 26-May-2017

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Numerical Solution of a Boundary Value Problem

!

!"#$%&&$$'()* +"#*',-%.,',/$011/'+&2#!

!!!

!"#$%!!&!#'(#)*+&,-!./0%&%1"#!2*/,.%%3$$*/4!2/.1/#$!!345!*52.,+! !+"#*)--)*$&0(2,&2#*$3.*$&6#*7)83.*(2,,)*',-%.,',/9*!

!

!:0#$83(;*80#$,).,*345*1#3/#).*2--2#*80#$3./6,/9!

!:0#$-2()#29!

!<#),$8)#2*=3.*),,),*%,*80#$-2()#2,>9!

!?11/'+&2,$*,).,9!

!@,-%.,',/$()&0.9!

!!A)/BC'*5)#*+"-D&*52(2#$83(2E2,*16*)C(2-,',/2,*+"#*F0.2#'$8*G,)-H$*,%#*011/'+&2,*0&+"#&$!

!!!!F).,&248,',/=)#>!

!

!!!!!

:0#$-2()#2,$*$'/,)&0#9** I2(".,',/B72&H/9!!!!!!!

Kungliga Tekniska högskolan !

KTH SCI, Lindstedtsvägen 25, 100 44 Stockholm. Tel: 08-790 80 50. Fax: 08-723 17 88. E-post: [email protected]

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, [email protected] VAN LIERDE, 920909-T058, master in mathematical 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 KADIRTask name: Deadline:Computer Lab 3 - Numerical Solution of a Boundary Value Problem October 18th 2013

I/We have followed the Code of Honour of the Department of Numerical Analysiswhen the task was performed.

Signature(s):

Teacher of thecourse’s signature:

Assessment/Rating:

Kungliga Tekniska hogskolanKTH SCI, Lindstedtsvagen 25, 100 44 Stockholm. Tel: 08-790 80 50. Fax: 08-723 17 88.

e-mail : [email protected]

Page 2: Numerical Solution of a Boundary Value Problem

Contents

1 Difference Formula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Numerical Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2.1 Discretization of the z-interval . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Parameter v . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.3 Spurious oscillations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

A Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8A.1 DN2221_lab3_FD.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

0

Page 3: Numerical Solution of a Boundary Value Problem

We consider the problem of computing the temparture in a pipe of length L containing a fluidheated by an electric coil. At steady state, the temperature T (z) in the pipe is determined bythe following equation:

− d

dz(kdTdz

) + vρCdT

dz= Q(z) (1)

where k is the heat conduction coefficient (constant in this case), v is the fluid velocity in thez-direction through the pipe, ρ is the fluid density and C is the heat capacity of the fluid. Thedriving function Q(z), modeling the electric coil is defined as:

Q(z) =

0 if 0 ≤ z ≤ aQ0 sin( z−a

b−aπ) if a ≤ z ≤ b

0 if b ≤ z ≤ L

We also have the folloxing boundary conditions. At z = 0, the temperature is T0:

T (0) = T0

and at z = L, heat is leaking out, having temperature Tout:

−kdTdz

(L) = kv(T (L)− Tout)

1 Difference FormulaTo 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 points {zi}0≤i≤N . We denote byh 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 eachpoint zi 1.First, let us calculate dT

dz(zi):

T (zi+1) = T (zi) + hdTdz

(zi) + h2

2d2Tdz2 (zi) + C1h

3

T (zi−1) = T (zi)− hdTdz (zi) + h2

2d2Tdz2 (zi) + C2h

3

The difference between these two expressions gives:

dT

dz(zi) = T (zi+1)− T (zi−1)

2h +O(h2)

1. In the following equations, Ci for 1 ≤ i ≤ 6 denote real constants. The presence of these terms in Taylorexpansions is based on the mean value theorem.

1

Page 4: Numerical Solution of a Boundary Value Problem

Let us now give an approximate expression for d2Tdz2 (zi):

T (zi+1) = T (zi) + hdTdz

(zi) + h2

2d2Tdz2 (zi) + h3

6d3Tdz3 (zi) + C3h

4

T (zi−1) = T (zi)− hdTdz (zi) + h2

2d2Tdz2 (zi)− h3

6d3Tdz3 (zi) + C4h

4

The sum of these two expressions gives:

d2T

dz2 (zi) = T (zi+1)− 2T (zi) + T (zi−1)h2 +O(h2)

We may now express the differential equation as the following difference equation:

(− k

h2 −vρC

2h )T (zi−1) + (− k

h2 + vρC

2h )T (zi+1) + 2kh2 T (zi) = Q(zi)

Discretization of the Boundary conditions:

The boundary condition in z = z0 = 0 simply states T (0) = T0 which gives:

(− k

h2 −vρC

2h )T0 + (− k

h2 + vρC

2h )T (z2) + 2kh2 T (z1) = Q(z1)

The boundary condition in z = zN = L states −k dTdz

(L) = kv(T (L) − Tout). How to discretizethis expression ?

T (zN−1) = T (zN)− hdTdz

(zN) + h2

2d2Tdz2 (zN) + C5h

3

T (zN−2) = T (zN)− 2hdTdz

(zN) + 2h2 d2Tdz2 (zN) + C6h

3

Calculating the difference between the first equation multiplied by four and the second equation,we obtain:

dT

dz(zN) = T (zN−2)− 4T (zN−1) + 3T (zN)

2h +O(h2)

The boundary condition then becomes:

−k T (zN−2)−4T (zN−1)+3T (zN )2h = kv(T (zN)− Tout)

⇔ 12h(−kT (zN−2) + 4kT (zN−1) + (−3k − 2hkv)T (zN)) = −kvTout

To compute an approximation of the solution to 1, we thus have to solve the following linearsystem:

2kh2 − k

h2 + vρC2h 0 0

− kh2 − vρC

2h2kh2 − k

h2 + vρC2h 0

... . . . ...0 − k

h2 − vρC2h

2kh2 − k

h2 + vρC2h

0 − k2h

4k2h

−3k−2hkv

2h

T (z1)

...

T (zN)

=

Q(z1) + ( kh2 + vρC

2h )T0Q(z2)

...Q(zN−1)−kvTout

2

Page 5: Numerical Solution of a Boundary Value Problem

2 Numerical ResultsHere we present numerical results we obtain with our MATLAB function (see appendix A.1).Obviously, our implementation is built from our discretization of the problem (1) we explainedin the 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[ Wm ·K ]

kv = 10[ Wm ·K ] ρ = 1[kg ·m−3] c = 1[ J

kg ·K ] Tout = 300[K] T0 = 400[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 caseswhere spurious oscillations occur.

2.1 Discretization of the z-intervalWe computed solutions with parameters (2) and v = 0, for N = 10, 20, 40, 80, i.e. for 11, 21, 41and 81 points of discretization for the z-interval of the problem (1). We observe on the figure1 that the shape of the graph becomes sharper.

0 2 4 6 8 10300

350

400

450

500

550

z [m]

T [

k]

Graph with 11 points

0 2 4 6 8 10300

350

400

450

500

550

600

z [m]

T [

k]

Graph with 21 points

0 2 4 6 8 10300

350

400

450

500

550

600

z [m]

T [

k]

Graph with 41 points

0 2 4 6 8 10300

350

400

450

500

550

600

z [m]

T [

k]

Graph with 81 points

Figure 1: Solutions of problem (1) with parameters (2) and v = 0 for N = 10, 20, 40, 80.

Besides, we can observe from figure 2 the solutions are converging as N is increasing. It meansthe error of discretization is decreasing when the stepsize of discretization is decreasing too.

3

Page 6: Numerical Solution of a Boundary Value Problem

0 1 2 3 4 5 6 7 8 9 10300

350

400

450

500

550

600

z [m]

T [k]

Graphes for v = 0 [m/s]

11 points

21 points

41 points

81 points

Figure 2: Solutions of problem (1) with parameters (2) and v = 0 for N = 10, 20, 40, 80 (frombottom to top).

2.2 Parameter vHere, we use a constant number of points of discretization : 41 points. We study the effectof the parameter v on the numerical results. We get the results for v = 0.1, 0.5, 1 and 10 [m · s−1].

We can see on figure 3 the temperature increases a moment. Then, it tends to be constantalong z-direction. The higher v is, the longer is the interval during which the temperature isconstant. Finally, it decreases according to the boundary condition. This behaviour is sharperon figure 4

Nevertheless, the obtained numerical result for v = 10 has an oscillation at the end of the graph.Actually, problem (1) is a spurious oscillation problem. We are going to study this numericaleffect in the point 2.3.

4

Page 7: Numerical Solution of a Boundary Value Problem

0 2 4 6 8 10300

350

400

450

500

550

600

z [m]

T [

k]

Graph for v = 0.1 [m/s]

0 2 4 6 8 10300

350

400

450

500

550

z [m]

T [

k]

Graph for v = 0.5 [m/s]

0 2 4 6 8 10300

350

400

450

500

z [m]

T [

k]

Graph for v = 1 [m/s]

0 2 4 6 8 10320

340

360

380

400

420

440

z [m]

T [

k]

Graph for v = 10 [m/s]

Figure 3: Solutions of problem (1) with parameters (2) and N = 40 for v = 0.1, 0.5, 1, 10.

0 1 2 3 4 5 6 7 8 9 10300

350

400

450

500

550

600

z [m]

T [

k]

Graphes with 41 points

v = 0.1 [m/s]

v = 0.5 [m/s]

v = 1 [m/s]

v = 10 [m/s]

Figure 4: Solutions of problem (1) with parameters (2) and N = 40 for v = 0.1, 0.5, 1, 10.

5

Page 8: Numerical Solution of a Boundary Value Problem

2.3 Spurious oscillationsFrom the point 2.2, we know the numerical solution oscillates for v = 10 and N = 40. Weare going to analyze the behaviour of the numerical solutions with v = 10, for N = 10, 20, 40.Figure 5 displays these different numerical solutions.

0 2 4 6 8 10300

350

400

450

500

z [m]

T [

k]

Graph with 11 points

0 2 4 6 8 10300

350

400

450

500

z [m]

T [

k]

Graph with 21 points

0 2 4 6 8 10320

340

360

380

400

420

440

z [m]

T [

k]

Graph with 41 points

Figure 5: Solutions of problem (1) with parameters (2) and v = 10 for N = 10, 20, 40.

We observe the oscillation starts earlier for greater stepsize. Let us try to explain this numericaleffect.

To understand this phenomenon, we use a model known as the advection-diffusion equation 2:

−εd2u

dx2 + du

dx= 0 for 0 ≤ x ≤ 1 with u(0) = 0, u(1) = 1

Solving this equation using the difference formulas explained above, we obtain a differenceequation. We may obtain the solution of this difference equation. To analyze the occurrenceof spurious oscillations, we define Pe := h

2ε , where h is the constant stepsize of the numericalmethod. According to the solution of the difference equation, we obtain the following results:– if Pe < 1, the numerical solution is smooth– if Pe > 1, the numerical solution oscillates

In the case of problem 1, it is difficult to obtain a general solution to the difference equationobtained with difference formulas. However, we may difine the same constant Pe := h

2ε . Then,displaying the graph of the solution for different values of h and v, we observe graphically thatoscillations appear for Pe > 1 and that the solution is smooth for Pe < 1 (this result is not

2. For the analysis of the advection-diffusion equation, see: EDSBERG (L.),Introduction to computation and modeling for differential equations, Wiley, 2008, p. 79.

6

Page 9: Numerical Solution of a Boundary Value Problem

accurate because it is only based on the general behaviour of the graph). The following tablegives the value of Pe for different values of the parameters of the problem (recording that, inthis case, ε := k

ρcv, if v 6= 0):

v N ε Pe

0.1 40 5 0.0250.5 40 1 0.1251 40 0.5 0.2510 40 0.05 2.510 20 0.05 510 10 0.05 10

We notice ε is decreasing while the velocity v is increasing. By definition, we know Pe is in-creasing while ε is decreasing. Then, Pe is increasing while v is increasing too.

Furthermore, Pe is increasing while h is increasing too. Therefore, the analytical solutionoscillates more for greater stepsize. Figure 6 displays this numerical behaviour for problem (1)with parameters (2) and v = 10, for N = 10, 20, 40.

0 1 2 3 4 5 6 7 8 9 10300

320

340

360

380

400

420

440

460

480

500

z [m]

T [k]

Graphes for v = 10 [m/s]

11 points

21 points

41 points

Figure 6: Solutions of problem (1) with parameters (2) and v = 10 for N = 10, 20, 40.

7

Page 10: Numerical Solution of a Boundary Value Problem

A Appendix

A.1 DN2221_lab3_FD.m

1 % DN2221 - Lab32 % Beaufort & Van Lierde3 %%%%4 %[Th ,z] = DN2221_lab3_FD (N,v) solves by finite difference method the5 % following differential problem :6 % -(k*T ’) ’ + v*c*rho*T’ = Q on z in [0;L]7 % s.t. T(z=0) = T0 and -kT ’(t=L) = kv *(T(z=L)-Tout)8 % where Q = Q0*sin ((z-a)/(b-a)) on [a;b] and Q = 0 elsewhere9 %Here we solve for :

10 %L = 10;%[ m]11 %a = 1; %[m]12 %b = 3; %[m]13 %Q0 = 50;%[ W.m^ -3]14 %k = .5;%[ W/(m.K)]15 %kv = 10;%[ W/(m.K)]16 %rho = 1;%[ kg.m^ -3]17 %c = 1;%[J/(K.kg)]18 %Tout = 300;%[ K]19 %T0 = 400;%[ K]20 % ----21 %22 %Besides , the functions displays parameters about oscillations , in respect23 %the model problem : -epsilon *f’’ + f = 024 %25 %@PRE:26 % - N, the number of interval ( scalar )27 % - v, the fluid velocity in the z- direction ( scalar )28 %29 % @POST :30 % - Th , the approximate discretise solution ((N+1) x1 vector )31 % - z, the discrete interval (1x(N+1) vector )32 %%%%33 f u n c t i o n [Th ,z] = DN2221_lab3_FD (N,v)3435 %% parameters36 L = 10;%[m]37 a = 1; %[m]38 b = 3; %[m]39 Q0 = 50;%[W.m^ -3]40 k = .5;%[W/(m.K)]41 kv = 10;%[W/(m.K)]42 rho = 1;%[kg.m^ -3]43 c = 1;%[J/(K.kg)]44 Tout = 300;%[K]45 T0 = 400;%[K]464748 % discretization49 z = l i n s p a c e (0,L,N+1);50 h = L/N;515253 % building discrete system54 Q = z e r o s (1,N+1);55 Q(z>a&z<b) = Q0 * s i n ( (z(z>a&z<b)-a)/(b-a) * pi );5657 b = [T0 ((2*h^2)*Q(2:end -1)) (2*h*kv/k)*Tout ]’;585960 d0 = [1 (4*k * ones (1,N -1)) (3+2* h*kv/k)]’;61 d1 = [0 0 (h*rho*c*v -2*k)*ones (1,N -1) ]’;62 dminus1 = [ -(2*k+h*rho*c*v)*ones (1,N -1) -4 0] ’;6364 A = spd iags ([ d0 d1 dminus1 ] ,[0 1 -1],N+1,N+1);65 A(end ,end -2) = 1;

8

Page 11: Numerical Solution of a Boundary Value Problem

666768 % solve discrete system69 Th = A\b;707172 epsilon = k/(v*rho*c);73 i f epsilon ~= inf74 f p r i n t f ([ ’epsilon = ’ num2str( epsilon )])75 f p r i n t f ([ ’, Pe = ’ num2str(h/(2* epsilon ))])76 f p r i n t f ([ ’, N = ’ num2str(N)])77 f p r i n t f ([ ’, v = ’ num2str(v) ’\n’])78 end798081 end

9