notes

uni notes
git clone git://popovic.xyz/notes.git
Log | Files | Refs

prb13_p.ipynb (4408B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "code",
      5    "execution_count": 112,
      6    "id": "f76cba3a",
      7    "metadata": {},
      8    "outputs": [],
      9    "source": [
     10     "import numpy as np\n",
     11     "from matplotlib import pyplot as plt\n",
     12     "from ipywidgets import interact"
     13    ]
     14   },
     15   {
     16    "cell_type": "code",
     17    "execution_count": 178,
     18    "id": "6b2ba2bc",
     19    "metadata": {},
     20    "outputs": [
     21     {
     22      "data": {
     23       "application/vnd.jupyter.widget-view+json": {
     24        "model_id": "e9c420d230764515bbfa697e2d58d2b1",
     25        "version_major": 2,
     26        "version_minor": 0
     27       },
     28       "text/plain": [
     29        "interactive(children=(IntSlider(value=40, description='n', max=80, min=1), Output()), _dom_classes=('widget-in…"
     30       ]
     31      },
     32      "metadata": {},
     33      "output_type": "display_data"
     34     },
     35     {
     36      "data": {
     37       "text/plain": [
     38        "<function __main__.change_step_euler(n)>"
     39       ]
     40      },
     41      "execution_count": 178,
     42      "metadata": {},
     43      "output_type": "execute_result"
     44     }
     45    ],
     46    "source": [
     47     "def eulers_method(f, x0, a, b, n):\n",
     48     "    x = np.zeros(n)\n",
     49     "    x[0] = x0\n",
     50     "    h = (b-a)/n\n",
     51     "    t = [a + h*i for i in range(n)]\n",
     52     "    for i in range(n-1):\n",
     53     "        x[i+1] = x[i] + h*f(t[i], x[i])\n",
     54     "    return x, t\n",
     55     "        \n",
     56     "a_euler = 0; b_euler = 1; x0_euler = 1;\n",
     57     "f_euler = lambda t, x: x\n",
     58     "\n",
     59     "def change_step_euler(n):\n",
     60     "    y_euler, t_euler = eulers_method(f_euler, x0_euler, a_euler, b_euler, n)\n",
     61     "    \n",
     62     "    t_ana_euler = np.linspace(0, 1, 100)\n",
     63     "    y_ana_euler = x0_euler*np.exp(t_ana_euler)\n",
     64     "    \n",
     65     "    plt.figure(figsize=(10, 8))\n",
     66     "    plt.plot(t_euler, y_euler, '.-', label='euler', lw=2, ms=10)\n",
     67     "    plt.plot(t_ana_euler, y_ana_euler, label='Analytical')\n",
     68     "    plt.legend(loc='best')\n",
     69     "\n",
     70     "interact(change_step_euler, n=(1, 80))"
     71    ]
     72   },
     73   {
     74    "cell_type": "code",
     75    "execution_count": 179,
     76    "id": "76c4d4a5",
     77    "metadata": {},
     78    "outputs": [
     79     {
     80      "data": {
     81       "application/vnd.jupyter.widget-view+json": {
     82        "model_id": "fe2121b2a7bc4d7890c6d9522f3f2d79",
     83        "version_major": 2,
     84        "version_minor": 0
     85       },
     86       "text/plain": [
     87        "interactive(children=(IntSlider(value=10, description='n', max=20, min=1), Output()), _dom_classes=('widget-in…"
     88       ]
     89      },
     90      "metadata": {},
     91      "output_type": "display_data"
     92     },
     93     {
     94      "data": {
     95       "text/plain": [
     96        "<function __main__.change_step_kuta(n)>"
     97       ]
     98      },
     99      "execution_count": 179,
    100      "metadata": {},
    101      "output_type": "execute_result"
    102     }
    103    ],
    104    "source": [
    105     "def runge_kutta_4(x0, f, a, b, n):\n",
    106     "    h = (b-a)/n\n",
    107     "    t = [a+h*i for i in range(n)]\n",
    108     "    x = np.zeros(n)\n",
    109     "    x[0] = x0\n",
    110     "    \n",
    111     "    for i in range(n-1):\n",
    112     "        k_1 = f(t[i], x[i])\n",
    113     "        k_2 = f(t[i] + h/2, x[i] + h*k_1/2)\n",
    114     "        k_3 = f(t[i] + h/2, x[i] + h*k_2/2)\n",
    115     "        k_4 = f(t[i] + h, x[i] + h*k_3)\n",
    116     "        \n",
    117     "        x[i+1] = x[i] + h/6*(k_1 + 2*k_2 + 2*k_3 + k_4)\n",
    118     "\n",
    119     "    return x, t\n",
    120     "\n",
    121     "a_kuta = 0; b_kuta = 1; x0_kuta = 1\n",
    122     "f_kuta = lambda t, x: 2*t*x\n",
    123     "\n",
    124     "def change_step_kuta(n):\n",
    125     "    y_runge, t_runge = runge_kutta_4(x0_kuta, f_kuta, a_kuta, b_kuta, n)\n",
    126     "    \n",
    127     "    t_ana_kuta = np.linspace(0, 1, 100)\n",
    128     "    y_ana_kuta = x0_kuta*np.exp(t_ana_kuta**2)\n",
    129     "    \n",
    130     "    plt.figure(figsize=(10, 8))\n",
    131     "    plt.plot(t_runge, y_runge, '.-', label='Runge-Kutta-4', lw=2, ms=10)\n",
    132     "    plt.plot(t_ana_kuta, y_ana_kuta, label='Analytical')\n",
    133     "    plt.legend(loc='best')\n",
    134     "\n",
    135     "interact(change_step_kuta, n=(1, 20))"
    136    ]
    137   },
    138   {
    139    "cell_type": "code",
    140    "execution_count": null,
    141    "id": "75aa43db",
    142    "metadata": {},
    143    "outputs": [],
    144    "source": []
    145   }
    146  ],
    147  "metadata": {
    148   "kernelspec": {
    149    "display_name": "Python 3",
    150    "language": "python",
    151    "name": "python3"
    152   },
    153   "language_info": {
    154    "codemirror_mode": {
    155     "name": "ipython",
    156     "version": 3
    157    },
    158    "file_extension": ".py",
    159    "mimetype": "text/x-python",
    160    "name": "python",
    161    "nbconvert_exporter": "python",
    162    "pygments_lexer": "ipython3",
    163    "version": "3.10.5"
    164   }
    165  },
    166  "nbformat": 4,
    167  "nbformat_minor": 5
    168 }