{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"nbpresent": {
"id": "007bb239-9038-4648-8c95-a1f429f40816"
}
},
"source": [
"# ODE using forward Euler"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpresent": {
"id": "5ce4c702-e106-43e5-8240-e4a75b6a02fe"
}
},
"source": [
"Consider the ODE\n",
"$$\n",
"y' = y, \\qquad t \\ge 0\n",
"$$\n",
"with initial condition\n",
"$$\n",
"y(0) = 1\n",
"$$\n",
"The exact solution is\n",
"$$\n",
"y(t) = \\exp(t)\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"nbpresent": {
"id": "d9a639b6-f8dd-487e-9cb2-8c4fb8419235"
}
},
"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": {
"nbpresent": {
"id": "bc5dd679-e120-45ab-92bf-4d636f05635e"
}
},
"source": [
"Right hand side function"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"nbpresent": {
"id": "0f38fe14-102f-4734-b083-0eace0da1f1b"
}
},
"outputs": [],
"source": [
"def f(t,y):\n",
" return y"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpresent": {
"id": "0043cfcd-9dd8-4b90-b371-60a72948422e"
}
},
"source": [
"Exact solution"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"nbpresent": {
"id": "398686b2-3a49-499a-a3f7-33d9369eee15"
}
},
"outputs": [],
"source": [
"def yexact(t):\n",
" return np.exp(t)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpresent": {
"id": "befe5152-527f-4f5b-a4fd-7326f8c499a4"
}
},
"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": 19,
"metadata": {
"nbpresent": {
"id": "7563d1c2-fc26-4bd1-bfa9-4fa98bb6e148"
}
},
"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": 20,
"metadata": {
"nbpresent": {
"id": "fd0ac33c-6fdd-4779-ab37-bb20c9b21d70"
}
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"t0,y0,T = 0.0,1.0,5.0\n",
"\n",
"te = np.linspace(t0,T,100)\n",
"ye = yexact(te)\n",
"plt.plot(te,ye,'--')\n",
"\n",
"H = [0.2,0.1,0.05]\n",
"\n",
"for h in H:\n",
" t,y = euler(t0,T,y0,h)\n",
" plt.plot(t,y)\n",
"\n",
"plt.legend(('Exact','0.2','0.1','0.05'),loc=2)\n",
"plt.xlabel('t')\n",
"plt.ylabel('y')\n",
"plt.grid(True);"
]
}
],
"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
}