{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Secant method" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given initial guess $x_0$, $x_1$, we find the next estimate by\n", "$$\n", "x_2 = x_1 - f(x_1) \\frac{x_1 - x_0}{f(x_1) - f(x_0)}\n", "$$\n", "We iterate until\n", "$$\n", "|x_2 - x_1| < |x_2| \\epsilon\n", "$$\n", "Let us find roots of\n", "$$\n", "f(x) = \\exp(x) - \\frac{3}{2} - \\arctan(x)\n", "$$" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%matplotlib inline\n", "%config InlineBackend.figure_format = 'svg'\n", "from numpy import exp, arctan, linspace, abs\n", "import matplotlib.pyplot as plt\n", "\n", "f = lambda x: exp(x) - 3.0/2.0 - arctan(x)\n", "\n", "x = linspace(0.0, 1.0, 100)\n", "plt.plot(x,f(x))\n", "plt.grid(True)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0.5 0.6 -0.31492633830067795 -0.21830069988007528\n", "1 0.6 0.8259241992584121 -0.21830069988007528 0.09364094333639517\n", "2 0.8259241992584121 0.7581046067123907 0.09364094333639517 -0.014440726409248872\n", "3 0.7581046067123907 0.7671659413813372 -0.014440726409248872 -0.0007430824196771324\n", "4 0.7671659413813372 0.7676575090185478 -0.0007430824196771324 6.472411304581094e-06\n", "Number of iterations = 5\n", "Final solution = 0.7676532662012716 -1.1213252548714081e-14\n" ] } ], "source": [ "M = 50 # maximum number of iterations\n", "eps = 1.0e-6 # tolerance for stopping\n", "x0, x1 = 0.5, 0.6\n", "\n", "for i in range(M):\n", " f0, f1 = f(x0), f(x1)\n", " x2 = x1 - f1*(x1 - x0)/(f1 - f0)\n", " if abs(x2-x1) < abs(x2)*eps:\n", " break\n", " print(i, x0, x1, f0, f1)\n", " x0, x1 = x1, x2\n", " \n", "print(\"Number of iterations = \", i)\n", "print(\"Final solution = \", x2, f(x2))" ] } ], "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }