pde of parabolic type

Upload: lambdastudentepl

Post on 17-Oct-2015

98 views

Category:

Documents


0 download

DESCRIPTION

Computer Lab 4 Supporting Chapter 3 of "Introduction to Computation and Modeling for Differential Equations"

TRANSCRIPT

  • ! !"#$%&&$$'()* +"#*',-%.,',/$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!

    !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 hgskolan !

    KTH SCI, Lindstedtsvgen 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 4 - Partial Differential Equation of Parabolic Type December 5th 2013

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

    Signature(s):

    Teacher of thecourses 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]

  • Contents

    1 Definition of dimensionless variables . . . . . . . . . . . . . . . . . . . . . . . . . 12 Space discretization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Numerical solution using Eulers explicit . . . . . . . . . . . . . . . . . . . . . . 54 ode23 vs ode23s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Jacobian from the discretization . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10A Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    A.1 Second order approximation of second derivative . . . . . . . . . . . . . . 13A.2 Second order approximation of first derivative . . . . . . . . . . . . . . . 13A.3 DN2221_lab4_mol.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14A.4 DN2221_lab4_euler.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

    0

  • The purpose of this paper is to analyze the numerical solutions to the following differentialequation describing the evolution of the temperature in a rod:

    CpT

    t= k

    2T

    x2, t > 0, 0 x L (1)

    with the boundary conditions

    T (0, t) ={T0 if 0 t tp0 if t > tp

    , Tx

    (L, t) = 0

    and initial conditionT (x, 0) =

    {T0 if x = 00 if 0 < x L

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

    1 Definition of dimensionless variablesLet us introduce the following variables u, and :

    T = T0u, x = L, t = tp (2)

    Then, we can formulate the following derivatives of u:

    u

    (, ) = tp

    T0

    T

    t(L, tp)

    u

    (, ) = L

    T0

    T

    x(L, tp)

    2u

    2(, ) = L

    2

    T0

    2T

    x2(L, tp)

    Substituting Tt

    and 2T

    x2in equation (1), we have:

    CpT0tp

    u

    = k T0

    L22u

    2

    u

    = ktp

    CpL22u

    2

    The problem becomes thus:

    u

    = a

    2u

    2, > 0, 0 < < L

    L= 1 (3)

    with boundary conditions:

    u(0, ) ={

    T0T0

    = 1 if 0 tptp

    = 10 if > 1 ,

    u

    (1, ) = 0

    and initial condition:u(, 0) =

    {T0T0

    = 1 if = 00 if 0 < L

    L= 1

    1

  • In this reformulated problem, a = ktpCpL2

    is a dimensionless parameter as

    [a] = Jm sC s

    1kgm3

    JkgC

    m2= 1

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

    2

  • 2 Space discretizationWe first discretize the space axis with constant time step hx between = 0 and = 1 in thefollowing manner

    i = ihx, 0 i Mwhere hx = 1M .

    We introduce a semi-discretization u(i, t) ' ui(t) where ui is a time-dependent function asso-ciated to the space point i. We use an approximation of order 2 of the second space derivativeof the function:

    2u

    2(i, ) =

    u(i+1, ) 2u(i, ) + u(i1, )h2x

    +O(h2x)

    The way to obtain the equation above is explained in appendix A.1.

    In discretized form, the equation becomes:

    duid

    = 1h2x

    (ui+1 2ui + ui1), i = 2, ...,M 1 (4)

    We still have to express the time derivative for i = 1 and i = M . In i = 1, we have:

    du1d

    = 1h2x

    (u2 2u1 + ()

    where () is defined by the boundary condition in = 0:

    () ={

    1 if 0 10 if > 1

    In M , we introduce a ghost point and write:

    duMd

    = 1h2x

    (uM+1 2uM + uM1) (5)

    the value at the ghost point M+1 = (M + 1)hx = 1 + hx is determined by the discretized formof Neumann boundary condition in M :

    u

    (1, ) = 0

    u

    (1, ) = u(1 + hx, ) u(1 hx, )2hx +O(h

    2x)

    (6)

    The proof of this formula is given in appendix A.2. Combining the boundary condition andthe numerical approximation of the time derivative in (1, ), we have:

    uM+1 uM12hx

    = 0 uM+1 = uM1

    (7)

    3

  • This equation implies thatduMd

    = 1h2x

    (2uM1 2uM)

    Summarizing all the equations above, we obtain:

    duid

    = 1h2x

    (ui+1 2ui + ui1), i = 2, ...,M 1du1d

    = 1h2x

    (u2 2u1 + ()duMd

    = 1h2x

    (2uM1 2uM)

    (8)

    Obviously, we do not need to approximate the time derivative in (0, ) as we already now thevalue of the function at this point, u(0, ) = (). This system can be summarized in thefollowing matricial form:

    dud

    = Au + b(), u(0) = u0

    where u() is a M 1 vector , A is of a M M matrix , b() is a M 1 vector and u0 is aM 1 vector, according to the following formulas:

    u() =

    u1()

    ...uM()

    A = 1h2x

    2 11 2 1

    . . .1 2 1

    2 2

    b() = 1h2x

    ()

    0...0

    u0 =

    0...0

    4

  • 3 Numerical solution using Eulers explicitIf we discretize the ODE system of the preceding section with Eulers explicit method withconstant 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 ish2x 12 . Indeed, the stability condition of the difference equation (9) is:

    |1 + j| 1, for all eigenvalues j of A (10)

    As the eigenvalues of A are1:

    j(A) = 4h2x

    sin2(jpihx

    2

    ), j = 1, 2, ..., N

    we must satisfy (in the worst case): 1 + h2x 1

    which is satisfied if and only if:h2x 12

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

    1EDSBERG (L.), Introduction to Computation and modeling for differential equations, ed. WILEY, NewJersey, 2008, p.115.

    5

  • Figure 1: Graph of the numerical solution for hx = 0.01 and = 0.25h2x ' 2.5 105 (stablethus) and in "normalized" time interval [0, 2].

    Figure 2: Graph of the numerical solution for hx = 0.05 and = 0.5h2x ' 1.25 103 (stablethus) and in "normalized" time interval [0, 2].

    6

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

    7

  • 4 ode23 vs ode23sDN2221_lab4_mol.m2 discretizes the space dimension of the PDE (3), i.e. we get:

    dudt

    = Au + b() (11)

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

    1 ode__(@(t,u,m)DN2221_lab4_mol(t,u,M) ,[0 2],u0);

    where M is the number of space intervals used for the space discretization, [0 2] is the timeinterval for the time integration performed by ode__ and u0 = zeros(M,1) is the initial con-dition. We use the default tolerance3.

    Let us compare the computations of ode23.m and ode23s.m for different values of M. We focusour comparison on the number of used time steps, the spent cpu-time and the maximal usedstepsize. Here are the results of our experiments:

    number of time steps : [/] cpu-time : [s] hmax : [/]M ode23 ode23s ode23 ode23s ode23 ode23s10 346 106 0.412748 0.570928 0.0169 0.147420 1296 126 1.045003 0.747145 0.0043 0.156040 5114 168 3.523565 2.461859 9.8187e 04 0.1403

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

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

    8

  • 5 Jacobian from the discretizationIn the previous section, we concluded that the implicit method ode23s.m is more efficientthan the explicit method ode23.m, owing to the numerical stability. As ode23s.m is an implicitmethod, it means it solves a linear system in order to compute the following time step. Since ourspace discretization uses a tridiagonal matrix A, it should be fast to solve the linear system.Unfortunately, it turns out that ode23s.m does not benefit 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 eitheroption jacobian which means the jacobian is a function of time and the function of thePDE, either the option jconstant which means the jacobian is a constant parameter, eitherthe option jpattern which means the jacobian is a sparse matrix4. We use these options bywriting:

    1 A = M^2 * spd iags ([[ ones(1,M-2) ,2,2];-2*ones(1,M);ones(1,M)],[-1,0,1],M,M);2 option = odeset(j__,A);3 ode23s(@(t,u,m)DN2221_lab4_mol(t,u,M) ,[0 2],u0 ,option);

    Since we just change the way to solve the linear system, the number of time step and themaximum step size should not change. It just the cpu-time that should change. Here are theresults of our experiments:

    cpu-time : [s]M Jacobian Jconstant Jpattern10 0.477388 0.659910 0.52321320 0.444353 0.967352 0.56088940 0.469736 2.594646 0.656076

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

    4This matrix S is such that S(i,j)=1 if component i of dudt (t, u) depends on component j of u, and 0otherwise. Actually, if S(i, j) = 1, we may set S(i, j) = a 6= 0.

    5The cpu-time is approximately equal to the one we had in the previous section.

    9

  • 6 ConclusionBefore concluding, let observe what we get with our explicit Euler method6. On figure 4, weobserve that for = {.5; 1} the temperature is increasing through the rod according to theheat diffusion equation. For > 1, we notice that the heat pulse is shut down and thereforethe temperature is decreasing, still according to the heat diffusion equation. In both cases, theslope of the curve in = 1 is zero, as it is expected owing to the isolation.

    0 0.5 10

    0.2

    0.4

    0.6

    0.8

    1

    xi

    u

    tau = 0.5

    0 0.5 10

    0.2

    0.4

    0.6

    0.8

    1

    xi

    u

    tau = 1

    0 0.5 10

    0.2

    0.4

    0.6

    0.8

    1

    xi

    u

    tau = 1.5

    0 0.5 10

    0.2

    0.4

    0.6

    0.8

    1

    xi

    u

    tau = 2

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

    Figure 5 displays the solution u(, ) computed by the explicit Euler method.

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

    10

  • 00.20.40.60.811.21.41.61.82

    0

    0.5

    1

    0

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

    0.8

    0.9

    1

    tauxi

    uh(x

    i,tau)

    Figure 5: u( ; ) returned by explicit Euler, with 11 points for the space discretization and1001 for the time discretization.

    From the previous results, we formulate the three following observations:

    1. It is better to use a stiff method (namely, ode23s.m, an implicit method) than a non-stiffmethod (namely, ode23.m, an explicit method). In the case of an equation involvingspace derivatives of order 2, we have seen that the semi-discretization with centereddifferentiation of order 2 gives a linear system of ODEs. The matrix associated to thissystem is a tridiagonal matrix with real eigenvalues of very different sizes. In general,to solve parabolic problems with the method of lines, stiff numerical methods should bemore efficient owing to our experiment on the model problem.

    2. If we use a centered differentiation formula to approximate the space derivative (or order2 here), the obtained for the semi-discretized problem is obviously a banded matrix (here,tridiagonal). This is because the differentiation formula in each point uses the value ofthe 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. Thethree MATLAB methods available with odeset.m are Jacobian, JConstant and JPattern.

    7For a second derivative in (i, ), the formula uses u(i+1, ), u(i, ) and (i1, ). For a fourth derivativein (i, ), the formula uses u(i+2, ), u(i+1, ), u(i, ), u(i1, ) and (i2, ). In general, for a derivative oforder n, the method uses n+ 1 points in the neighborhood of (i, ).

    11

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

    8This method should be used when the jacobian is known to be sparse but its entries are unknown.9Unfortunately, MATLAB documentation of odeset.m does not give any information about the op-

    tion JConstant. The following document gives a short description of this option: ANONYMOUS,Ordinary Differential Equations, p.27,http://www.jhu.edu/motn/relevantnotes/usingmatlab_ode.pdf,consulted on the 4th of december 2013.

    12

  • A AppendixA.1 Second order approximation of second derivativeWe want to compute a discrete approximation of the second derivative of u in each point (, )10.Let us now give an approximate expression for d2T

    dz2 (zi):

    u(i+1, ) = u(i, ) + hdudz (i, ) +h2

    2d2udz2 (i, ) +

    h3

    6d3udz3 (i, ) + C1h

    4

    u(i1, ) = u(i, ) hdud (i, ) + h2

    2d2ud2 (i, ) h

    3

    6d3ud3 (i, ) + C2h

    4

    The sum of these two expressions gives:

    d2u

    d2(zi) =

    u(i+1, ) 2u(i, ) + u(i1, )h2

    +O(h2)

    A.2 Second order approximation of first derivativeWe want to compute a discrete approximation of the first and second derivative of u in eachpoint (1, )11.

    u(1 + h, ) = u(1, ) + hdud

    (1, ) + h22d2ud2 (1, ) + C1h

    3

    u(1 h, ) = u(1, ) hdud

    (1, ) + h22d2Td2 (1, ) + C2h

    3

    The difference between these two expressions gives:

    du

    d(1, ) = u(1 + h, ) u(1 h, )2h +O(h

    2)

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

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

    13

  • A.3 DN2221_lab4_mol.m

    1 %DN2221 - lab42 %Beaufort & Van Lierde3 %%4 %DN2221_lab4_mol(t,u,M) discretize the sapce dimension of the following5 %parabolic problem6 %----7 %du/dt = d^2u/dx^28 %for x in (0;1)9 %----10 %by using a (central) finite difference method of second order11 %12 %@PRE:13 %- t is the time for evaluating the discretization14 %- u is the space discrete value of u(t,x)15 %- M is the number of intervals for discretizing the space interval (0,1)16 %@POST:17 %- dudt is the time derivative that is approximated by the discretization18 %of the second order derivative in space19 %%20 f unc t i on dudt = DN2221_lab4_mol(t,u,M)21 hx = 1/M;22 dudt= z e ro s (M,1);23 dudt (1)=(u(2) -2*u(1)+(t

  • 36 end

    15

    Definition of dimensionless variablesSpace discretizationNumerical solution using Euler's explicitode23 vs ode23sJacobian from the discretizationConclusionAppendixSecond order approximation of second derivativeSecond order approximation of first derivativeDN2221_lab4_mol.mDN2221_lab4_euler.m