MAS212 Class Test 2 (2017)

Aim:

  • To reinforce what you have learnt in lectures and lab classes this semester.

How it works:

  • Complete the notebook below, as follows. Click on a question, then from the menu select Insert -> Insert Cell Below. From the dropdown box (next to the "stop" symbol) choose whether you wish to insert 'Code' or 'Markdown' (i.e. text with HTML and LaTeX capabilities). Save regularly!
  • Press Shift-Enter to execute the code in a cell.
  • Submit via http://somas-uploads.shef.ac.uk/mas212 by midnight on Sunday 10th Dec. If you are off-campus, you will need to use a VPN to submit.
  • This is an open-book test, which means you may consult books, notes and internet resources. Do not discuss or share your test with anyone. Copy-and-pasting is not permitted, except from the MAS212 code files specified in the questions. Please give answers in your own words.
  • This test is worth $5\%$ of the module mark. Please don't spend an excessive amount of time on it.
  • All submissions will be automatically checked for excessive similarities. Where there is sufficient circumstantial evidence of excessive similarities, a mark of zero can be awarded for this test.

Key Resources:

  • Lecture materials and links on course web page: http://sam-dolan.staff.shef.ac.uk/mas212/
In [2]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
%matplotlib inline


A. Definitions and Concepts

1. Convergence (Lecture 9).
Define the spectral radius $\rho(R)$, where $R$ is a square matrix.

2. Conditioning (Lecture 9).
(i) Define the row sum norm $||A||$ and the condition number $C$ of an $n\times n$ square matrix $A$.
(ii) Find the condition number of the matrix below. Is $Ax = b$ well-conditioned or ill-conditioned, in this case? $$ A = \begin{bmatrix} 1 & 1/2 & 1/3 \\ 1/2 & 1/3 & 1/4 \\ 1/3 & 1/4 & 1/5 \end{bmatrix} $$ (iii) In your own words, briefly describe the key lesson of Wilkinson's polynomial (Lec. 9).

3. Stability (Lectures 5 and 6). An MAS110 student decides to solve the differential equation below with the forward Euler method, and a step size of $h=0.1$. $$ \frac{dx}{dt} = -30 x + 1, \quad \quad x(0) = 2. $$

(a) What problem would the student encounter?
(b) Approximately, what is the maximum step size $h_{max}$ needed to avoid this problem with the forward Euler method?


B. Coding and implementation

4. Gauss-Jordan elimination.
Apply row operations (as in Lab Class 9) to find the inverse matrix $A^{-1}$ and the solution $x$ in the linear system $A x = b$ where $$ A = \begin{pmatrix} 1 & 2 & -3 \\ 0 & -1 & 3 \\ 2 & 1 & 2 \end{pmatrix}, \quad \quad b = \begin{pmatrix} -1 \\ 4 \\ 9 \end{pmatrix} $$ (You may use and adapt the code in gauss-jordan1.py only from Lab Class 9).

In [ ]:
 

5. Solving an ODE system numerically. (Lecture 6)
Use the backward Euler method to solve the ODE system in Question 3 with a step size $h=0.01$. Plot the solution $x(t)$ over the domain $t \in [0, 1]$.

In [ ]:
 

6. The logistic map. (Lec 3 / Lab Class 3).
The logistic map is $$ x_{n+1} = r x_n (1 - x_n) $$ where $r$ is a real parameter.
(i) Compute sequences $[x_0, x_1, \ldots x_{40}]$ with $x_0 = 1/2$, for three parameter choices:
$r = 2.5$, $r = 3.5$ and $r = 3.8$.
(ii) Plot the three sequences $(n,x_n)$ on the same graph. (Your plot should look something like this.) Briefly comment on what you see.

In [ ]:
 

7. Linear regression.
For the data set below, find the line of best fit $y = mx + c$, using linear regression.
(i) Calculate $m$ and $c$ using the formulae in Lecture 8 and print to screen to six decimal places.
(ii) Plot the data set with the line of best fit.

In [3]:
xs = np.array([ 0.38209254, -0.63095558, -0.27098983, -1.77894635,  0.89872936,
        1.72859985,  1.05801651,  1.8002796 , -0.48903099, -0.86228466,
        0.81095674])
ys = np.array([-0.48265341, -2.13554737, -0.60375244, -1.86415841,  1.27816274,
        2.15335151,  0.7339228 ,  1.87013722,  0.33664746, -0.47411395,
        0.81644587])

8. Animation. (Lecture 7).
Use matplotlib.animation.FuncAnimation() to create an animation with 200 frames, showing a standing wave on a string, described by: $$ y(x,t) = \sin(x) \sin(t) + 0.2 \sin(2 x) \cos(2 t) + 0.1 \sin(3 x) \sin(3 t) $$ Each frame of the animation should show $y$ vs $x$ on the domain $x \in [0, \pi]$. The frames should be linearly-spaced across $t \in [0, 4 \pi]$.
(You may adapt the code in sinwave.py, as in Lab Class 7, Q2).)

In [ ]:
%matplotlib notebook