Problem Sheet 6 result of Explicit Euler Method(blue circle), Implicit Euler Method(red triangle)...

download Problem Sheet 6 result of Explicit Euler Method(blue circle), Implicit Euler Method(red triangle) and

of 12

  • date post

    25-Apr-2020
  • Category

    Documents

  • view

    9
  • download

    0

Embed Size (px)

Transcript of Problem Sheet 6 result of Explicit Euler Method(blue circle), Implicit Euler Method(red triangle)...

  • H. Ammari A. Scapin A. Vanel

    Spring Term 2019

    Numerical Analysis II ETH Zürich

    D-MATH

    Problem Sheet 6

    Problem 6.1 Implicit Mid-Point Rule vs. Explicit and Implicit Euler Meth- ods

    We consider the ordinary differential equation of second order ẍ = − sinx for the initial values x(0) = π/2, ẋ(0) = 0.

    (6.1a) Rewrite the problem into an IVP of first-order ODE system.

    Solution: Let x(1) = x, x(2) = ẋ, x = (x(1), x(2)), the original problem can be rewritten as ẋ = f(x), where

    f

    ( x(1) x(2)

    ) =

    ( x(2)

    − sinx(1)

    ) (6.1b) Try to solve the IVP using the Explicit Euler Method,

    xk+1 = xk + hf(tk, x k) (6.1.1)

    by filling in templates EulerSolve.m and EulerStep.m.

    Solution: The following code works for solving by Explicit Euler Method.

    Listing 6.1: Implementation of EulerSolve.m 1 f u n c t i o n [t,y] = EulerSolve(f,y0,T,h) 2 %EULERSOLVE explicit euler ODE solver

    3 % [T,Y] = EULERSOLVE(F,Y0,[T0 T1],H) solves the ODE

    y’=f(t,y) over the

    4 % interval [T0 T1] with y(T0) = Y0 using the explicit Euler

    method with

    5 % step size (at most) H. The input argument F should be a

    function

    6 % handle taking the arguments (t,y) and returning a column

    vector

    7 % corresponding to f(t,y).

    8

    9 % C.J. Gittelson / SAM ETHZ / 2008

    10

    11 % Initialize variables

    12 N = c e i l ((T(2)-T(1))/h); % number of steps

    Problem Sheet 6 Page 1 Problem 6.1

  • 13 h = (T(2)-T(1))/N; % adjust step size to fit interval length 14 d = s i z e(y0,1); % dimension of solution 15 t = l i n s p a c e(T(1),T(2),N+1); % time grid 16 y = nan(d,N+1); % solution 17 y(:,1) = y0; % set initial value 18

    19 % Compute solution

    20 f o r j=1:N 21 y(:,j+1) = EulerStep(f,y(:,j),t(j),h); 22 end 23

    24 re turn

    The EulerStep is implemented as follows:

    Listing 6.2: Implementation of EulerStep.m 1 f u n c t i o n y1 = EulerStep(f,y0,t0,h) 2 %EULERSTEP step of explicit Euler method

    3 % Y1 = EULERSTEP(F,Y0,T0,H) computes the approximate

    solution Y1 at time

    4 % T0+H using one step of the explicit Euler method starting

    at Y0. F

    5 % should be a function handle taking the arguments (t,y)

    and returning a

    6 % column vector corresponding to f(t,y).

    7

    8 % C.J. Gittelson / SAM ETHZ / 2008

    9

    10 y1 = y0 + h*f(t0,y0); 11

    12 re turn

    (6.1c) Try to solve the IVP using the Implicit Euler Method,

    xk+1 = xk + hf(tk+1, x k+1) (6.1.2)

    by filling in templates ImpEulerSolve.m and ImpEulerStep.m.

    Use a fixed-point iteration to solve xk+1 out of the implicit equation, i.e.

    xk+1new = x k + hf(tk+1, x

    k+1 old )

    where xk+1new is the new guessing of x k+1, and xk+1old is the old guessing of x

    k+1.

    Solution: The following code works for solving by Implicit Euler Method.

    Listing 6.3: Implementation of ImpEulerSolve.m 1 f u n c t i o n [t,y] = ImpEulerSolve(f,y0,T,h,tol) 2 %IMPEULERSOLVE imlpicit euler ODE solver

    Problem Sheet 6 Page 2 Problem 6.1

  • 3 % [T,Y] = IMPEULERSOLVE(F,Y0,[T0 T1],H,TOL) solves the ODE

    y’=f(t,y) over

    4 % the interval [T0 T1] with y(T0) = Y0 using the implicit

    Euler method

    5 % with step size (at most) H. The input argument F should

    be a function

    6 % handle taking the arguments (t,y) and returning a column

    vector

    7 % corresponding to f(t,y).

    8

    9 % C.J. Gittelson / SAM ETHZ / 2008

    10

    11 % Initialize variables

    12 N = c e i l ((T(2)-T(1))/h); % number of steps 13 h = (T(2)-T(1))/N; % adjust step size to fit interval length 14 d = s i z e(y0,1); % dimension of solution 15 t = l i n s p a c e(T(1),T(2),N+1); % time grid 16 y = nan(d,N+1); % solution 17 y(:,1) = y0; % set initial value 18

    19 % Compute solution

    20 f o r j=1:N 21 y(:,j+1) = ImpEulerStep(f,y(:,j),t(j),h,tol); 22 end 23

    24 re turn

    The ImpEulerStep is implemented as follows:

    Listing 6.4: Implementation of ImpEulerStep.m 1 f u n c t i o n y1 = ImpEulerStep(f,y0,t0,h,tol) 2 %IMPEULERSTEP step of implicit Euler method

    3 % Y1 = IMPEULERSTEP(F,Y0,T0,H,F0,TOL) computes the

    approximate

    4 % solution Y1 at time T0+H and the corresponding value of F

    using one

    5 % step of the implicit Euler method starting at Y0. F

    should be a

    6 % function handle taking the arguments (t,y) and returning

    a column vector

    7 % corresponding to f(t,y) and F0 is the evaluation at

    (T0,Y0) (computed

    8 % at the previous step). The solution is computed up to a

    tolerance of

    9 % TOL, weighted with 1/H.

    10

    11 % C.J. Gittelson, D.D. Wright, M. Cada / SAM ETHZ / 2008

    Problem Sheet 6 Page 3 Problem 6.1

  • 12

    13 % Initialize

    14 t1 = t0 + h; 15

    16 % Initial estimate

    17 f0 = f(t0,y0); 18 Y1 = y0 + h*f0; 19

    20 errNorm = norm(Y1 - y0); 21

    22 % Fixed point iteration

    23 i = 0; 24 whi le errNorm > h*tol 25 Y1old = Y1; 26 Y1 = y0 + h*f(t1,Y1); % y0 constant 27 errNorm = norm(Y1 - Y1old); 28 i = i+1; 29 i f (i>=1e5) 30 f p r i n t f(’Fixed point iteration terminated after %d steps

    with error %g\n’,i,errNorm); 31 break 32 end 33 end 34

    35 % Define solution at next timestep

    36 y1 = Y1; 37

    38 re turn

    (6.1d) Try to solve the IVP using the Implicit Mid-point Method,

    xk+1 = xk + hf

    ( tk +

    h

    2 , 1 2 (xk + xk+1)

    ) . (6.1.3)

    There are two implementations of the implicit mid-point rule. The first version uses the same fixed-point iteration as the one implemented in ImpEulerStep.m. Finish the matlab template ImpMidPointStep.m by referring ImpEulerStep.m. The function ImpMidPointStep is called in ImpMidPointSolve.m.

    The second version uses half an implicit Euler step and half an explicit Euler step. The two half-steps are given by

    xk+ 1 2 := xk +

    h

    2 f

    ( tk +

    h

    2 , xk+

    1 2

    ) , implicit Euler with step size

    h

    2 ,

    xk+1 := xk+ 1 2 +

    h

    2 f

    ( tk +

    h

    2 , xk+

    1 2

    ) , explicit Euler with step size

    h

    2 .

    Problem Sheet 6 Page 4 Problem 6.1

  • Prove that the second version is equivalent to the first version, i.e. one step of the implicit mid- point rule

    xk+1 = xk + hf

    ( tk +

    h

    2 , xk + xk+1

    2

    ) ,

    and implement it using template ImpMidPointSolveWithEuler.m. You may want to use the function EulerStep and ImpEulerStep you just defined.

    Solution: The equivalence can be seen as follows: Inserting xk+ 12 in the second equation, we obtain

    xk+1 = xk + hf

    ( tk +

    h

    2 , xk+

    1 2

    ) ,

    and, adding xk to both sides and dividing by 2, we see that

    xk+1 + xk

    2 = xk +

    h

    2 f

    ( tk +

    h

    2 , xk+

    1 2

    ) =: xk+

    1 2 .

    The following code works for solving by Implicit Midpoint method.

    Listing 6.5: Implementation of ImpMidPointSolve.m 1 f u n c t i o n [t,y] = ImpMidPointSolve(f,y0,T,h,tol) 2 %IMPMIDPOINTSOLVE implicit mid-point ODE solver

    3 % [T,Y] = IMPMIDPOINTSOLVE(F,Y0,[T0 T1],H,TOL) solves the

    ODE y’=f(t,y) over

    4 % the interval [T0 T1] with y(T0) = Y0 using the implicit

    mid-point method

    5 % with step size (at most) H. The input argument F should

    be a function

    6 % handle taking the arguments (t,y) and return a column

    vector corresponding

    7 % to f(t,y).

    8

    9 % D.D. Wright, C.J. Gittelson / SAM ETHZ / 2008

    10

    11 % Initialize variables

    12 N = c e i l ((T(2)-T(1))/h); % number of steps 13 h = (T(2)-T(1))/N; % adjust step size to fit interval length 14 d = s i z e(y0,1); % dimension of solution 15 t = l i n s p a c e(T(1),T(2),N+1); % time grid 16 y = nan(d,N+1); % solution 17 y(:,1) = y0; % set initial value 18

    19 % Compute solution

    20 f o r j=1:N 21 y(:,j+1) = ImpMidPointStep(f,y(:,j),t(j),h,tol); 22 end 23

    24 re turn

    Problem Sheet 6 Page 5 Problem 6.1

  • The code for one step of Implicit Midpoint method is shown in Listing 6.6.

    Listing 6.6: Implementation of ImpMidPointStep.m 1 f u n c t i o n y1 = ImpMidPointStep(f,y0,t0,h,tol) 2 %IMPMIDPOINTSTEP step of implicit Euler method

    3 % Y1 = IMPMIDPOINTSTEP(F,Y0,T0,H,F0,TOL) computes the

    approximate

    4 % solution Y1 at time T0+H and the corresponding value of F

    using one

    5 % step of the implicit mid-point rule starting at Y0. F

    should be a

    6 % function handle taking the arguments (t,y) and return a

    column vector

    7 % corresponding to f(t,y) and F0 is the evaluation at

    (T0,Y0) (computed

    8 % at the previous step). The solution is computed up to a

    tolerance of

    9 % TOL, weighted with 1/H.

    10

    11 % C.J. Gittelson, D.D. Wright, M. Cada / SAM ETHZ / 2008

    12

    13 % Initial estimate

    14 f0 = f(t0,y0); 15 Y1 = y0 + h*f0; 16 f1 = f(t0+h/2,(Y1+y0)/2); 17 errNorm = norm(Y1 - y0); 18

    19 % Fixed point iteration

    20 i = 0; 21 whi le errNorm > tol 22 Y1old = Y1; 23 Y1 = y0 + h*f(t0+h/2,(y0+Y1)/2); % y0 constant 24 errNorm = norm(Y1 - Y1old); 25 i = i+1; 26 i f (i>=1e5) 27 f p r i n t f(’F