cs305/mm355 maple practical sessions

Upload: abdsitt

Post on 13-Feb-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/23/2019 CS305/MM355 Maple Practical Sessions

    1/7

    CS305/MM355 Maple Practical Sessions.

    Work Sheet Five.

    Take this work sheet with you to the practical class. You are to read through it in

    the practical, reproducing for yourself all of the Maple output on the sheet. Only

    then should you attempt the exercises on the sheet at the back. If you have any

    difficulties, seek assistance from the tutor. Practical work will account for up to

    30% of the final mark.

    You can save your Maple session at any time by clicking on the 'Save As....' option

    in the 'File' tab; I suggest that you save the current sheet as 'session5.mws'.

    WHEN YOU HAVE COMPLETED THE SOLUTIONS ON EXERCISE SHEET 5, SAVE THEM IN A FILE

    CALLED 'solutions2.mws'. AT THE BEGINNING OF THE FILE, SPECIFY YOUR NAME,

    STUDENT NUMBER AND CLASS. PRINT OUT THE FILE 'solutions2.mws' AND GIVE IT TO

    THE THE LECTURER EITHER IN CLASS, IN A LABORATORY SESSION, OR BY PUSHING IT

    UNDER HIS OFFICE DOOR (MARTIN MEERE, ROOM C120, ARUS DE BRUN). THE DEADLINE FOR

    SUBMITTING YOUR WORK FOR PROBLEM SHEET FIVE IS THE 7th OF MARCH. COPYING WILL

    BE SEVERELY PUNISHED; IF YOU SHARE YOUR WORK, YOU WILL ALSO BE SHARING YOUR

    MARKS.

    The Rayleigh-Ritz Method

    We illustrate how the Rayleigh-Ritz procedure can be implemented in Maple via an example. We

    begin with a bit of revision. Consider the following boundary value problem on 0 Int({Diff(w(x),x)*Diff(u(x),x)+w(x)*u(x)-w(x)*sin(x)},x=0..1)=0;

  • 7/23/2019 CS305/MM355 Maple Practical Sessions

    2/7

    =d

    0

    1

    { }+

    d

    d

    x( )w x

    d

    d

    x( )u x ( )w x ( )u x ( )w x ( )sin x x 0

    or

    =( )B ,u w ( )l wwhere

    =( )B ,u w d

    0

    1

    { }+

    d

    d

    x( )w x

    d

    d

    x( )u x ( )w x ( )u x x

    =( )l w d0

    1

    ( )w x ( )sin x x

    We must make a choice for our approximation functions. We will consider the particular case N=3.

    Recall that our first approximation function phi0(x) must satisfy the essential boundary condition,while phi1(x), phi2(x), phi3(x) must satisfy the homogeneous form of the essential boundary

    condition. With this in mind, we make the following choice

    > restart:for i from 0 to 3 do phi[i](x):=x^i end do;

    :=( )0x 1

    :=( )1 x x

    :=( )2 x x2

    :=( )3 x x3

    In the Rayleigh Ritz method, we must solve the following equation for (c1,c2,c3):

    =

    B11 B12 B13

    B21 B22 B23

    B31 B32 B33

    c1

    c2

    c3

    F1

    F2

    F3

    where

    =Bij ( )B ,i j=Fi ( )l i ( )B ,i 0

    > for i to 3 do

    F[i]:=int(phi[i](x)*sin(x),x=0..1)-int(diff(phi[0](x),x)*diff(ph

    i[i](x),x)+phi[0](x)*phi[i](x),x=0..1)end do:

    > for i to 3 do

    for j to 3 do

    B[i,j]:=int(diff(phi[i](x),x)*diff(phi[j](x),x)+phi[i](x)*phi[j]

    (x),x=0..1);

    end do:

    end do:

    > BC:=matrix([[B[1,1],B[1,2],B[1,3]],[B[2,1],B[2,2],B[2,3]],[B[3,1

    ],B[3,2],B[3,3]]]);

  • 7/23/2019 CS305/MM355 Maple Practical Sessions

    3/7

    :=BC

    4

    3

    5

    4

    6

    5

    5

    4

    23

    15

    5

    3

    6

    5

    5

    3

    68

    35> FC:=vector([F[1],F[2],F[3]]);

    :=FC

    , , ( )sin 1 ( )cos 1

    1

    2+ ( )cos 1 2 ( )sin 1

    7

    3 5 ( )cos 1 3 ( )sin 1

    1

    4

    To find the c's, we simply multiply (inverse ofBC)*F:

    >with(linalg):c:=evalf(multiply(inverse(BC),FC));

    :=c [ ], ,-0.42916838 0.45483397 -0.16262447

    Our approximation is now given by

    > U3:=phi[0](x)+c[1]*phi[1](x)+c[2]*phi[2](x)+c[3]*phi[3](x);

    :=U3 + 1 0.42916838x 0.45483397x2 0.16262447x3It is useful for comparative purposes to calculate the exact solution. We can calculate this as

    follows:

    > deq:=-diff(u(x),x,x)+u(x)=sin(x);

    :=deq = +

    d

    d2

    x2

    ( )u x ( )u x ( )sin x

    >bcs:=u(0)=1,D(u)(1)=0;

    :=bcs ,=( )u 0 1 =( )( )D u 1 0> exact:=dsolve({deq,bcs},u(x));

    exact ( )u x1

    4

    ex

    ( ) + +2 ( )cosh 1 2 ( )sinh 1 ( )cos 1( )cosh 1

    =:=

    1

    4

    e( )x

    ( )+ +2 ( )cosh 1 2 ( )sinh 1 ( )cos 1( )cosh 1

    1

    2( )sin x+ +

    We now plot the exact solution and our approximation on the same graph for comparative

    purposes:

    >plot([U3,rhs(exact)],x=0..1);

    It is also worth plotting the error, that is, rhs(exact)-U3:

    >plot(rhs(exact)-U3,x=0..1);

  • 7/23/2019 CS305/MM355 Maple Practical Sessions

    4/7

    The Galerkin Method

    We now obtain an approximation to the same problem using the Galerkin method. In the Galerkin

    method, we begin by writing our differential equation in the general form

    =( )A u fwhereAis a differential operator. For our particular probem, we have

    > restart:A(u)=-diff(u(x),x,x)+u(x),f=sin(x);

    ,=( )A u +

    d

    d2

    x2

    ( )u x ( )u x =f ( )sin x

    We will make an approximation withN=3again. We now have to choose our approximation

    functions such that both the essential and natural boundary conditions are satisified. We make the

    following choice:

    >phi[0](x):=1; for i from 1 to 3 do

    phi[i](x):=x^(i+1)-(i+1)*x^i/i end do;

    :=( )0 x 1

    :=( )1 x x2

    2x

    :=( )2 x x3

    3

    2x

    2

    :=( )3 x x4

    4

    3x

    3

    In the Galerkin method, we must solve the following equations for the coefficients c1,c2,c3:

    =

    A11 A12 A13A

    21 A

    22 A

    23

    A31 A32 A32

    c1c

    2

    c3

    F1F

    2

    F3

    where

    :=Fi d

    0

    1

    i ( )f ( )A 0 x

    :=Aij d

    0

    1

    i ( )A j x

    We now denote byA_phi[i] the operatorAacting onphi[i].

  • 7/23/2019 CS305/MM355 Maple Practical Sessions

    5/7

    > for i from 0 to 3 do

    A_phi[i]:=-diff(phi[i](x),x,x)+phi[i](x)

    end do:

    > for i from 1 to 3 do

    F[i]:=int(phi[i](x)*(sin(x)-A_phi[0]),x=0..1)

    end do:

    > for i from 1 to 3 dofor j from 1 to 3 do

    A[i,j]:=int(phi[i](x)*A_phi[j],x=0..1)

    end do:

    end do:

    >AC:=matrix([[A[1,1],A[1,2],A[1,3]],[A[2,1],A[2,2],A[2,3]],[A[3,1

    ],A[3,2],A[3,3]]]);

    :=AC

    28

    15

    43

    60

    122

    315

    43

    60

    11

    28

    71

    280

    122

    315

    71

    280

    58

    315

    > F:=vector([F[1],F[2],F[3]]);

    :=F

    , , +

    4

    33 ( )cos 1 +

    13

    4

    7

    2( )cos 1 6 ( )sin 1

    362

    15

    59

    3( )cos 1 16 ( )sin 1

    >with(linalg):c:=evalf(multiply(inverse(AC),F));

    :=c [ ], ,0.2182873 -0.1871590 0.0367546

    > U3:=phi[0](x)+c[1]*phi[1](x)+c[2]*phi[2](x)+c[3]*phi[3](x);

    :=U3 + +1. 0.4990258000x2 0.4365746x 0.2361651333x3 0.0367546x4

    The Collocation Method

    We illustrate the method with the example discussed above. We choose the same approximation

    functions as those for the Galerkin method, that is,

    > restart:with(linalg):phi[0](x):=1:for i from 1 to 3 do

    phi[i](x):=x^(i+1)-(i+1)*x^i/i end do:

    Warning, the protected names norm and trace have been redefined andunprotected

    > U3:=phi[0](x)+c[1]*phi[1](x)+c[2]*phi[2](x)+c[3]*phi[3](x);

    :=U3 + + +1 c1 ( )x2

    2x c2

    x3

    3

    2x

    2c3

    x4

    4

    3x

    3

    The residual is given by:

    > R:=-diff(U3,x,x)+U3-sin(x);

    R 2c1

    c2

    ( )6x 3 c3 ( )12x2

    8x 1 c1

    ( )x2 2x c2

    x3

    3

    2x

    2c

    3

    x4

    4

    3x

    3 + + + +:=

    ( )sin x

  • 7/23/2019 CS305/MM355 Maple Practical Sessions

    6/7

    We choose the collocation pointsx=1/4,x=1/2,x=3/4:

    > eqnset:={subs(x=1/4,R)=0,subs(x=1/2,R)=0,subs(x=3/4,R)=0};

    eqnset = + + + 39

    16c

    1

    91

    64c

    2

    947

    768c

    31

    sin

    1

    40 = + +

    11

    4c

    11

    43

    48c

    3

    1

    4c

    2

    sin

    1

    20, ,{:=

    = + 47

    16c

    1

    123

    64c

    2

    255

    256c

    3

    1

    sin3

    40}

    > sols:=evalf(solve(eqnset,{c[1],c[2],c[3]}));

    :=sols { }, ,=c1 0.2182606603 =c3 0.0367218111 =c2 -0.1869829286> U3:=phi[0](x)+.2182606603*phi[1](x)-.1869829286*phi[2](x)+.36721

    8111e-1*phi[3](x);

    :=U3 + +1. 0.4987350532x2 0.4365213206x 0.2359453434x3 0.0367218111x4

    > eqn:=-diff(u(x),x,x)+u(x)=sin(x):bcs:=u(0)=1,D(u)(1)=0:exact:=ds

    olve({eqn,bcs},u(x)):

    >plot(rhs(exact)-U3,x=0..1);

    The Least-Squared Method

    We solve the same problem as before, but now using the least-squared method. We again have:

    ,=( )A u +

    d

    d2

    x2

    ( )u x ( )u x =f ( )sin x

    We will make an approximation withN=3again. We use the same approximaton functions as

    those for the Galerkin method:

    > restart:phi[0](x):=1:for i from 1 to 3 do

    phi[i](x):=x^(i+1)-(i+1)*x^i/i end do:

    In the least-squared method, we must solve the following equations for the coefficients c1 ,c2, c3:

    =

    A11 A12 A13

    A21 A22 A23

    A31 A32 A32

    c1

    c2

    c3

    F1

    F2

    F3

    where

    =Aij d

    0

    1

    ( )A i ( )A j x

  • 7/23/2019 CS305/MM355 Maple Practical Sessions

    7/7

    :=Fi d

    0

    1

    ( )f ( )A 0 ( )A i x

    > for i from 0 to 3 do

    A_phi[i]:=-diff(phi[i](x),x,x)+phi[i](x)

    end do;

    :=A_phi0 1

    :=A_phi1 + 2 x2

    2x

    :=A_phi2 + + 6x 3 x3

    3

    2x

    2

    :=A_phi3 + + 12x2

    8x x4

    4

    3x

    3

    > for i from 1 to 3 do

    F[i]:=int(A_phi[i]*(sin(x)-A_phi[0]),x=0..1)

    end do;

    :=F1 5 ( )cos 14

    3

    :=F2 +

    25

    4

    13

    2( )cos 1 12 ( )sin 1

    :=F3

    722

    15

    119

    3( )cos 1 32 ( )sin 1

    > for i from 1 to 3 do

    for j from 1 to 3 doA[i,j]:=int(A_phi[i]*A_phi[j],x=0..1)

    end do:

    end do:

    >AC:=matrix([[A[1,1],A[1,2],A[1,3]],[A[2,1],A[2,2],A[2,3]],[A[3,1

    ],A[3,2],A[3,3]]]);

    :=AC

    36

    5

    73

    60

    206

    315

    73

    60

    517

    140

    687

    280

    206

    315

    687

    280

    778

    315

    > F:=vector([F[1],F[2],F[3]]);

    :=F

    , ,5 ( )cos 1

    4

    3+

    25

    4

    13

    2( )cos 1 12 ( )sin 1

    722

    15

    119

    3( )cos 1 32 ( )sin 1

    >with(linalg):c:=evalf(multiply(inverse(AC),F));

    :=c [ ], ,0.2183328560 -0.18737550 0.03693630

    > U3:=phi[0](x)+c[1]*phi[1](x)+c[2]*phi[2](x)+c[3]*phi[3](x);

    :=U3 + +1. 0.4993961060x2

    0.4366657120x 0.2366239000x3

    0.03693630x4