- \n",
"
- To reinforce what you have learnt in lectures and lab classes this semester.\n", "

- \n",
"
- 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! \n", " \n", "
- Press Shift-Enter to execute the code in a cell.\n", "\n", "
- Submit via http://somas-uploads.shef.ac.uk/mas212 by 5pm on Mon 12th Dec.
*If you are off-campus, you will need to use a VPN to submit.*\n", "\n", " - Each question will be marked out of 5.\n", "\n", "
- 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**.*Please give answers in your own words.*\n", " \n", " - Class test 2 is intended to be more challenging than Class Test 1. Please
**don't**spend an excessive amount of time on this, as it is only worth $5\\%$ of the module mark.\n", " \n", " - All submissions will be automatically checked for excessive similarities. Where there is sufficient circumstantial evidence of excessive similarities, a mark of zero will be awarded for this test.\n", "

- \n",
"
- Lecture materials and links on course web page: http://sam-dolan.staff.shef.ac.uk/mas212/\n", "

\n", "

\n", "When does the iterative scheme $x$ $\\rightarrow$ $Rx + c$ converge? (Here $x$ and $c$ are column vectors.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "(b) Consider the linear system $A (x + \\Delta x) = b + \\Delta b$. Without proof, give an upper bound for $|| \\Delta x ||$ in terms of row sum norms of $x$, $b$, $\\Delta b$, $A$ and $A^{-1}$.

\n", "(c) Find the condition number of the matrix below. Is $Ax = b$ well-conditioned or ill-conditioned, in this case?\n", "$$\n", "A = \\begin{bmatrix} 1 & 1/2 & 1/3 & 1/4 \\\\ 1/2 & 1/3 & 1/4 & 1/5 \\\\ 1/3 & 1/4 & 1/5 & 1/6 \\\\ 1/4 & 1/5 & 1/6 & 1/7 \\end{bmatrix}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "(b) If the numerical scheme is

\n", "(c) Briefly describe a practical method for checking that your implementation has the correct order of convergence." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "(b) Approximately, what is the maximum step size $h_{max}$ needed to avoid this problem with the forward Euler method?

\n", "(c) Suggest an alternative method to avoid this problem.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "

\n", "Use odeint() to find a numerical solution to the ODE system\n", "\\begin{eqnarray}\n", "\\frac{dx}{dt} &=& -\\left( x^3 + x y^2 + y - x \\right) \\nonumber \\\\\n", "\\frac{dy}{dt} &=& -\\left( y^3 + x^2 y - y - x \\right) \\nonumber\n", "\\end{eqnarray}\n", "\n", "on the domain $t \\in [0,20]$ with initial condition $x(0) = 0.01$, $y(0) = 0$.

\n", "\n", "Plot a phase portrait and briefly describe what you observe (in your own words). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "Use matplotlib.animation.FuncAnimation() to create an animation with 400 frames, in which the $i$th frame shows a plot of $y(x)$ over the domain $x \\in [-5,5]$ and range $y \\in [-0.4,1]$ where\n", "$$\n", "y(x) = \\frac{\\sin(\\alpha_i x)}{\\alpha_i x}, \\quad \\quad \\alpha_i = 4 \\sin\\left( \\frac{\\pi i}{400} \\right) + 0.1 .\n", "$$" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#### Please choose between these two options: \n", "# %matplotlib # Uncomment this line to get the animation in a separate window.\n", "# %matplotlib nbagg # Or, uncomment this line if you want the animation to appear in the notebook." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "Write code to obtain a numerical solution of the differential equation\n", "$$\n", "\\frac{dx}{dt} = -x + \\left\\{ 1+ \\tanh(t-20) \\right\\} \\sin t, \\quad \\quad x(t < 0) = 0,\n", "$$\n", "using the linear two-step method, with a step size of $h=0.02$ on the domain $t \\in [0, 60]$.

\n", "Plot your solution.

\n", "Print the numerical value of $x(60)$ to 12 decimal places." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "Solve the 3D linear system $(A + \\Delta A) x = b$ for $x$ by using a suitable iterative method, with $A$, $\\Delta A$, $b$ as given below." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "np.random.seed(1)\n", "A = np.diag([2,3,-1])\n", "dA = np.matrix(0.2*(2*np.random.rand(9)-1).reshape(3,3)) # This is \"Delta A\"\n", "b = np.matrix(\"1 ; 2 ; 3\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

For the data set below, find the line of best fit $y = mx + c$, using linear regression.

\n", "Calculate $m$ and $c$, with any valid method, and print to screen.

\n", "Plot the data set along with the line of best fit.

" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [], "source": [ "xs = np.array([ 0.01777072, -0.01156888, 0.21384365, 0.37845755, 0.43702859,\n", " 0.58597224, 0.66804486, 0.77566893, 0.89928527, 1.01621027])\n", "ys = np.array([-0.25776996, -0.3336627 , -0.24307303, 0.64467634, 0.89869925,\n", " 0.87292456, 0.6649084 , 1.2645241 , 1.88451126, 1.69648964])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "The array below gives the Fourier coefficients $\\tilde{X}_k$ which were calculated from a data set $x_j$ using the function np.fft.fft(). Apply an