{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "601a7055-0a41-40d7-b68b-f278c075b0ec",
   "metadata": {},
   "source": [
    "An example problem:\n",
    "$${\\bf A} = \\left[ \\matrix{ a & b \\cr c & d } \\right] $$\n",
    "$$ a = -2, b = -2, c = -2, d = 5$$\n",
    "$$ \\lambda_1 = -1, \\lambda_2 = -6$$\n",
    "\n",
    "$${\\bf{v}}_1 = [2,-1]^T, \\\\ {\\bf{v}}_2 = [1,2]^T $$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a610815b-f106-4ad6-bb8f-06d8a7c89049",
   "metadata": {},
   "source": [
    "Let's plot this over the phase space. We're going to integrate backwards in time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7949eb12-4f1a-4046-9aa7-3b11fd00125a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Solve some simple ODEs\n",
    "from scipy.integrate import odeint\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1ffc36f4-4c21-4b5e-8470-ed4217b3feb4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 1000x1000 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize = (10,10))\n",
    "a,b,c,d = -2, -2, -2, -5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f1279f76-319e-4cb4-8af9-2d015f8b98ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "#define function to integrate\n",
    "def dx_dt(x,t):\n",
    "    term1 = a*x[0] + b*x[1]\n",
    "    term2 = c*x[0] + d*x[1]\n",
    "    return [term1, term2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0e62fabe-c9a7-4cab-8116-d5832b4712c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 176080 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#forward in time\n",
    "ts = np.linspace(0,20,500)\n",
    "ic = np.linspace(-1,1,10)\n",
    "\n",
    "for r in ic:\n",
    "    for s in ic:\n",
    "        x0 = [r,s]\n",
    "        xs = odeint(dx_dt, x0, ts)\n",
    "        plt.plot(xs[:,0], xs[:,1],\"g\")\n",
    "\n",
    "#plt.show\n",
    "#We really can't see the ones that come back out..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cad84a08-cf4c-4ad9-8334-d42ef30003b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'y')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 68052 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#Backward in time\n",
    "ts = np.linspace(0,-0.1,500)\n",
    "\n",
    "for r in ic:\n",
    "    for s in ic:\n",
    "        x0 = [r,s]\n",
    "        xs = odeint(dx_dt, x0, ts)\n",
    "        plt.plot(xs[:,0], xs[:,1],\"r\")\n",
    "\n",
    "plt.show\n",
    "plt.xlabel('x',fontsize = 15)\n",
    "plt.ylabel('y',fontsize = 15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b3217fd9-ba1e-4be0-820a-2a9a68ab6d35",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.quiver.Quiver at 0x7acfe8b7afc0>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 107508 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#Making a quiver plot. Use numpy to create an M-grid\n",
    "X,Y = np.mgrid[-1:1:30j, -1:1:30j]\n",
    "P = a*X + b*Y\n",
    "Q = c*X + d*Y\n",
    "\n",
    "plt.quiver(X,Y,P,Q,color = \"b\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
