{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Newton method with double root"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider the function\n",
"$$\n",
"f(x) = (x-1)^2 \\sin(x)\n",
"$$\n",
"for which $x=1$ is a double root."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'svg'\n",
"from numpy import sin,cos,linspace,zeros,abs\n",
"from matplotlib.pyplot import plot,xlabel,ylabel,grid"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def f(x):\n",
" return (x-1.0)**2 * sin(x)\n",
"\n",
"def df(x):\n",
" return 2.0*(x-1.0)*sin(x) + (x-1.0)**2 * cos(x)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x = linspace(0.0,2.0,100)\n",
"plot(x,f(x))\n",
"xlabel('x')\n",
"ylabel('f(x)')\n",
"grid(True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def newton(x0,m):\n",
" n = 50\n",
" x = zeros(50)\n",
" x[0] = x0\n",
" print(\"%6d %24.14e\" % (0,x[0]))\n",
" for i in range(1,50):\n",
" x[i] = x[i-1] - m*f(x[i-1])/df(x[i-1])\n",
" if i > 1:\n",
" r = (x[i] - x[i-1])/(x[i-1]-x[i-2])\n",
" else:\n",
" r = 0.0\n",
" print(\"%6d %24.14e %14.6e\" % (i,x[i],r))\n",
" if abs(f(x[i])) < 1.0e-14:\n",
" break"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We first apply the standard newton method."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0 2.00000000000000e+00\n",
" 1 1.35163555744248e+00 0.000000e+00\n",
" 2 1.18244356861394e+00 2.609520e-01\n",
" 3 1.09450383817604e+00 5.197630e-01\n",
" 4 1.04837639635805e+00 5.245347e-01\n",
" 5 1.02452044172239e+00 5.171749e-01\n",
" 6 1.01235093391487e+00 5.101245e-01\n",
" 7 1.00619920246051e+00 5.055037e-01\n",
" 8 1.00310567444820e+00 5.028711e-01\n",
" 9 1.00155437342780e+00 5.014666e-01\n",
" 10 1.00077757303341e+00 5.007412e-01\n",
" 11 1.00038888338214e+00 5.003726e-01\n",
" 12 1.00019446594325e+00 5.001868e-01\n",
" 13 1.00009723903915e+00 5.000935e-01\n",
" 14 1.00004862103702e+00 5.000468e-01\n",
" 15 1.00002431089794e+00 5.000234e-01\n",
" 16 1.00001215554384e+00 5.000117e-01\n",
" 17 1.00000607779564e+00 5.000059e-01\n",
" 18 1.00000303890375e+00 5.000029e-01\n",
" 19 1.00000151945336e+00 5.000015e-01\n",
" 20 1.00000075972705e+00 5.000007e-01\n",
" 21 1.00000037986362e+00 5.000004e-01\n",
" 22 1.00000018993183e+00 5.000002e-01\n",
" 23 1.00000009496592e+00 5.000001e-01\n"
]
}
],
"source": [
"newton(2.0,1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Newton method is converging linearly. Now try the modified Newton method."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0 2.00000000000000e+00\n",
" 1 7.03271114884954e-01 0.000000e+00\n",
" 2 1.06293359720705e+00 -2.773614e-01\n",
" 3 1.00108318855754e+00 -1.719679e-01\n",
" 4 1.00000037565568e+00 1.750696e-02\n",
" 5 1.00000000000005e+00 3.469257e-04\n"
]
}
],
"source": [
"newton(2.0,2)"
]
}
],
"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.10.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}