You are on page 1of 17

!

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

! ! !
Name, personal number, program and email for all345 students behind this submission : "#$%! !&!#'(#)*+&,-!./0%&%1 "#!2*/,.%%3$$*/4!2/.1/#$!! !*52.,+! ! Hadrien VAN LIERDE, 920909-T058, master in mathematical engineering, hvl@kth.se

+"#*)--)*$&0(2,&2#*$3.*$&6#*7)83.*(2,,)*',-%.,',/9* Pierre-Alexandre BEAUFORT, 920104-T093, master in mathematical engineering, beaufort@kth.se !

Course code, course name and program or course set : DN2221, Applied numerical methods - part 1, master of sciences in engineering

Teacher: Lennart EDSBERG Task name: :0#$83(;*80#$,).,*345*1#3/#).*2--2#*80#$3./6,/9 ! Computer Lab 4 - Partial Dierential Equation of Parabolic Type

Examiner: Ashraful KADIR Deadline: December 5th 2013

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

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

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

Teacher of the ?11/'+&2,$* ,).,9 ! courses signature:

Assessment/Rating:

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

!
!

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

Kungliga Tekniska hogskolan KTH SCI, Lindstedtsvagen 25, 100 44 Stockholm. Tel: 08-790 80 50. Fax: 08-723 17 88. e-mail : elevexp@math.kth.se

! ! !

Contents
1 2 3 4 5 6 A Denition of dimensionless variables . . . . . . . . . . . Space discretization . . . . . . . . . . . . . . . . . . . . Numerical solution using Eulers explicit . . . . . . . . ode23 vs ode23s . . . . . . . . . . . . . . . . . . . . . Jacobian from the discretization . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . Appendix . . . . . . . . . . . . . . . . . . . . . . . . . A.1 Second order approximation of second derivative A.2 Second order approximation of rst derivative . A.3 DN2221_lab4_mol.m . . . . . . . . . . . . . . . A.4 DN2221_lab4_euler.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 3 5 8 9 10 13 13 13 14 14

The purpose of this paper is to analyze the numerical solutions to the following dierential equation describing the evolution of the temperature in a rod: Cp with the boundary conditions T (0, t) = and initial condition T (x, 0) = T0 0 if 0 t tp if t > tp T0 0 , T (L, t) = 0 x 2T T = k 2 , t > 0, 0 x L t x (1)

if x = 0 if 0 < x L

