{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MATH 375 Assignment 3\n",
    "\n",
    "**By Brenton LeMesurier**\n",
    "\n",
    "**Revised on February 15, 2021**, adding \"Exercise 0\" and reformatting to work better as a template for your work."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Python code drafts due Wednesday February 17**\n",
    "(There is a bonus for doing this part on time!)\n",
    "\n",
    "**Final version due Monday February 22**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This file last run on 2021-02-22 at 16:32\n"
     ]
    }
   ],
   "source": [
    "# Get a \"time stamp\" for when the Python code in this notebook was last run.\n",
    "from time import strftime,localtime\n",
    "print(strftime(\"This file last run on %Y-%m-%d at %H:%M\", localtime()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 0: Using this notebook as a template for yours\n",
    "\n",
    "Start work on this assignment as follows:\n",
    "- 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*\n",
    "\n",
    "- Edit the top \"title page\" cell, inserting your name and the **current** date, and keep that date up-to-date.\n",
    "As an alternative on the date, you can use module `time` to get a date-and-time stamp; see above.\n",
    "\n",
    "- You might also want to update the `import` statements below as suggested there.\n",
    "\n",
    "- 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.\n",
    "\n",
    "Also, before uploading a notebook, do a \"clean run\" either with the \"fast-forward\" button above or with the menu selection\n",
    "\n",
    "    Kernel > Restart Kernel and Run all Cells ...\n",
    "\n",
    "and then check that the output is as you expect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# If you find it more convenient,\n",
    "# you can import some items on a \"first name basis\";\n",
    "# For example you can use `sin(x)` instead of `np.sin(x)` with:\n",
    "from numpy import sin\n",
    "# ... and `plot(...)` instead of `plt.plot(...)` and so on, with:\n",
    "from matplotlib.pyplot import figure, plot, title, grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 1: Comparing Root-finding Methods\n",
    "\n",
    "**Note:** This builds on the previous exercise comparing the Bisection and Newton's Methods; just adding the Secant Method."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A) Write a Python function implementing the secant method with usage\n",
    "\n",
    "    (root, errorEstimate, iterations, functionEvaluations) = secant(f, a, b, errorTolerance, maxIterations)\n",
    "\n",
    "Revise your previous implementations of the Bisection Method and Newton's method to mimic this interfacce:\n",
    "\n",
    "    (root, errorEstimate, iterations, functionEvaluations) = bisection(f, a, b, errorTolerance, maxIterations)\n",
    "    \n",
    "    (root, errorEstimate, iterations, functionEvaluations) = newton(f, x_0, errorTolerance, maxIterations)\n",
    "\n",
    "**Aside:** the last input parameter `maxIterations` could be optional, with a default like `maxIterations=100`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "B) Use these to solve the equation\n",
    "\n",
    "$$ 10 - 2x + \\sin(x) = 0 $$\n",
    "\n",
    "i) with [estimated] absolute error of no more than $10^{-8}$, and then\n",
    "\n",
    "ii) with [estimated] absolute error of no more than $10^{-15}$.\n",
    "\n",
    "Note in particular how many iterations and how many function evaluations are needed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "C) Discuss: rank these methods for speed, as indicated by these experiments, and explain your ranking."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 2\n",
    "\n",
    "A) For $x = 8.024$ and $y = 8.006$,\n",
    "- Round  each to three significant figures, giving $x_a$ and $y_a$.\n",
    "- Compute the absolute errors in each of these approximations, and in their difference as an approximation of $x - y$.\n",
    "- Compute the relative errors in each of these three approximations."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "B) Then look at rounding to only two significant digits!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 3\n",
    "\n",
    "A) Illustrate why computing the roots of the quadratic equation $ax^2 + bx + c = 0$ with the standard formula\n",
    "\n",
    "$$x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}$$\n",
    "\n",
    "can sometimes give poor accuracy when evaluated using machine arithmetic such as IEEE-64 floating-point arithmetic.\n",
    "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$.\n",
    "(It is sufficient to consider real value of the ocefficients.\n",
    "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.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "B) Then describe a careful procedure for always getting accurate answers.\n",
    "State the procedure first with words and mathematical formulas, and then express it in pseudo-code."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 4\n",
    "\n",
    "A) Solve the system of equations \n",
    "\n",
    "$$\n",
    "\\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]\n",
    "$$\n",
    "\n",
    "by naive Gaussian elimination.\n",
    "Do this *by hand*, rounding each intermediate result to four significant digits,\n",
    "and write down each intermediate version of the system of equations."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "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.\n",
    "Residual calculations must be done to high precision, so I recommend that you do this part with Python in a notebook."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "This work is licensed under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
