MATH2019 Introduction to Scientific Computation
— Coursework 1 (10%) —
Submission deadline: 3pm, Tuesday, 2 Nov 2021
Note: This is currently version 2.2 of the PDF document. (19th October, 2021)
Further questions will be added to this PDF in the upcoming weeks.
This coursework contributes 10% towards the overall grade for the module.
Rules:
• Each student is to submit their own coursework.
• You are allowed to work together and discuss in small groups (2 to 3 people), but you must write your
own coursework and program all code by yourself.
• Please be informed of the UoN Academic Misconduct Policy (incl. plagiarism, false authorship, and
collusion).
Coursework Aim and Coding Environment:
• In this coursework you will develop Python code related to algorithms that solve nonlinear equations,
and you will study some of the algorithms’ behaviour.
• As discussed in the lecture, you should write (and submit) plain Python code (.py), and you are strongly
encouraged to use the Spyder IDE (integrated development environment). Hence you should not write
IPython Notebooks (.ipynb), and you should not use Jupyter).
How and Where to run Spyder:
• Spyder comes as part of the Anaconda package (recall that Jupyter is also part of Anaconda). Here
are three options on how to run Spyder:
(1) You can choose to install Anaconda on you personal device (if not done already).
(2) You can open Anaconda on any University of Nottingham computer.
(3) You can open a UoN Virtual Desktop on your own personal device, which is virtually the same as
logging onto a University of Nottingham computer, but through the virtual desktop. The simply open
Anaconda. Here is further info on the UoN Virtual Desktop.
• The A18 Computer Room in the Mathematical Sciences building has a number of computers available,
as well as desks with dual monitors that you can plug into your own laptop.
Online 1-on-1 Support and Time-tabled Support Sessions (Thu 3-4pm, Wed 9-10am):
• You can work on the coursework whenever you prefer.
• We especially encourage you to work on it during the time-tabled computing and drop-in sessions
(Thursdays 3-4pm and Wednesdays 9-10am). There is no need to come to the time-table allocated
locations, as you can obtain online 1-on-1 support via MS Teams from PGR Student Teaching Assistants.
• To obtain online 1-on-1 support during these sessions, please join the MATH2019 (21-22) MS Team
by using the code fnen7oo.
• For those of you that wish to go to the time-table allocated locations, please be aware that there
is limited space: Thursdays (3-4pm) is in Pharmacy A06 (small seminar room with capacity of 33) and
Wednesdays (9-10am) is in Clive Granger (computer room with capacity of 68). You can find the lecturer
there during these sessions.
Piazza:
• You are allowed and certainly encouraged(!) to also ask questions using Piazza to obtain clarification
of the coursework questions, as well as general Python queries.
• However, when using Piazza, please ensure that you are not revealing any answers to others. Also,
School of Mathematical Sciences — Version: 19th October, 2021 — Lecturer: Dr Kris van der Zee Page 1 of 5
please ensure that you are not directly revealing any code that you wrote. Doing so is considered
Academic Misconduct.
• When in doubt, please simply arrange a 1-on-1 meeting with the PGR Teaching Assistants or Lecturers
during the Online 1-on-1 Support Sessions (Thursdays 3-4pm and Wednesdays 9-10am).
Some Further Advice:
• You are expected to have basic familiarity with Python, in particular: logic and loops, functions, NumPy
and matplotlib.pyplot. Note that it will always be assumed that the package numpy is imported as np,
and matplotlib.pyplot as plt.
• Helpful resources: Python 3 Online Documentation – Spyder IDE (integrated development editor) –
NumPy User Guide – Matplotlib Usage Guide – Moodle Page Core Programming 2020-2021 (Lectured
by Fredrik Stromberg),
• Write your code as neatly and read-able as possible so that it is easy to follow. Add some comments
to your code that indicate what a piece of code does. Frequently run your code to check for (and
immediately resolve) mistakes and bugs.
• Coursework Questions with a “?” are more tedious, but can be safely skipped, as they don’t affect
follow-up questions.
Submission Procedure:
• Submission will open after 21 October 2021.
• To submit, simply upload the requested .py-files on Moodle. (Your submission will be checked for
plagiarism using turnitin.)
• Your work will be marked (mostly) automatically: This is done by running your functions and comparing
their output against the true results.
Getting Started:
• Download the contents of the “Coursework 1 Pack” folder from Moodle into a single folder.
(More files may be added in later weeks.)
• Open Spyder (see instructions above), and if you wish you can watch a basic intro video from the
Spyder website (click “Watch video”).
• You can also watch the recording of Lecture 1, second hour, where a brief demonstration was given.
I Bisection method
Let f(x) = x
3 + x
2 − 2x − 2. Note that this is the same function as in Lecture 1.
Consider the bisection method for finding the root of f in the interval [1, 2].
1 Open the py-file just trying out.py from the Coursework 1 Pack (from Moodle),
and add your code to it:
• First simply plot the function to get an idea of what it looks like.
Hint: Use matplotlib.pyplot; see, e.g., the Matplotlib Usage Guide.
• Then try implementing the bisection algorithm as explained in Lecture 1.
Hint: The Lecture 1 SlidesAndNotes PDF contains a pseudo-code algorithm
for bisection, as well as a simple Python code. This was also demonstrated in
Lecture 1.
• Verify that the first few approximations are correct. What value do your approximations
seem to converge to?
2 Next, you will repeat what you have done above, but now by writing a module
Coursework 1 Page 2 of 5
(a .py file) with a function for the entire algorithm. To get started, open the file
rootfinders.py. Note that this file contains already an unfinished function with
the following signature:
def bisection (f ,a ,b , Nmax )
This function returns a numpy.ndarray p vec p array, ← Sentence
corrected on
19 Oct 11am
shape (Nmax, ), which
is a 1-D array of the approximations pn (n = 1, 2, . . .) computed by the bisection
method. The input f can be a lambda function or function defined using def, a
and b define the initial interval [a, b] of interest, and Nmax is the maximum number
of iterations.
• Complete this function so that it implements the bisection algorithm, and provides
the output as required above.
• Test your function by running the main.py file, which contains the following:
import numpy as np
import matplotlib . pyplot as plt
import rootfinders as rf
%% Question 2
Initialise
f = lambda x : x 3 + x 2 - 2* x - 2
a = 1
b = 2
Nmax = 5
Run bisection
p_array = rf . bisection (f ,a ,b , Nmax )
Print output
print ( p_array )
I (The below has been added on 14 Oct 2021.)
Marks can be obtained for your bisection function definition for correctly gener- [10 / 40]
ating the required output, for certain set(s) of inputs for {f,a,b,Nmax}. The correctness
of the following will be checked:
• The type of output p array
• The np.shape (number of columns and rows) of output p array
• The values of output p array
To test your code, download the file test student code.py into the same folder
as rootfinders.py. Then run test student code, which generates in your folder
the file StudentCodeTestOutput.html. Open that file with a browser to see exactly
what tests were performed on your code.
Note that in marking your work, different input(s) may be used.)
Coursework 1 Page 3 of 5
I Fixed-point iteration method
To solve the root-finding problem f(x) = 0, but using fixed-point iteration, we define
g(x) = x − c f(x)
and consider the corresponding fixed-point problem g(p) = p. (Note that if g(p) = p,
then indeed f(p) = 0.)
3 • Add in your file rootfinders.py the following function definition:
def fixedpoint_iteration (f ,c , p0 , Nmax )
This function returns a numpy.ndarray p vec p array, ← Sentence
corrected on
19 Oct 11am
shape (Nmax, ), which is
a 1-D array of the approximations pn (n = 1, 2, . . .) computed by the fixed-point
iteration method. The input f can be a lambda function or function defined using
def, ← Sentence
corrected on
14 Oct, 6pm
a and b define the initial interval [a, b] of interest, c is a real number used
in the above definition of g(x), p0 is a real number representing the initial approximation
to start the fixed-point iteration, and Nmax is the maximum number of
iterations.
• Complete this function so that it implements the fixed-point iteration algorithm,
and provides the output as required above.
• Also add a brief description at the top of your definition, the so-called
doc string. This description should become visible, whenever one types:
help(rf.fixedpoint iteration) or rf.fixedpoint iteration?.
Hint: See the docstring of the bisection function, which was already given.
← Sentence
added on 14
Oct, 6pm
See also the useful online pandas guide to writing a docstring.
• Test your function by running the main.py file, which has been updated to
additionally contain the following:
%% Question 3
Initialise
f = lambda x : x 3 + x 2 - 2* x - 2
c = 1/12
p0 = 1
Nmax = 10
Run fixedpoint_iteration
p_array = rf . fixedpoint_iteration (f ,c , p0 , Nmax )
Print output
print ( p_array )
I
Marks can be obtained for your fixedpoint iteration definition for generating the [10 / 40]
required output, for certain set(s) of inputs for {f,c,p0,Nmax}. The correctness of
the following will be checked:
Coursework 1 Page 4 of 5
• The type of output p array
• The np.shape (number of columns and rows) of output p array
• The values of output p array
• As well as for the output of ”help(fixedpoint iteration)”.
To test your code, download the file test student code.py into the same folder
as rootfinders.py. Then run test student code, which generates in your folder
the file StudentCodeTestOutput.html. Open that file with a browser to see exactly
what tests were performed on your code.
Note that in marking your work, different input(s) may be used.)
More questions will be added next week.