{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Instability of multistep method"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Conside the ODE\n",
"$$\n",
"y' = -y\n",
"$$\n",
"with initial condition\n",
"$$\n",
"y(0) = 1\n",
"$$\n",
"The exact solution is\n",
"$$\n",
"y(t) = \\exp(-t)\n",
"$$\n",
"Let us use a 2-step, second order method\n",
"$$\n",
"y_{n+2} - 2.01 y_{n+1} + 1.01 y_n = h[0.995 f_{n+1} - 1.005 f_n]\n",
"$$\n",
"We will use the initial conditions\n",
"$$\n",
"y_0 =1, \\qquad y_1 = \\exp(-h)\n",
"$$\n",
"Then solving for $y_{n+2}$\n",
"$$\n",
"y_{n+2} = 2.01 y_{n+1} - 1.01 y_n + h[0.995 f_{n+1} - 1.005 f_n]\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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": [
"Exact solution"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def yexact(t):\n",
" return np.exp(-t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The next function implements the 2-step method."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def f(t,y):\n",
" return -y\n",
"\n",
"def solve(t0,T,y0,h):\n",
" N = int((T-t0)/h)\n",
" y = np.zeros(N)\n",
" t = np.zeros(N)\n",
" t[0], y[0] = t0, y0\n",
" t[1], y[1] = t0+h, np.exp(-(t0+h))\n",
" for n in range(2,N):\n",
" t[n] = t[n-1] + h\n",
" y[n] = 2.01*y[n-1] - 1.01*y[n-2] \\\n",
" + h*(0.995*f(t[n-1],y[n-1]) - 1.005*f(t[n-2],y[n-2]))\n",
" return t, y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solve for decreasing h"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"