程序代写代做代考 algorithm python Resit Assignment¶

Resit Assignment¶
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.
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.
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.
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

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

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.
Saving your work¶
When you edit the notebook, before closing the window/tab make sure to select “File”->”Save and Checkpoint”. 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
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

Question 2: Linear algebra¶
2.1 Consider the matrix
\begin{equation} A = \left( \begin{array}{ccc} 1 & 0.1 & 0.1 \\ 0.1 & 2 & 0.1 \\ 0.1 & 0.1 & 3 \end{array} \right), \end{equation}
and vector
\begin{equation} b = \left( \begin{array}{c} 10.5 \\ 9.1 \\ 4.4 \end{array} \right). \end{equation}
Compute and display the vector $x$ which solves the linear system
\begin{equation} A x = b. \end{equation}
[4 marks]
2.2 Given a length $3$ vector $y_0 = \left( 1, 0, 1 \right)^T$, compute and display $y_{5}$ in the sequence
\begin{equation} y_n = D^{-1} \left( b – \left( A – D \right) y_{n – 1} \right) \textrm{ for } n = 1, 2, \ldots \end{equation}
where
\begin{equation} D = \left( \begin{array}{ccc} 1 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 3 \end{array} \right), \end{equation}
and where $A$ and $b$ are as given in question 2.1. Investigate what is obtained as $n \rightarrow \infty$, and briefly comment on the result.
[7 marks]
In [ ]:
# Code for questions 2.1 and 2.2

Discussion for question 2.2¶

Question 3: Interpolation and curve fitting¶
Consider the following function
\begin{equation} F \left( x \right) = \left| x \right|, \end{equation}
on the interval $x \in \left[ -1, +1 \right]$. Define $N + 1$ interpolation points $x_n$ for $n \in \left\{ 0, \ldots, N \right\}$, with
\begin{equation} x_n = -1 + 2 \frac{n}{N} \quad \textrm{ for } n \in \left\{ 0, \ldots N \right\}, \end{equation}
and thus define $f_n$ for $n \in \left\{ 0, \ldots N \right\}$ where
\begin{equation} f_n = F \left( x_n \right) \quad \textrm{ for } n \in \left\{ 0, \ldots N \right\}. \end{equation}
3.1 Plot the function $F \left( x \right)$ on the interval $x \in \left[ -1, +1 \right]$, and the coordinates $\left( x_n, f_n \right)$ 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 $\left( x_n, f_n \right)$ 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

Discussion for question 3.3¶

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
\begin{align} a + \left( b + c \right) & \ne \left( a + b \right) + c, \\ a + \left( b + c \right) & \ne \left( a + c \right) + b. \end{align}
Your code should display appropriate output to illustrate the results.
[3 marks]
In [ ]:
# Code for question 4.1

4.2 Consider the function
\begin{equation} F \left( x \right) = \frac{e^x – 1 – x}{x^2}. \end{equation}
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 \left( x \right)$. 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

Discussion for question 4.2¶

4.3 Write a new Python function which uses the Taylor expansion for $e^x$
\begin{equation} e^x = 1 + \sum_{n = 1}^\infty \frac{1}{n!} x^n, \end{equation}
substituted into the expression for $F \left( x \right)$ and simplified, to yield a more accurate calculation of $F \left( x \right)$ 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

Question 5: Numerical calculus¶
5.1 Consider the following numerical approximation for a first derivative
\begin{equation} F’ \left( x \right) \approx D \left( x \right) = \frac{1}{\Delta x} \left[ -\frac{3}{2} F \left( x – \Delta x \right) + 3 F \left( x \right) – \frac{5}{2} F \left( x + \Delta x \right) + F \left( x + 2 \Delta x \right) \right]. \end{equation}
By considering the derivative of the function $F \left( x \right) = x e^x$ 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

Discussion for question 5.1¶

Question 6: Fixed point iteration¶
6.1 Consider the function
\begin{equation} F \left( x \right) = \ln x – x + 4. \end{equation}
Roots of $x$ are sought via fixed point iteration
\begin{equation} x_n = \Phi \left( x_{n – 1} \right) \textrm{ for } x = 1, 2, \ldots, \end{equation}
with iteration function
\begin{equation} \Phi \left( x \right) = \alpha \left( \ln x – x + 4 \right) + x, \end{equation}
where $\alpha$ is a real parameter to be specified.
Consider an initial guess $x_0 = 5$. Use this fixed-point iteration to seek a root of $F \left( x \right)$, considering different values of $\alpha \in \left[ 1, \ldots, 5 \right]$.
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

Discussion for question 6.1¶

6.2 Use scipy.optimize.fsolve, and Newton’s method, to find a root of $F \left( x \right)$.
[7 marks]
In [ ]:
# Code for question 6.2

Question 7: Root finding¶
7.1 The provided code defines a more complicated function $G \left( x \right)$.
Apply two different root finding algorithms to find a root of this function on the interval $x \in \left[ 1, 5 \right]$. 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

Discussion for question 7.1¶