1 Resit Assignment
resit
August 16, 2019
This resit assessment is marked out of 100 and comprises 100% of the resit course mark. You are not eligible for this resit if you have already passed the course.
1.0.1 Academic misconduct
The assessment is primarily summative in nature. You are expected to be aware of and abide by University policies on academic misconduct.
• School of Mathematics academic misconduct advice and policies • Academic Services academic misconduct information
This is an individual assignment – do not copy the work of another student.
If you use any resources (e.g. textbooks or websites) then include appropriate references in your solutions. Course materials do not need to be referenced.
1.0.2 Markdown
In workshops you have edited Jupyter Notebook “code” cells. Cells can also contain formatted text in “markdown” cells. Some questions ask for further discussion and explanation, which should be provided in appropriate markdown cells. You may wish to use formatting features of markdown cells, although this is optional.
• Jupyter Notebook: Markdown cells
You can edit markdown cells by double clicking on them, and render the markdown by selecting the cell
and pressing Shift+Return.
1.0.3 Code commentary
Your code should be extensively commented, with the functionality of each line of code explained with a comment. This is to test your understanding of the code you have written. Up to half of the marks associated with the coding part of a question may be deducted for a missing, incomplete, or inaccurate code commentary.
The following provides an example of the expected level of commenting.
In [ ]: def is_prime(n): “””
Return whether an input positive integer is prime.
“””
if n == 1: # If n is 1 …
return False # … then n is not prime
1
for i in range(2, n): # Test integers i from 2 to n – 1 inclusive if n % i == 0: # If n is divisible by i …
return False # … then n is not prime
# If n is not divisible by any integers from 2 to n – 1 inclusive then n is # prime
return True
1.0.4 Output and figures
Your code must generate and display all relevant output when run, and all figure formatting must be performed programmatically and not via the interactive plotting interface.
Rerun your code cells after editing your code, to make sure that the output is updated.
1.0.5 Saving your work
When you edit the notebook, before closing the window/tab make sure to select “File”->“Save and Check- point”. If using Noteable make sure you also download and keep a copy of the edited file, using “File”- >“Download as”->“Notebook”.
On lab computers make sure you save edited notebook files in an appropriate location, as otherwise they may be lost when you logout.
• Information Services: Saving your files 2 Question 1: Functions
1.1 Write a function named square factor which accepts as input a positive integer, and returns True if the integer is divisible by the square of any integer greater than one, and False otherwise.
For example:
square_factor(256)
should return True, while
square_factor(6) should return False.
[6 marks]
In [ ]: # Code for question 1.1
3 Question 2: Linear algebra
2.1 Consider the matrix
and vector
1 0.1 0.1
A=0.1 2 0.1, (1)
0.1 0.1 3
10.5
b= 9.1 . (2)
4.4
Compute and display the vector x which solves the linear system
Ax = b. (3) 2
[4 marks]
2.2 Given a length 3 vector y0 = (1, 0, 1)T , compute and display y5 in the sequence
where
yn =D−1(b−(A−D)yn−1) forn=1,2,… (4)
100
D=0 2 0, (5)
003
and where A and b are as given in question 2.1. Investigate what is obtained as n → ∞, and briefly comment on the result.
[7 marks]
In [ ]: # Code for questions 2.1 and 2.2
3.0.1 Discussion for question 2.2
4 Question 3: Interpolation and curve fitting
Consider the following function
F (x) = |x|, (6) ontheintervalx∈[−1,+1]. DefineN+1interpolationpointsxn forn∈{0,…,N},with
xn =−1+2n forn∈{0,…N}, (7) N
and thus define fn for n ∈ {0,…N} where
fn = F (xn) for n ∈ {0,…N}. (8)
3.1 Plot the function F (x) on the interval x ∈ [−1,+1], and the coordinates (xn,fn) for N = 5. You should choose how to present the plot(s) so that the data presentation is clear and easy to understand. Your plot(s) should be appropriately formatted.
[4 marks]
3.2 In a new figure window, plot the interpolating polynomial fitting through the (xn, fn) data for N = 5, a degree 2 least squares fitting polynomial fitting these data (as defined in lectures and workshops), and a cubic spline fitting through these data. You should choose how to present the plot(s) so that the data presentation is clear and easy to understand. Your plot(s) should be appropriately formatted.
[8 marks]
In [ ]: # Code for questions 3.1 and 3.2
3.3 Consider the fits described in question 3.2, but for different values of N. Investigate what happens as N is increased. Your code should generate appropriate output and/or figures to illustrate the results. Discuss the results, and the quality of the different fits through the data.
[10 marks]
In [ ]: # Code for questions 3.3
3
4.0.1 Discussion for question 3.3
5 Question 4: Floating point
4.1 Find three floating point values a, b, and c such that, in floating point arithmetic, both of the following hold
a + (b + c) ̸= (a + b) + c, (9) a + (b + c) ̸= (a + c) + b. (10)
Your code should display appropriate output to illustrate the results.
[3 marks]
In [ ]: # Code for question 4.1 4.2 Consider the function
F(x)= ex −1−x. (11) x2
Write a Python function which, given a floating point value of x, uses the NumPy exp function and basic floating point arithmetic (specifically the +, -, *, /, or ** operators) to evaluate and return the value of F (x). Investigate what happens for values of x with small magnitude. Your code should generate appropriate output and/or figures to illustrate the results. Discuss and explain the results.
[7 marks]
In [ ]: # Code for question 4.2 5.0.1 Discussion for question 4.2
4.3 Write a new Python function which uses the Taylor expansion for ex
ex=1+∞ 1xn, (12)
n!
n=1
substituted into the expression for F (x) and simplified, to yield a more accurate calculation of F (x) for values of x with small magnitude. Your code should generate appropriate output and/or figures to illustrate the behaviour of your new function, and to compare its behaviour against the function from question 4.2.
[7 marks]
In [ ]: # Code for question 4.3
6 Question 5: Numerical calculus
5.1 Consider the following numerical approximation for a first derivative ′135
F (x)≈D(x)=∆x −2F(x−∆x)+3F(x)−2F(x+∆x)+F(x+2∆x) . (13)
By considering the derivative of the function F (x) = xex at x = 1, investigate the accuracy of this numerical approximation. Discuss your results. Your code should generate appropriate output and/or figures to illustrate the results and to support your discussion.
[9 marks]
In [ ]: # Code for question 5.1
4
6.0.1 Discussion for question 5.1
7 Question 6: Fixed point iteration
6.1 Consider the function
F (x) = ln x − x + 4. (14) Roots of x are sought via fixed point iteration
with iteration function
xn = Φ(xn−1) for x = 1,2,…, (15)
Φ (x) = α (ln x − x + 4) + x, (16)
where α is a real parameter to be specified.
Consider an initial guess x0 = 5. Use this fixed-point iteration to seek a root of F (x), considering different values of α ∈ [1,…,5].
Discuss and explain the convergence properties of this fixed-point iteration. Your code should generate appropriate output and/or figures to illustrate the results and to support your discussion.
[12 marks]
In [ ]: # Code for question 6.1 7.0.1 Discussion for question 6.1
6.2 Use scipy.optimize.fsolve, and Newton’s method, to find a root of F (x). [7 marks]
In [ ]: # Code for question 6.2
8 Question 7: Root finding
7.1 The provided code defines a more complicated function G (x).
Apply two different root finding algorithms to find a root of this function on the interval x ∈ [1,5].
Explain and justify your choice of root finding algorithms, and describe their key properties. Your code should generate appropriate output and/or figures to illustrate the results and to support your discussion.
[16 marks]
In [ ]: import numpy as np
def G(x):
s = np.array([1.0, 0.0], dtype=float)
tau = np.array([0.0, 0.0], dtype=float)
A = np.array([[1.0, -0.01], [x * x * 0.01, 1.0]], dtype=float)
for i in range(10):
s = np.linalg.solve(A, s)
v = np.array([0.0, -2 * x * 0.01 * s[0]], dtype=float)
tau = np.linalg.solve(A, tau – v)
return tau[0] * (s[0] – 9.9121651714487924e-01)
# Code for question 7.1
8.0.1 Discussion for question 7.1
5