where L is the length of the rod [m], is the density [kg/m3 ], Cp is the heat capacity [J/(kg C ), k is the thermal conductivity [J/(m s C )] of the rod and T0 is the temperatuire of a heat impulse hitting the left end for a duration tp .

Denition of dimensionless variables


T = T0 u, x = L , t = tp (2)

Let us introduce the following variables u, and :

Then, we can formulate the following derivatives of u: u tp T (, ) = (L, tp ) T0 t u L T (, ) = (L, tp ) T0 x L2 2 T 2u ( , ) = (L, tp ) 2 T0 x2 Substituting T 2T and in equation (1), we have: t x2 Cp T0 u T0 2 u = k 2 2 tp L ktp 2 u u = Cp L2 2

The problem becomes thus: u 2u L = a 2 , > 0, 0 < < = 1 L with boundary conditions: u(0, ) = and initial condition: u(, 0) = 0
T0 T0

(3)

=1

if 0 if > 1
T0 T0

tp tp

=1

u (1, ) = 0

=1 1

if = 0 if 0 <

L L

=1

In this reformulated problem, a = [ a] =

ktp is a dimensionless parameter as Cp L2 1


kg m3

J s msC

J kg C

m2

=1

From now, we will assume that a has the numerical value a = 1.

Space discretization

We rst discretize the space axis with constant time step hx between = 0 and = 1 in the following manner i = ihx , 0 i M where hx =
1 . M

We introduce a semi-discretization u(i , t) ui (t) where ui is a time-dependent function associated to the space point i . We use an approximation of order 2 of the second space derivative of the function: u(i+1 , ) 2u(i , ) + u(i1 , ) 2u (i , ) = + O(h2 x) 2 2 hx The way to obtain the equation above is explained in appendix A.1. In discretized form, the equation becomes: 1 dui = 2 (ui+1 2ui + ui1 ), i = 2, ..., M 1 d hx We still have to express the time derivative for i = 1 and i = M . In i = 1, we have: du1 1 = 2 (u2 2u1 + ( ) d hx where ( ) is dened by the boundary condition in = 0: ( ) = 1 0 if 0 1 if > 1 (4)

In M , we introduce a ghost point and write: 1 duM = 2 (uM +1 2uM + uM 1 ) d hx (5)

the value at the ghost point M +1 = (M + 1)hx = 1 + hx is determined by the discretized form of Neumann boundary condition in M : u (1, ) = 0 u u(1 + hx , ) u(1 hx , ) (1, ) = + O(h2 x) 2hx (6)

The proof of this formula is given in appendix A.2. Combining the boundary condition and the numerical approximation of the time derivative in (1, ), we have: uM +1 uM 1 =0 2hx uM +1 = uM 1 (7)

This equation implies that

duM 1 = 2 (2uM 1 2uM ) d hx

Summarizing all the equations above, we obtain: dui 1 = 2 (ui+1 2ui + ui1 ), i = 2, ..., M 1 d hx 1 du1 = 2 (u2 2u1 + ( ) d hx duM 1 = 2 (2uM 1 2uM ) d hx

(8)

Obviously, we do not need to approximate the time derivative in (0, ) as we already now the value of the function at this point, u(0, ) = ( ). This system can be summarized in the following matricial form: du = Au + b( ), u(0) = u0 d where u( ) is a M 1 vector , A is of a M M matrix , b( ) is a M 1 vector and u0 is a M 1 vector, according to the following formulas: u1 ( ) . u( ) = . . uM ( )

A=

1 h2 x

2 1 1 2

1 ... 1

2 1 2 2

( ) 0 1 b( ) = 2 . . hx . 0

u0 =

. . .

0 0

Numerical solution using Eulers explicit

If we discretize the ODE system of the preceding section with Eulers explicit method with constant time-step , we obtain the following numerical scheme: uk+1 = uk + (Auk + b(k )), k = 1, 2, ... (9)

where k = k and uk u(k ). One can show that the stability condition of this scheme is 1 . Indeed, the stability condition of the dierence equation (9) is: h2 2
x

|1 + j | 1, for all eigenvalues j of A As the eigenvalues of A are1 : j (A) = we must satisfy (in the worst case): 1+ which is satised if and only if: 4 jhx , j = 1, 2, ..., N sin2 2 hx 2 1 h2 x

(10)

1 h2 2 x

In the gures below, we plotted three graphs showing the numerical solutions. For the three cases, we chose parameter values of hx = 0.01 and = 0.25h2 2.5 105 (gure 1), hx = x 0.05 and = 0.5h2 1.25 103 (gure 2) and nally, hx = 0.05 and = 0.502hx 0.0013 x (gure 2). The two rst cases give stable solutions (even if we are actually on the stability limit for the second one). The third choice of value gives an unstable solution as expected and as we can see on the graph. In the two rst cases, we choose a very small value of the space step to show how small the time step must be. Yet, it is possible to solve the problem with such a small time step but the cost in time is much higher (around 10 seconds) than for the second case (less precision but cost in time of 0.5 seconds). The speed of the program was improved by doing the operation uk + (Auk + b(k )) in a vector form instead of a product of a matrix and a vector (see the MATLAB functions used are in appendix A.3 and A.4).

EDSBERG (L.), Introduction to Computation and modeling for dierential equations, ed. WILEY, New Jersey, 2008, p.115.

Figure 1: Graph of the numerical solution for hx = 0.01 and = 0.25h2 x thus) and in "normalized" time interval [0, 2].

2.5 105 (stable

Figure 2: Graph of the numerical solution for hx = 0.05 and = 0.5h2 x thus) and in "normalized" time interval [0, 2].

1.25 103 (stable

Figure 3: Graph of the numerical solution for hx = 0.05 and = 0.502hx thus) and in "normalized" time interval [0, 2].

0.0013 (unstable

ode23 vs ode23s
du = Au + b( ) dt

DN2221_lab4_mol.m2 discretizes the space dimension of the PDE (3), i.e. we get: (11)

We can then use the MATLAB functions ode23.m and ode23s.m in order to integrate (11) over the time. Observe that ode23.m is an explicit method while ode23s.m is implicit. We use these functions by writing:
1

ode__ ( @ (t ,u , m ) D N2 22 1_ l ab 4_ mo l (t ,u , M ) ,[0 2] , u0 ) ;

where M is the number of space intervals used for the space discretization, [0 2] is the time interval for the time integration performed by ode__ and u0 = zeros(M,1) is the initial condition. We use the default tolerance3 . Let us compare the computations of ode23.m and ode23s.m for dierent values of M. We focus our comparison on the number of used time steps, the spent cpu-time and the maximal used stepsize. Here are the results of our experiments: M 10 20 40 number of time steps : [/] ode23 ode23s 346 106 1296 126 5114 168 cpu-time : [s] ode23 ode23s 0.412748 0.570928 1.045003 0.747145 3.523565 2.461859 hmax : [/] ode23 ode23s 0.0169 0.1474 0.0043 0.1560 9.8187e 04 0.1403

First, we notice that the number of time steps for the implicit method is much smaller than for ode23.m. Since the explicit method needs more time steps, it needs more cpu-time for large numbers of space intervals. It is due to the fact that ode23.m has to use very small time steps for large numbers of space intervals in order to ensure the stability, while the implicit method does not as it is unconditionally stable for every time stepsize.

2 3

see appendix A.3 relative error tolerance is 1e 3

Jacobian from the discretization

In the previous section, we concluded that the implicit method ode23s.m is more ecient than the explicit method ode23.m, owing to the numerical stability. As ode23s.m is an implicit method, it means it solves a linear system in order to compute the following time step. Since our space discretization uses a tridiagonal matrix A, it should be fast to solve the linear system. Unfortunately, it turns out that ode23s.m does not benet from this sparse discretization. Therefore, we have to specify this information to ode23s.m by setting an option with odeset.m. The option is about the jacobian, which is obviously here the matrix A. We can use either option jacobian which means the jacobian is a function of time and the function of the PDE, either the option jconstant which means the jacobian is a constant parameter, either the option jpattern which means the jacobian is a sparse matrix4 . We use these options by writing:
1 2 3

A = M ^2 * s p d i a g s ([[ ones (1 ,M -2) ,2 ,2]; -2* ones (1 , M ) ; ones (1 , M ) ] ,[ -1 ,0 ,1] , M , M ) ; option = odeset ( j__ ,A ) ; ode23s ( @ (t ,u , m ) DN 22 21 _ la b4 _m o l (t ,u , M ) ,[0 2] , u0 , option ) ;

Since we just change the way to solve the linear system, the number of time step and the maximum step size should not change. It just the cpu-time that should change. Here are the results of our experiments: M 10 20 40 Jacobian 0.477388 0.444353 0.469736 cpu-time : [s] Jconstant 0.659910 0.967352 2.594646 Jpattern 0.523213 0.560889 0.656076

We observe that the cpu-time is quite low for the options Jacobian and Jpattern, but the option Jconstant is not really ecient5 .

This matrix S is such that S(i,j)=1 if component i of du dt (t, u) depends on component j of u, and 0 otherwise. Actually, if S (i, j ) = 1, we may set S (i, j ) = a = 0. 5 The cpu-time is approximately equal to the one we had in the previous section.
4

Conclusion

Before concluding, let observe what we get with our explicit Euler method6 . On gure 4, we observe that for = {.5; 1} the temperature is increasing through the rod according to the heat diusion equation. For > 1, we notice that the heat pulse is shut down and therefore the temperature is decreasing, still according to the heat diusion equation. In both cases, the slope of the curve in = 1 is zero, as it is expected owing to the isolation.
tau = 0.5 1 0.8 0.6 u 0.4 0.2 0 0 0.5 xi tau = 1.5 1 u 1 0.8 0.6 0.4 0.2 0 0 0.5 xi tau = 2 1 tau = 1

1 0.8 0.6 u 0.4 0.2 0 0 0.5 xi 1 u

1 0.8 0.6 0.4 0.2 0 0 0.5 xi 1

Figure 4: u( ; ) returned by explicit Euler with = {0.5; 1; 1.5; 2}, with 11 points for the space discretization and 1001 for the time discretization. Figure 5 displays the solution u(, ) computed by the explicit Euler method.

DN2221_lab4_euler.m performs such a method; see appendix A.4.

10

1 0.9 0.8 0.7 0.6 uh(xi,tau) 0.5 0.4 0.3 0.2 0.1 0 0 0.5 1 xi 1 0.8 0.6 0.4 0.2 0

1.8

1.6

1.4 tau

1.2

Figure 5: u( ; ) returned by explicit Euler, with 11 points for the space discretization and 1001 for the time discretization. From the previous results, we formulate the three following observations: 1. It is better to use a sti method (namely, ode23s.m, an implicit method) than a non-sti method (namely, ode23.m, an explicit method). In the case of an equation involving space derivatives of order 2, we have seen that the semi-discretization with centered dierentiation of order 2 gives a linear system of ODEs. The matrix associated to this system is a tridiagonal matrix with real eigenvalues of very dierent sizes. In general, to solve parabolic problems with the method of lines, sti numerical methods should be more ecient owing to our experiment on the model problem. 2. If we use a centered dierentiation formula to approximate the space derivative (or order 2 here), the obtained for the semi-discretized problem is obviously a banded matrix (here, tridiagonal). This is because the dierentiation formula in each point uses the value of the function in neighbor points7 . 3. As we just said, the semi-discretization gives an ODE system with a banded jacobian. It is thus interesting to use numerical methods taking this structure into account. The three MATLAB methods available with odeset.m are Jacobian, JConstant and JPattern.
For a second derivative in (i , ), the formula uses u(i+1 , ), u(i , ) and (i1 , ). For a fourth derivative in (i , ), the formula uses u(i+2 , ), u(i+1 , ), u(i , ), u(i1 , ) and (i2 , ). In general, for a derivative of order n, the method uses n + 1 points in the neighborhood of (i , ).
7

11

Concerning Jacobian to JPattern, we note that both options reduce signicantly the cost in time of the function ode23s.m: this is because the jacobian is known and not computed numerically. The option Jacobian gives even faster results than JPattern: Jacobian gives the exact value of the jacobian as a sparse matrix whereas JPattern only gives the structure of the jacobian (the jacobian is sparse and the option JPattern shows which entries of it are nonzero)8 . Finally, the option JConstant does not reduce the cost in time of ode23s. Indeed, with the option JConstant, the jacobian seems to be still computed numerically, by using an information that we provide with odeset.m.9

This method should be used when the jacobian is known to be sparse but its entries are unknown. Unfortunately, MATLAB documentation of odeset.m does not give any information about the option JConstant. The following document gives a short description of this option: ANONYMOUS, Ordinary Dierential Equations, p.27,http://www.jhu.edu/motn/relevantnotes/usingmatlab_ode.pdf, th consulted on the 4 of december 2013.
9

12

A
A.1

Appendix
Second order approximation of second derivative

We want to compute a discrete approximation of the second derivative of u in each point (, )10 . 2T (zi ): Let us now give an approximate expression for d dz 2 ( , ) + u(i+1 , ) = u(i , ) + h du dz i du u(i1 , ) = u(i , ) h d (i , ) + The sum of these two expressions gives: d2 u u(i+1 , ) 2u(i , ) + u(i1 , ) (zi ) = + O(h2 ) 2 d h2
h2 2 h2 2 d2 u ( , ) dz 2 i d2 u ( , ) d 2 i d u +h ( , ) + C1 h4 6 dz 3 i 3 3 d u h ( , ) + C2 h4 6 d 3 i
3 3

A.2

Second order approximation of rst derivative

We want to compute a discrete approximation of the rst and second derivative of u in each point (1, )11 . 2 d2 u (1, ) + h (1, ) + C1 h3 u(1 + h, ) = u(1, ) + h du d 2 d 2 2 2 d T (1, ) + h (1, ) + C2 h3 u(1 h, ) = u(1, ) h du d 2 d 2 The dierence between these two expressions gives: du u(1 + h, ) u(1 h, ) (1, ) = + O(h2 ) d 2h

In the following equations, C1 and C2 denote real constants. The presence of these terms in Taylor expansions is based on the mean value theorem. 11 In the following equations, C1 and C2 denote real constants. The presence of these terms in Taylor expansions is based on the mean value theorem.

10

13

A.3
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

DN2221_lab4_mol.m

% DN2221 - lab4 % Beaufort & Van Lierde %% % DN 22 2 1_ la b4 _ mo l (t ,u , M ) discretize the sapce dimension of the following % parabolic problem % ---% du / dt = d ^2 u / dx ^2 % for x in (0;1) % ---% by using a ( central ) finite difference method of second order % % @PRE : % - t is the time for evaluating the discr etization % - u is the space discrete value of u (t , x ) % - M is the number of intervals for discretizing the space interval (0 ,1) % @POST : % - dudt is the time derivative that is approximated by the discreti zation % of the second order derivative in space %% f u n c t i o n dudt = DN 22 21 _ la b4 _m o l (t ,u , M ) hx = 1/ M ; dudt = z e r o s (M ,1) ; dudt (1) =( u (2) -2* u (1) +( t <=1) ) / hx ^2; dudt (2: M -1) =( u (3: M ) -2* u (2: M -1) + u (1: M -2) ) / hx ^2; dudt ( M ) =2*( u (M -1) -u ( M ) ) / hx ^2; end

A.4
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 34 35

DN2221_lab4_euler.m

% DN2221 - lab4 % Beaufort & Van Lierde %% % D N 2 2 2 1 _ l a b 4 _ e u l e r (M ,N , TAU ) integrate in time the following problem % ---% du / dt = D N 22 21 _l a b4 _m ol ( tau ( n ) , uh (2: end , n ) ,M ) % ---% by using the explicit Euler method % % @PRE : % - M is the number of space intervals that discretize the space interval % (0 ,1) % - N is the number of time intervals that discretize the time interval % (0 , TAU ) % - TAU is the final time %% f u n c t i o n u = D N 2 2 2 1 _ l a b 4 _ e u l e r (M ,N , TAU ) % space and time discret ization xi = l i n s p a c e (0 ,1 , M +1) ; tau = l i n s p a c e (0 , TAU , N +1) ; % time stepsize dt = TAU / N ; % allocation uh = z e r o s ( M +1 , N +1) ; uh (1 ,1: l e n g t h ( tau ( tau <=1) ) ) = 1; % computation , according to explicit Euler method for n = 1 : N uh (2: end , n +1) = uh (2: end , n ) + dt * DN 2 22 1_ la b 4_ mo l ( tau ( n ) , uh (2: end , n ) ,M ) ; end

14

36

end

15

You might also like