{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Instability of forward Euler scheme"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider the ODE\n",
"$$\n",
"y' = -5 t y^2 + \\frac{5}{t} - \\frac{1}{t^2}, \\qquad t \\ge 1\n",
"$$\n",
"with initial condition\n",
"$$\n",
"y(1) = 1\n",
"$$\n",
"The exact solution is\n",
"$$\n",
"y(t) = \\frac{1}{t}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'svg'\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Right hand side function"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def f(t,y):\n",
" return -5.0*t*y**2 + 5.0/t - 1.0/t**2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exact solution"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def yexact(t):\n",
" return 1.0/t"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This implements Euler method\n",
"$$\n",
"y_n = y_{n-1} + h f(t_{n-1},y_{n-1})\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def euler(t0,T,y0,h):\n",
" N = int((T-t0)/h)\n",
" y = np.zeros(N)\n",
" t = np.zeros(N)\n",
" y[0] = y0\n",
" t[0] = t0\n",
" for n in range(1,N):\n",
" y[n] = y[n-1] + h*f(t[n-1],y[n-1])\n",
" t[n] = t[n-1] + h\n",
" return t, y"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of iterations= 126\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"t0 = 1.0\n",
"y0 = 1.0\n",
"T = 25.0\n",
"h = 0.19\n",
"t,y = euler(t0,T,y0,h)\n",
"print('Number of iterations=',len(t))\n",
"te = np.linspace(t0,T,100)\n",
"ye = yexact(te)\n",
"plt.plot(t,y,te,ye,'--')\n",
"plt.legend(('Numerical','Exact'))\n",
"plt.xlabel('t')\n",
"plt.ylabel('y')\n",
"plt.title('Step size = ' + str(h));"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of iterations= 114\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"h = 0.21\n",
"t,y = euler(t0,T,y0,h)\n",
"print('Number of iterations=',len(t))\n",
"plt.plot(t,y,te,ye,'--')\n",
"plt.legend(('Numerical','Exact'))\n",
"plt.xlabel('t')\n",
"plt.ylabel('y')\n",
"plt.title('Step size = ' + str(h));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Adaptive stepping"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will use a variable time step\n",
"$$\n",
"y_n = y_{n-1} + h_{n-1} f(t_{n-1}, y_{n-1})\n",
"$$\n",
"where\n",
"$$\n",
"h_{n-1} = \\frac{1}{|f_y(t_{n-1},y_{n-1})|} = \\frac{1}{10 t_{n-1} |y_{n-1}|}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def aeuler(t0,T,y0):\n",
" t, y = [], []\n",
" y.append(y0)\n",
" t.append(t0)\n",
" time = t0; n = 1\n",
" while time < T:\n",
" h = 1.0/np.abs(10*t[n-1]*y[n-1])\n",
" y.append(y[n-1] + h*f(t[n-1],y[n-1]))\n",
" time = time + h\n",
" t.append(time)\n",
" n = n + 1\n",
" return np.array(t), np.array(y)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of iterations= 241\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"t,y = aeuler(t0,T,y0)\n",
"print('Number of iterations=',len(t))\n",
"\n",
"plt.plot(t,y,te,ye,'--')\n",
"plt.legend(('Numerical','Exact'))\n",
"plt.xlabel('t')\n",
"plt.ylabel('y');\n",
"\n",
"plt.figure()\n",
"plt.plot(range(len(t)-1),t[1:]-t[0:-1])\n",
"plt.ylabel('Step size, h')\n",
"plt.xlabel('Iteration');"
]
}
],
"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": 1
}