{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# p25: Stability regions for ODE formulas"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%config InlineBackend.figure_format='svg'\n",
"from numpy import pi,real,imag,zeros,exp,arange\n",
"from matplotlib.pyplot import figure,subplot,plot,axis,grid,title"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"figure(figsize=(8,8))\n",
"\n",
"# Adams-Bashforth\n",
"subplot(2,2,1)\n",
"z = exp(1j*pi*arange(0,201)/100); r = z - 1\n",
"s = 1; rr = r/s; plot(real(rr),imag(rr))\n",
"s = (3 - 1/z)/2; rr = r/s; plot(real(rr),imag(rr))\n",
"s = (23 - 16/z + 5/z**2)/12; rr = r/s; plot(real(rr),imag(rr))\n",
"axis('equal'); grid('on')\n",
"title('Adams-Bashforth')\n",
"\n",
"# Adams-Moulton\n",
"subplot(2,2,2)\n",
"s = (5*z + 8 - 1/z)/12; rr = r/s; plot(real(rr),imag(rr))\n",
"s = (9*z + 19 - 5/z + 1/z**2)/24; rr = r/s; plot(real(rr),imag(rr))\n",
"s = (251*z + 646 - 264/z + 106/z**2 - 19/z**3)/720; rr = r/s; plot(real(rr),imag(rr))\n",
"d = 1 - 1/z\n",
"s = 1 - d/2 - d**2/12 - d**3/24 - 19*d**4/720 - 3*d**5/160; dd = d/s; plot(real(dd),imag(dd))\n",
"axis('equal'); grid('on')\n",
"title('Adams-Moulton')\n",
"\n",
"# Backward differentiation\n",
"subplot(2,2,3)\n",
"r = 0\n",
"for i in range(1,7):\n",
" r = r + d**i/i; plot(real(r),imag(r))\n",
"axis('equal'); grid('on')\n",
"title('Backward differentiation')\n",
"\n",
"# Runge-kutta\n",
"subplot(2,2,4)\n",
"w = 0; W = 1j*zeros(len(z)); W[0] = w;\n",
"for i in range(1,len(z)):\n",
" w = w - (1+w-z[i]); W[i] = w\n",
"plot(real(W),imag(W))\n",
"w = 0; W = 1j*zeros(len(z)); W[0] = w;\n",
"for i in range(1,len(z)):\n",
" w = w - (1+w+0.5*w**2-z[i]**2)/(1+w); W[i] = w\n",
"plot(real(W),imag(W))\n",
"w = 0; W = 1j*zeros(len(z)); W[0] = w;\n",
"for i in range(1,len(z)):\n",
" w = w - (1+w+0.5*w**2+w**3/6-z[i]**3)/(1+w+0.5*w**2); W[i] = w\n",
"plot(real(W),imag(W))\n",
"w = 0; W = 1j*zeros(len(z)); W[0] = w;\n",
"for i in range(1,len(z)):\n",
" w = w - (1+w+0.5*w**2+w**3/6+w**4/24-z[i]**4)/(1+w+w**2/2+w**3/6); W[i] = w\n",
"plot(real(W),imag(W))\n",
"axis('equal'); grid('on')\n",
"title('Runge-Kutta');"
]
}
],
"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.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 1
}