Coursework_1
Coursework_1
— Coursework 1 —
Submission deadline: 15:00 Thursday 14 November 2024
This coursework contributes 10% towards your mark for this module.
Rules
It is not permitted to use generative artificial intelligence (AI) software for this coursework. Ensure that
you have read and have understood the Policy on academic misconduct. One of the things stated in
this policy is that “The submission of work that is generated and/or improved by software that is not
permitted for that assessment, for the purpose of gaining marks will be regarded as false authorship
and seen as an attempt to gain an unpermitted academic advantage”.
This coursework should be your own individual work, with the exceptions that:
1. You may ask for and receive help from the lecturer Richard Rankin although not all questions will be
answered and those that are will be answered to all students that attend the class.
2. You may copy from material provided on the Moodle pages:
• Introduction to Scientific Computation (MATH2033 UNNC) (FCH1 24-25)
• Analytical and Computational Foundations (MATH1028 UNNC) (FCH1 23-24)
• Calculus (MATH1027 UNNC) (FCH1 23-24)
• Linear Mathematics (MATH1030 UNNC) (FCH1 23-24)
Coding Environment
You should write and submit a py file. You are strongly encouraged to use the Spyder IDE (integrated
development environment). You should not write or submit an ipynb file and so you should not use
Jupyter Notebook.
It will be assumed that numpy is imported as np, and that matplotlib.pyplot is imported as plt.
Submission Procedure:
To submit, upload your nonlinear equations.py file through the Coursework 1 assignment activity in the
Coursework 1 section of the Moodle page Introduction to Scientific Computation (MATH2033 UNNC)
(FCH1 24-25).
Marking
Your nonlinear equations.py file will be mainly marked by running your functions with certain inputs and
comparing the output with the correct output. In addition, the requested docstrings will be marked.
def bisection_method (f ,a ,b ,t , n ) :
Assume that:
• The type of the input f is function.
• The type of the input a is numpy.float64, float or int.
• The type of the input b is numpy.float64, float or int.
• The type of the input t is numpy.float64, float or int.
• The type of the input n is int.
• The inputs a and b are real numbers that are such that a < b.
• The input f is such that f is real-valued and continuous on [a, b] and f(a)f(b) < 0.
• The input t is a positive real number.
• The input n is a positive integer.
Complete the function bisection method so that it returns a tuple (p, r) where:
• p is a numpy.ndarray with shape (m, ) which is such that p[k] = pk+1 for k = 0, 1, . . . , m − 1
where m is the smallest positive integer less than n for which f(pm ) = 0 or the error bound for the
bisection method implies that the absolute error in pm is less than or equal to t if such an integer m
exists and m = n otherwise.
• r is a bool which is such that r = True if f(pm ) = 0 or the error bound for the bisection method
implies that the absolute error in pm is less than or equal to t and r = False otherwise.
A test that you can perform on your bisection method function is to run the Question 1 cell of the
tests.py file and check that what is printed is:
[1.5 1.25 1.375]
False
[30 marks]
Coursework 1 Page 2 of 4
2. Let pi be the approximation to a zero of f obtained after performing i iterations of the method of false
position starting with the initial approximations a and b.
The nonlinear equations.py file contains an unfinished function with the following first line:
def false_position (f ,a ,b ,t , n ) :
Assume that:
• The type of the input f is function.
• The type of the input a is numpy.float64, float or int.
• The type of the input b is numpy.float64, float or int.
• The type of the input t is numpy.float64, float or int.
• The type of the input n is int.
• The inputs a and b are real numbers.
• The input f is such that f is real-valued and continuous on [min{a, b}, max{a, b}] and f(a)f(b) <
0.
• The input t is a positive real number.
• The input n is a positive integer.
Complete the function false position so that it returns a tuple (p, r) where:
• p is a numpy.ndarray with shape (m + 2, ) which is such that p[0] = a, p[1] = b and p[k] = pk−1
for k = 2, 3, . . . , m + 1 where m is the smallest positive integer less than n for which the absolute
value of f evaluated at pm is less than or equal to t if such an integer m exists and m = n otherwise.
• r is a bool which is such that r = True if the absolute value of f evaluated at pm is less than or
equal to t and r = False otherwise.
The docstring of the function false position in the nonlinear equations.py file is the docstring
of the function bisection method. Modify the docstring of the function false position appro-
priately and only where necessary so that it is correct for the function false position described
above.
A test that you can perform on your false position function is to run the Question 2 cell of the
tests.py file and check that what is printed is:
[1. 2. 1.33333333 1.4 ]
True
[35 marks]
Coursework 1 Page 3 of 4
3. The nonlinear equations.py file contains an unfinished function with the following first line:
def error_plot (f ,a ,b ,z , n ) :
Assume that:
• The type of the input f is function.
• The type of the input a is numpy.float64, float or int.
• The type of the input b is numpy.float64, float or int.
• The type of the input z is numpy.float64, float or int.
• The type of the input n is int.
• The inputs a and b are real numbers that are such that a < b.
• The input f is such that f is real-valued and continuous on [a, b] and f(a)f(b) < 0.
• The input z is a real number.
• The input n is a positive integer.
• Calling the function bisection method from question 1 with t = 1e − 20 will result in the
numpy.ndarray p that is the element with index 0 in the returned tuple having shape (n, ).
• Calling the function false position from question 2 with t = 1e − 20 will result in the
numpy.ndarray p that is the element with index 0 in the returned tuple having shape (n, ).
Complete the function error plot so that it returns a matplotlib.figure.Figure, with an ap-
propriate legend and a single pair of appropriately labelled axes, on which there is a semilogy plot
of:
• the absolute error |z − pi | for i = 1, 2, . . . , n for which |z − pi | is positive where pi is the approx-
imation to the zero z of f obtained after performing i iterations of the bisection method starting
from the initial interval [a, b].
• the absolute error |z − pi | for i = 1, 2, . . . , n for which |z − pi | is positive where pi is the ap-
proximation to the zero z of f obtained after performing i iterations of the method of false position
starting with the initial approximations a and b.
A test that you can perform on your error plot function is to run the Question 3 cell of the tests.py
file. Complete the two incomplete sentences in the docstring so that for each of the two methods the
sentences describe the behaviour of the absolute error seen in the figure produced by this test.
[35 marks]
Coursework 1 Page 4 of 4