{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MATH 375 Assignment 2\n",
    "\n",
    "**By Brenton LeMesurier**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Due Friday February 5**\n",
    "\n",
    "**Drafts encouraged by Friday January 29**, for Python code in particular."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Objectives"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n",
    "## A test case\n",
    "\n",
    "As a first test case you can use the familiar example\n",
    "$x - \\cos(x) = 0$,\n",
    "which we have seen to have a unique root that lies in the interval $[-1, 1]$.\n",
    "\n",
    "Here is a definition for the test function $f$.\n",
    "Note that I get the cosine function from the module `numpy` rather than the standard Python module `math`, because `numpy` will be far more useful for us, and so I encourage you to **avoid module `math` as much as possible!**\n",
    "\n",
    "    from numpy import cos\n",
    "    def f(x):\n",
    "        return x - cos(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 1\n",
    "\n",
    "The equation\n",
    "$x^3 -2x + 1 = 0$\n",
    "can be written as a fixed point equation in many ways, including\n",
    "1. $\\displaystyle x = \\frac{x^3 + 1}{2}$\n",
    "<br>\n",
    "and\n",
    "2. $x = \\sqrt[3]{2x-1}$\n",
    "\n",
    "For each of these options:\n",
    "\n",
    "(a) Verify that its fixed points do in fact solve the above cubic equation.\n",
    "\n",
    "(b) Determine whether fixed point iteration with it will converge to the solution $r=1$.\n",
    "(assuming a \"good enough\" initial approximation).\n",
    "\n",
    "**Note:** computational experiments can be a useful start, but prove your answers mathematically!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 2\n",
    "\n",
    "a) Show that Newton's method applied to\n",
    "\n",
    "$$ f(x) = x^k - a $$\n",
    "\n",
    "leads to fixed point iteration with function\n",
    "\n",
    "$$ g(x) = \\frac{(k-1) x + \\displaystyle \\frac{a}{x^{k-1}}}{k}. $$\n",
    "\n",
    "b) Then verify mathematically that the iteration\n",
    "$x_{k+1} = g(x_k)$ has *super-linear* convergence."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 3\n",
    "\n",
    "a) Create a Python function for Newton's method,\n",
    "with usage\n",
    "    \n",
    "    (root, errorEstimate, iterations, functionEvaluations) = newton(f, Df, x_0, errorTolerance, maxIterations)\n",
    "\n",
    "(The last input parameter `maxIterations` could be optional, with a default like `maxIterations=100`.)\n",
    "\n",
    "b) based on your function `bisection2` create a third (and final!) version with usage\n",
    "\n",
    "    (root, errorBound, iterations, functionEvaluations) = bisection(f, a, b, errorTolerance, maxIterations)\n",
    "\n",
    "c) Use both of these to solve the equation\n",
    "\n",
    "$$ f_1(x) = 10 - 2x + \\sin(x) = 0 $$\n",
    "\n",
    "i) with [estimated] absolute error of no more than $10^{-6}$, 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.\n",
    "\n",
    "Graph the function, which will help to find a good starting interval $[a, b]$ and initial approximation $x_0$.\n",
    "\n",
    "d) Repeat, this time finding the unique real root of\n",
    "\n",
    "$$ f_2(x) = x^3 - 3.3 x^2 + 3.63 x - 1.331 = 0 $$\n",
    "\n",
    "Again graph the function, to find a good starting interval $[a, b]$ and initial approximation $x_0$.\n",
    "\n",
    "e) This second case will behave differently than for $f_1$ in part (c): describe the difference.\n",
    "(We will discuss the reasons in class.)"
   ]
  },
  {
   "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
}
