H. AmmariA. ScapinA. Vanel
Spring Term 2019
Numerical Analysis IIETH Zurich
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 x = − sinx for the initial valuesx(0) = π/2, x(0) = 0.
(6.1a) Rewrite the problem into an IVP of first-order ODE system.
Solution: Let x(1) = x, x(2) = x, x = (x(1), x(2)), the original problem can be rewritten asx = 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, xk) (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.m1 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:N21 y(:,j+1) = EulerStep(f,y(:,j),t(j),h);22 end23
24 re turn
The EulerStep is implemented as follows:
Listing 6.2: Implementation of EulerStep.m1 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, xk+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 = xk + hf(tk+1, x
k+1old )
where xk+1new is the new guessing of xk+1, and xk+1
old is the old guessing of xk+1.
Solution: The following code works for solving by Implicit Euler Method.
Listing 6.3: Implementation of ImpEulerSolve.m1 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:N21 y(:,j+1) = ImpEulerStep(f,y(:,j),t(j),h,tol);22 end23
24 re turn
The ImpEulerStep is implemented as follows:
Listing 6.4: Implementation of ImpEulerStep.m1 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*tol25 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 break32 end33 end34
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, 12(xk + xk+1)
). (6.1.3)
There are two implementations of the implicit mid-point rule. The first version uses the samefixed-point iteration as the one implemented in ImpEulerStep.m. Finish the matlab templateImpMidPointStep.m by referring ImpEulerStep.m. The function ImpMidPointStepis called in ImpMidPointSolve.m.
The second version uses half an implicit Euler step and half an explicit Euler step. The twohalf-steps are given by
xk+12 := xk +
h
2f
(tk +
h
2, xk+
12
), implicit Euler with step size
h
2,
xk+1 := xk+12 +
h
2f
(tk +
h
2, xk+
12
), 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 usethe 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+
12
),
and, adding xk to both sides and dividing by 2, we see that
xk+1 + xk
2= xk +
h
2f
(tk +
h
2, xk+
12
)=: xk+
12 .
The following code works for solving by Implicit Midpoint method.
Listing 6.5: Implementation of ImpMidPointSolve.m1 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:N21 y(:,j+1) = ImpMidPointStep(f,y(:,j),t(j),h,tol);22 end23
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.m1 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 > tol22 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(’Fixed point iteration terminated after %d steps
with error %g\n’,i,errNorm);28 break29 end30 end31
32 % Define solution and feval at next timestep
33 y1 = Y1;34
35 re turn
For the implicit mid-point rule, version II, see Listing 6.7.
Problem Sheet 6 Page 6 Problem 6.1
Listing 6.7: Implementation of ImpMidPointSolveWithEuler.m1 f u n c t i o n [t,y] = ImpMidPointSolveWithEuler(f,y0,T,h,tol)2 %IMPMIDPOINTSOLVE imlpicit mid-point ODE solver
3 % [T,Y] = IMPMIDPOINTSOLVEWITHEULER(F,Y0,[T0 T1],H,TOL)
solves the ODE
4 % y’=f(t,y) over the interval [T0 T1] with y(T0) = Y0 using
the implicit
5 % mid-point method with step size (at most) H by performing
a ’half step’ of
6 % implicit Euler, then a ’half step’ of explicit Euler. The
input argument
7 % F should be a function handle taking the arguments (t,y)
and return a
8 % column vector corresponding to f(t,y).
9
10 % D.D. Wright, C.J. Gittelson / SAM ETHZ / 2008
11
12 % Initialize variables
13 N = c e i l ((T(2)-T(1))/h); % number of steps
14 h = (T(2)-T(1))/N; % adjust step size to fit interval length
15 d = s i z e(y0,1); % dimension of solution
16 t = l i n s p a c e(T(1),T(2),N+1); % time grid
17 y = nan(d,N+1); % solution
18 y(:,1) = y0; % set initial value
19
20 % Compute solution
21 f o r j=1:N22 yhalf = ImpEulerStep(f,y(:,j),t(j),h/2,tol);23 y(:,j+1) = EulerStep(f,yhalf,t(j)+h/2,h/2);24 end25
26 re turn
(6.1e) Now try to solve the problem using classical Runge-Kutta method of fourth order .Complete the code using template Rk4Solve.m. Classical Runge-Kutta Method of fourth orderis defined as follows:
xk+1 = xk +h
6(k1 + 2k2 + 2k3 + k4)
k1 := f(tk, xk)
k2 := f(tk +h
2, xk +
h
2k1)
k3 := f(tk +h
2, xk +
h
2k2)
k4 := f(tk + h, xk + hk3)
Solution: The implementation of RK4 is shown as follows:
Problem Sheet 6 Page 7 Problem 6.1
Listing 6.8: Implementation of RK4Solve.m1 f u n c t i o n [t,y] = RK4Solve(f,y0,T,h)2 %RK4Solve Runge-Kutta 4 ODE solver (the classic RK4)
3 % [T,Y] = RK4SOLVE(F,Y0,[T0 T1],H) solves the ODE y’=f(t,y)
over the interval
4 % [T0 T1] with y(T0) = Y0 using the classical Runge-Kutta
order 4 method (see
5 % the Butcher Table below) with step size (at most) H. The
input argument F
6 % should be a function handle taking the arguments (t,y)
and returning a
7 % column vector corresponding to f(t,y).
8 %
9 % Butcher Table:
10 % 0 |
11 % 1/2 | 1/2
12 % 1/2 | 0 1/2
13 % 1 | 0 0 1
14 % ____|________________
15 % | 1/6 1/3 1/3 1/6
16
17 % D.D. Wright, C.J. Gittelson / SAM ETHZ / 2008
18
19 % Initialize variables
20 N = c e i l ((T(2)-T(1))/h); % number of steps
21 h = (T(2)-T(1))/N; % adjust step size to fit interval length
22 d = s i z e(y0,1); % dimension of solution
23 t = l i n s p a c e(T(1),T(2),N+1); % time grid
24 y = nan(d,N+1); % solution
25 y(:,1) = y0; % set initial value
26
27 % Compute solution
28 f o r j=1:N29 y(:,j+1) = RK4Step(f,y(:,j),t(j),h);30 end31
32 re turn33
34 f u n c t i o n y1 = RK4Step(f,y0,t0,h)35
36 k = nan( s i z e(y0,1),4);37
38 k(:,1) = f(t0, y0);39 k(:,2) = f(t0 + h/2, y0 + h/2*k(:,1));40 k(:,3) = f(t0 + h/2, y0 + h/2*k(:,2));41 k(:,4) = f(t0 + h, y0 + h*k(:,3));42
Problem Sheet 6 Page 8 Problem 6.1
43 y1 = y0 + h/6*(k(:,1) + 2*k(:,2) + 2*k(:,3) + k(:,4));44
45 re turn
(6.1f) Now run the script Compare.m. You will get two graphs in phase space.
The first graph will be on time interval [0, 8] with step size h = 0.25, showing the simulationresult of Explicit Euler Method(blue circle), Implicit Euler Method(red triangle) and ImplicitMidpoint Method(green cross). The second graph will be on time interval [0, 500] with step sizeh = 1, showing the simulation result of Implicit Midpoint Method(green cross) and classical4-order Runge-Kutta Method(pink square).
Describe the observed behavior of the solutions from these four different algorithms, and try toexplain them.
Solution:
Figure 6.1: Explicit, Implicit Euler and Implicit Midpoint
Observed behavior.We can see in Figure 6.1 and Figure 6.2, that the Euler and Runge-Kutta 4 solutions tend towards(0, 0) for big k. A longer time interval is necessary to observe the behavior for Runge-Kutta4. The implicit Euler and RK4 solutions have a so-called “numerical friction”, although thereis no friction in the model and the total energy is (supposed to be) conserved. The solution ofthe explicit Euler method undergoes blow–up. Only the solution of the implicit mid-point ruleinherits the stability of the exact solution.
The mathematical Pendulum equation is given by{q = p,
p = − sin q.(6.1.4)
We can see that (6.1.4) is a Hamilton system for the Hamilton function
H(p, q) =1
2p2 − cos q.
Problem Sheet 6 Page 9 Problem 6.1
Figure 6.2: Implicit Midpoint and RK4
Indeed,
−∂H∂q
= − sin q = p,∂H
∂p= p = q.
Also, the energy H(p, q) is a conserved quantity of (6.1.4).
d
dtH(p, q) =
2
2pp+ (sin q)q = p(− sin q) + (sin q)p = 0.
Although the energy should be conserved, we can plot H(p, q) as a function of time and see theenergy increase for explicit Euler, decrease for implicit Euler and RK4, etc.
Listing 6.9: Implementation of s8a3Script.m1 % Explicit & implicit Euler, Mid-Point Rule and RK4 for the
mathematical
2 % pendulum.
3
4 % C.J. Gittelson, modified by D.D. Wright / SAM ETHZ / 2008
5
6 % Define right hand side
7 f = @(t,y) [y(2);- s i n(y(1))];8
9 % Define problem and discretization parameters
10 y0 = [pi/2;0];11 T = [0 8];12 h = 0.25;
Problem Sheet 6 Page 10 Problem 6.1
13 tol = 1e-6;14
15 % Serie 8, Aufgabe 3, Part a)
16 [t,yExp] = EulerSolve(f,y0,T,h);17 [t,yImp] = ImpEulerSolve(f,y0,T,h,tol);18 %[t,yImpMidPoint] = ImpMidPointSolve(f,y0,T,h,tol);
19 % or implicit mid-point using explicit and implicit
half-steps:
20 [t,yImpMidPoint] = ImpMidPointSolveWithEuler(f,y0,T,h,tol);21
22 % Plot solution in phase space
23 f i g u r e;24 p l o t(mod(yExp(1,:)+pi ,2*pi)-pi ,yExp(2,:),’bo-’);25 hold on;26 p l o t(mod(yImp(1,:)+pi ,2*pi)-pi ,yImp(2,:),’rˆ-’);27 p l o t(mod(yImpMidPoint(1,:)+pi ,2*pi)-pi ,yImpMidPoint(2,:),...28 ’gx-’,’MarkerSize’,16);29 hold off;30 t i t l e ([’\bf Numerical Methods for the Mathematical Pendulum,
T=’ num2str(T(2)) ...31 ’ h=’ num2str(h)]);32 l egend(’explicit’,’implicit’,’implicit midpoint’);33 x l a b e l(’\alpha’);34 y l a b e l(’p’);35 s e t(gca,’XLim’,[-pi p i]);36
37 % Serie 8, Aufgabe 3, Part c)
38 f i g u r e;39 T = [0 500];40 h = 1;41 [t,yImpMidPoint] = ImpMidPointSolve(f,y0,T,h,tol);42 % or implicit mid-point using explicit and implicit
half-steps:
43 % [t,yImpMidPoint] = ImpMidPointSolveWithEuler(f,y0,T,h,tol);
44 [t,yRK4] = RK4Solve(f,y0,T,h);45 p l o t(mod(yImpMidPoint(1,:)+pi ,2*pi)-pi ,yImpMidPoint(2,:),...46 ’gx-’,’MarkerSize’,16);47 hold on;48 p l o t(mod(yRK4(1,:)+pi ,2*pi)-pi ,yRK4(2,:),’ms-’);49 t i t l e ([’\bf Numerical Methods for the Mathematical Pendulum,
T=’ num2str(T(2)) ...50 ’ h=’ num2str(h)]);51 l egend(’implicit midpoint’,’RK4’);52 x l a b e l(’\alpha’);53 y l a b e l(’p’);54 a x i s square;
Problem Sheet 6 Page 11 Problem 6.1
Published on 27 March 2019.To be submitted by 4 April 2019.Last modified on April 4, 2019
Problem Sheet 6 Page 12 Problem 6.1
Top Related