{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# MATH 4242 Python Lab 9: Complex eigenvalues and discrete dynamics\n",
        "\n",
        "**Course sections:** 8.2--8.3\n",
        "\n",
        "This notebook accompanies LEAP Assignment 9. LEAP supplies your\n",
        "parameter values and grades your mathematical responses. Copy those\n",
        "values into the indicated cell below. Run a cell with **Shift+Enter**.\n",
        "\n",
        "The notebook is a computational scratchpad: your graded answers are\n",
        "entered in LEAP unless your instructor says otherwise.\n",
        "\n",
        "**Programming support:** Independent application. Use the patterns from earlier labs, consulting the hints when needed.\n",
        "\n",
        "A cell marked `TODO` is intentionally unfinished. Read the directions\n",
        "immediately above it before running it. Placeholders such as zero keep\n",
        "the notebook runnable, but their output is not an answer. If you are\n",
        "new to notebooks,\n",
        "complete `00_Getting_Started.ipynb` before this lab.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "np.set_printoptions(precision=6, suppress=True)\n",
        "print(\"NumPy version:\", np.__version__)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1. A rotation-contraction matrix\n",
        "\n",
        "Copy positive integers `r` and `s` from LEAP and set $d=r+s$. The\n",
        "matrix below has complex-conjugate eigenvalues. NumPy stores complex\n",
        "values using `j`, so $2+3i$ appears as `2+3j`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Replace these example values with your LEAP values.\n",
        "r = 2\n",
        "s = 3\n",
        "d = r + s\n",
        "\n",
        "A = (1/d) * np.array([[r, -s],\n",
        "                      [s,  r]], dtype=float)\n",
        "\n",
        "# TODO: compute the eigenvalues and their magnitudes.\n",
        "eigenvalues = np.zeros(2, dtype=complex)  # TODO: use np.linalg.eigvals\n",
        "magnitudes = np.zeros(2)                  # TODO: use np.abs\n",
        "print(\"A =\\n\", A)\n",
        "print(\"eigenvalues:\", eigenvalues)\n",
        "print(\"magnitudes:\", magnitudes)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2. Iterate the dynamical system\n",
        "\n",
        "Starting with $x_0=(1,0)^T$, repeatedly apply $x_{n+1}=Ax_n$.\n",
        "Store every state so the trajectory can be inspected and plotted.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x = np.array([1.0, 0.0])\n",
        "trajectory = [x.copy()]\n",
        "\n",
        "# Study this loop, then try changing 20 to another positive integer.\n",
        "for n in range(20):\n",
        "    x = A @ x\n",
        "    trajectory.append(x.copy())\n",
        "\n",
        "trajectory = np.array(trajectory)\n",
        "print(\"x_1 =\", trajectory[1])\n",
        "print(\"x_20 =\", trajectory[-1])\n",
        "print(\"||x_20|| =\", np.linalg.norm(trajectory[-1]))\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "\n",
        "plt.plot(trajectory[:, 0], trajectory[:, 1], \"o-\")\n",
        "plt.axhline(0, color=\"black\", linewidth=0.7)\n",
        "plt.axvline(0, color=\"black\", linewidth=0.7)\n",
        "plt.gca().set_aspect(\"equal\", adjustable=\"box\")\n",
        "plt.xlabel(\"first component\")\n",
        "plt.ylabel(\"second component\")\n",
        "plt.title(\"Trajectory of x[n+1] = A x[n]\")\n",
        "plt.grid(alpha=0.25)\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Reflection.** Connect the spiral in the plot to the nonzero imaginary\n",
        "part and to the common magnitude of the eigenvalues. State the\n",
        "long-term behavior in LEAP.\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}
