Assignment 4
Assignment 4
Assignment 4
Submission Details
This assignment is due to be handed in before
Student ID
Student ID of partner
Please fill in your ID and that of your working partner above. You need to submit your work
even when working with a partner.
Please read carefully all instructions on this page to make sure you understand all rules and restric-
tions. Every submission consists of two parts: A theoretical part, which can be handwritten or computer-
written, and a numerical part, which is to be submitted as jupyter notebook .ipynb-file. Further rules
and restrictions:
All handwritten parts must be legible to receive full marks.
Make sure to provide explanations and show your work. The correct answer alone and without
explanation will not receive full marks.
Numerical work should come in an .ipynb-file with comments, explanations, code, and figures.
The submission file needs to be able to run through from top to bottom to reproduce all the results
of your submission.
Python is the only computer language to be used in this course.
You are allowed to use pre-defined mathematical functions (such as exp, linspace, max, ...), but
you are NOT allowed to use high-level numerical functions (such as interpolate, polyfit, ...).
All work must be explained, commented on, and all work must be shown. Implementation should
contain comments and explanations in markdown surrounding them. Figures should have appro-
prate axis scaling, labels, legends, etc.
The assignments can be worked on and submitted in pairs. If you choose to do so, indicate your
own and your partner’s student ID in the fields above. Each partner must upload all submission
files to moodle. In this case,both partners receive the same mark.
Late panelties apply automatically to late submissions. Even if you submit together with a part-
ner, and your partner submits in time, you will be penalised for a late submission if your own
submission is not in time.
To reiterate the above: You will only earn marks if you submit work, and do so before the deadline.
The use of generative AI in this assignment is strictly forbidden.
Assignment 4 MA2K4 2023/24– Numerical Methods and Computing
Theoretical Part
This part is to be solved by hand and manual calculation, without the help of a computer. Show
all your work to receive full marks.
4.2 Derive the region of absolute stability for the Heun method
for θ ∈ [0, 1]. Show that the θ-method is A-stable if and only if θ ≥ 21 .
4.4 In this problem, we will discuss different Runge-Kutta methods in light of the order-
and consistency conditions derived in the lecture.
a) Show that the Runge-Kutta method with Butcher tableau
0
1/2 1/2
3/4 3/4
2/9 1/3 4/9
2
Assignment 4 MA2K4 2023/24– Numerical Methods and Computing
4.5 For a general two-stage implicit Runge-Kutta method, applied to the test problem y ′ (t) =
λy(t), show that
where ∆ is the determinant of the matrix I − λhA, where I is the identity on R2×2 and
A is taken from the Butcher tableau of the method.
4.6 Consider the 2-stage implicit Runge-Kutta method given by the Butcher tableau
√ √
(3 − √3)/6 1/4
√ (3 − 2 3)/12
(3 + 3)/6 (3 + 2 3)/12 1/4 .
1/2 1/2
Deduce that, when applied to the test problem, we have un+1 = R(λh)un , where
1 + 12 λh + 1 2 2
12 λ h
R(λh) =
1 − 12 λh + 1 2 2
12 λ h
3
Assignment 4 MA2K4 2023/24– Numerical Methods and Computing
Numerical Part
This part is to be solved in python in a single jupyter notebook .ipynb-file. Make sure that all
your explanations, code, figures, results, comments, and discussions are included in this single
file to receive all marks. Re-use as much code as possible and avoid any code duplication.
4.7 In this problem we will plot the region of absolute stability of various methods.
Hint: The contour or contourf functions of matplotlib allow plotting isolines (lines
only or filled), which can be helpful in visualising regions in the complex plane.
a) Plot the region of absolute stability of the Heun method as derived in problem 4.2.
Comment on whether the Heun method is A-stable.
b) Plot region of absolute stability for the Runge-Kutta method defined in 4.6. Comment
on whether this method is A-stable.
4.8 In this problem, we will implement a function to compute the EOC given a vector of
step sizes hk and a vector of errors ek . We will then use it to experimentally verify the
consistency order of various numerical methods for solving ODEs.
a) Write a function lastError(Phi, h, N) that returns ∣uN − yN ∣, i.e. the difference be-
tween the approximation un and the exact solution yn at the final step n = N , using
step size h, and the method given by the increment function Φ(t, y, f, h), for the initial
value problem
y ′ (t) = −(1 + t)y 2 , y(0) = 1 ,
which has the exact solution y(t) = 2/(t2 +2t+2). You can use your function evolve(t0,
y0, f, Phi, h, N) from the last quiz to generate an approximate solution un , n ∈
{0, . . . , N }.
b) Write a function EOC(hs, es) that takes a vector of timesteps hs and a vector of errors
es to compute the experimental order of convergence as a vector. Given our definition
in the lecture, we cannot compute the EOC for the first pair of timestep and error.
Your function should therefore make sure to put a sensible value in its place (such as
the floating point value NaN (“not a number”)).
c) For the stepsizes
T
hk = , k ∈ {0, . . . , 10} ,
N0 2k
(and therefore corresponding number of steps Nk = N0 2k ), with N0 = 4, T = 1.5,
provide a graphical evaluation of the errors, using your function from question a),
comparing against their theoretical scaling, as well as a table of the EOCs, using your
function from question b). Do so both for the forward Euler method and the Heun
method. You can use your implementation of the increment functions from the last
quiz. Comment on your results.
4
Assignment 4 MA2K4 2023/24– Numerical Methods and Computing
4.9 In this problem we will implement the increment function for a rather generic Runge-
Kutta methods, including explicit as well as diagonally implicit Runge-Kutta methods
(DIRKs).
a) Write a function explicitRK(t,u,f,h,A,b,c) that implements the increment func-
tion for a generic explicit Runge-Kutta methods with a Butcher tableau
c1 a11 . . . a1s
c A ⋮ ⋮ ⋱ ⋮
T ∶= c .
b s as1 . . . ass
b1 . . . bs
0 0 0 0
0 0 0
0 0 1/2 1/2 0 0
, 1/2 1/2 0 , and ,
1 1 −1 2 0
´¹¹ ¹ ¹ ¸¹ ¹ ¹ ¹¶ 0 1
´¹¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¸¹¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¶ 1/6 2/3 1/6
“Forward Euler”
“Explicit Midpoint” ´¹¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¸ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹¶
“RK3”
which are of first, second, and third order, respectively. Use your code and the param-
eters and IVP from the previous problem to plot the error at the last step, ∣uN −yN ∣ for
the same hk , Nk , to graphically verify the order of your implementation. Comment
on the observed orders in relation to the number of stages, and compare to what is
possible in theory.
Hint: The first of these methods is the forward Euler method, which you can use to
check whether your code produces exactly identical results as your previous imple-
mentation.
Hint: In order to conform to the increment function layout used earlier, you can define
wrapper functions like
1 def RK3 (t , u , f , h ) :
2 A = [[0 ,0 ,0] ,[1/2 ,0 ,0] ,[ -1 ,2 ,0]]
3 b = [1/6 ,2/3 ,1/6]
4 c = [0 ,1/2 ,1]
5 return explicitRK (t , u , f , h , A , b , c )
5
Assignment 4 MA2K4 2023/24– Numerical Methods and Computing
which we cannot solve in closed form and thus need to solve approximately. For this,
define (at each stage i ∈ {1, . . . , s}) a function F (x) given by
i−1
F (x) = x − f (t + hci , u + h ∑ aij Kj + haii x) .
s=1
Note that here we have split off the Ki term from the sum, which now only goes to
i − 1. Values of x for which F (x) = 0 correspond to the desired values Ki .
We can now employ a root finding algorithm to approximate the roots. For example,
after defining the function F(x) in python as above, we could use
1 from scipy . optimize import root_scalar
2
3 K = root_scalar (F , x0 = f (t , u ) , x1 =2* f (t , u ) ) . root
The function root scalar is a scalar root finder, which provided with an initial guess
x0 (taken to be f (t, u) here) and a second guess x1 (in this case 2f (t, u)), iteratively
tries to find a root of the function F (x). The function returns a RootResult object, of
which we only want the root itself, given as the property root.
Use your implementation of DIRK to do a graphical convergence study, excactly like in
the previous problem (with the same IVP, and using your previous implementations),
for the DIRK methods
√ √
1
2 + √6
3 1
+
2 √6
3
0√
1/2 1/2
1
and 1
2 − 6
3
− 33 1
2 + 63
1 1
´¹¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¸ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¶ 2 2
“Implicit Midpoint” ´¹¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¸¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¶
“Crouzeix”
to verify graphically that these methods have orders 2 and 3. Comment on the ob-
served order and the required number of stages in comparison to the explicit meth-
ods.