{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Root Finding by Interval Halving, Exercise 2 template\n",
"\n",
"Author: ...\n",
"\n",
"Last revised on August 26, 2024"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction\n",
"\n",
"This is a template for working on Exercise 2 of Section 1.1, *Root Finding by Interval Halving*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a Python function implementing this better algorithm, with usage\n",
"\n",
" (root, errorBound) = bisection2(f, a, b, errorTolerance)\n",
"\n",
"Note that this also improves the output, by giving information about the accuracy of the output result:\n",
"we should always aim at this from now on.\n",
"\n",
"(Again I have changed the names of the error tolerance and error bound from $E_{tol}$ and $E_{max}$,\n",
"both to be more descriptive and to avoid subscripts.\n",
"Note also the \"camel case\" style: concatenating words [since spaces are not allowed in names] and capitalizing each new word.\n",
"Another popular option is to replace each space in a descriptive phrase by the underscore \"_\" as with `error_tolerance`.)\n",
"\n",
"Test it with the above example: $f(x) = x - \\cos x$, $[a, b] = [-1, 1]$,\n",
"this time accurate to within $10^{-4}$.\n",
"\n",
"Use the fact that there is a solution in the interval $(-1, 1)$."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# We will often need resources from the modules numpy and pyplot:\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# We can also import items from a module individually, so they can be used by \"first name only\".\n",
"# This will often be done for mathematical functions.\n",
"from numpy import cos"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def bisection2(f, a, b, errorTolerance):\n",
" \"\"\"\n",
" This is a \"stub\": it functions in that it is \"syntactically correct\",\n",
" but does not do the right thing.\n",
" Instead it gives the best available answers without having done any real work!\n",
"\n",
" Inputs:\n",
" f: a continuous function from and to real values\n",
" a: to be continued ...\n",
" \"\"\"\n",
" root=(a+b)/2\n",
" errorBound=(b-a)/2\n",
" return (root, errorBound)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Aside: look what the function `help` does:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function bisection2 in module __main__:\n",
"\n",
"bisection2(f, a, b, errorTolerance)\n",
" This is a \"stub\": it functions in that it is \"syntactically correct\",\n",
" but does not do the right thing.\n",
" Instead it gives the best available answers without having done any real work!\n",
" \n",
" Inputs:\n",
" f: a continuous function from and to real values\n",
" a: to be continued ...\n",
"\n"
]
}
],
"source": [
"help(bisection2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now test this, by first defining the needed inputs `f`, `a`, `b` and the error tolerance `errorTolerance` ..."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 4
}