{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ODE with periodic solution"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider the ODE system\n",
"$$\n",
"x' = -y, \\qquad y' = x\n",
"$$\n",
"with initial condition\n",
"$$\n",
"x(0) = 1, \\qquad y(0) = 0\n",
"$$\n",
"The exact solution is\n",
"$$\n",
"x(t) = \\cos(t), \\qquad y(t) = \\sin(t)\n",
"$$\n",
"This solution is periodic. It also has a quadratic invariant\n",
"$$\n",
"x^2(t) + y^2(t) = 1, \\qquad \\forall t\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 15,
"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": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"theta = np.linspace(0.0, 2*np.pi, 500)\n",
"xe = np.cos(theta)\n",
"ye = np.sin(theta)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Forward Euler scheme"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def ForwardEuler(h,T):\n",
" N = int(T/h)\n",
" x,y = np.zeros(N),np.zeros(N)\n",
" x[0] = 1.0\n",
" y[0] = 0.0\n",
" for n in range(1,N):\n",
" x[n] = x[n-1] - h*y[n-1]\n",
" y[n] = y[n-1] + h*x[n-1]\n",
" return x,y"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"