MATH 375 Assignment 3

By Brenton LeMesurier

Revised on February 15, 2021, adding “Exercise 0” and reformatting to work better as a template for your work.

Python code drafts due Wednesday February 17 (There is a bonus for doing this part on time!)

Final version due Monday February 22

# Get a "time stamp" for when the Python code in this notebook was last run.
from time import strftime,localtime
print(strftime("This file last run on %Y-%m-%d at %H:%M", localtime()))
This file last run on 2021-02-22 at 16:32

Exercise 0: Using this notebook as a template for yours

Start work on this assignment as follows:

  • Download a copy of this notebook, saving it either under the same name assignment3.ipynb or with your name or initials appended in some way, such as assignment3-Brenton-LeMesurier.ipynb or assignment3-BL.ipynb

  • Edit the top “title page” cell, inserting your name and the current date, and keep that date up-to-date. As an alternative on the date, you can use module time to get a date-and-time stamp; see above.

  • You might also want to update the import statements below as suggested there.

  • Note that I have broken up each exercise into cells for each part — insert you work in between, so that the statement of each part is kept close to your work on it.

Also, before uploading a notebook, do a “clean run” either with the “fast-forward” button above or with the menu selection

Kernel > Restart Kernel and Run all Cells ...

and then check that the output is as you expect.

import numpy as np
import matplotlib.pyplot as plt

# If you find it more convenient,
# you can import some items on a "first name basis";
# For example you can use `sin(x)` instead of `np.sin(x)` with:
from numpy import sin
# ... and `plot(...)` instead of `plt.plot(...)` and so on, with:
from matplotlib.pyplot import figure, plot, title, grid

Exercise 1: Comparing Root-finding Methods

Note: This builds on the previous exercise comparing the Bisection and Newton’s Methods; just adding the Secant Method.

A) Write a Python function implementing the secant method with usage

(root, errorEstimate, iterations, functionEvaluations) = secant(f, a, b, errorTolerance, maxIterations)

Revise your previous implementations of the Bisection Method and Newton’s method to mimic this interfacce:

(root, errorEstimate, iterations, functionEvaluations) = bisection(f, a, b, errorTolerance, maxIterations)

(root, errorEstimate, iterations, functionEvaluations) = newton(f, x_0, errorTolerance, maxIterations)

Aside: the last input parameter maxIterations could be optional, with a default like maxIterations=100.

B) Use these to solve the equation

\[ 10 - 2x + \sin(x) = 0 \]

i) with [estimated] absolute error of no more than \(10^{-8}\), and then

ii) with [estimated] absolute error of no more than \(10^{-15}\).

Note in particular how many iterations and how many function evaluations are needed.

C) Discuss: rank these methods for speed, as indicated by these experiments, and explain your ranking.

Exercise 2

A) For \(x = 8.024\) and \(y = 8.006\),

  • Round each to three significant figures, giving \(x_a\) and \(y_a\).

  • Compute the absolute errors in each of these approximations, and in their difference as an approximation of \(x - y\).

  • Compute the relative errors in each of these three approximations.

B) Then look at rounding to only two significant digits!

Exercise 3

A) Illustrate why computing the roots of the quadratic equation \(ax^2 + bx + c = 0\) with the standard formula

\[x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}\]

can sometimes give poor accuracy when evaluated using machine arithmetic such as IEEE-64 floating-point arithmetic. This is not alwys a problem, so identify specifically the situations when this could occur, in terms of a condition on the coefficents \(a\), \(b\), and \(c\). (It is sufficient to consider real value of the ocefficients. Also as an aside, there is no loss pr precisio prbe, when th roots are non-real, so you only need consider quadratics with real roots.)

B) Then describe a careful procedure for always getting accurate answers. State the procedure first with words and mathematical formulas, and then express it in pseudo-code.

Exercise 4

A) Solve the system of equations

\[\begin{split} \left[ \begin{array}{ccc} 4. & 2. & 1. \\ 9. & 3. & 1. \\ 25. & 5. & 1. \end{array} \right] x = \left[ \begin{array}{c} 0.693147 \\ 1.098612 \\ 1.609438 \end{array} \right] \end{split}\]

by naive Gaussian elimination. Do this by hand, rounding each intermediate result to four significant digits, and write down each intermediate version of the system of equations.

B) Compute the residual vector \(r := b - A x_a \) and residual maximum norm \(\| r \|_\text{max} = \| b - A x_a \|_\text{max}\) for your approximation. Residual calculations must be done to high precision, so I recommend that you do this part with Python in a notebook.


This work is licensed under Creative Commons Attribution-ShareAlike 4.0 International