Skip to content
Extraits de code Groupes Projets
NM3_ex_solution.ipynb 7,26 ko
Newer Older
  • Learn to ignore specific revisions
  • {
     "cells": [
      {
       "cell_type": "markdown",
       "id": "narrow-newspaper",
       "metadata": {},
       "source": [
        "## Exercice : méthode de Romberg\n",
        "\n",
        "Estimation de \n",
        "\n",
        "$$\n",
        "\\int_0^{\\frac{3\\pi}{4}} \\sin x\\ dx\n",
        "$$\n",
        "\n",
        "Par la méthode de Romberg. Cette intégrale vaut $1 + 1/\\sqrt{2} \\approx 1.7071067811865475$"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 38,
       "id": "accomplished-coordinator",
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "R11=0.8330405509046936\n"
         ]
        }
       ],
       "source": [
        "import numpy as np\n",
        "import math\n",
        "\n",
        "def f(x):\n",
        "    return np.sin(x)\n",
        "\n",
        "# méthode des trapèzes avec 2 points:\n",
        "x_0 = 0\n",
        "x_n = 3 * math.pi / 4\n",
        "R11 = (f(x_0) + f(x_n))/2 * (x_n - x_0)\n",
        "print(f\"{R11=}\")"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "annoying-access",
       "metadata": {},
       "source": [
        "Calcul de R21 par la méthode itérative des trapèzes:"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 46,
       "id": "fewer-partition",
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "R21=1.5049402075046334\n"
         ]
        }
       ],
       "source": [
        "R21 = R11/2 + f(x_n/2) * (x_n - x_0)/2\n",
        "print(f\"{R21=}\")"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "returning-mission",
       "metadata": {},
       "source": [
        "calcul de R22 par Richardson avec $p=2$ puisque l'erreur est dominée par $h^2$:"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 40,
       "id": "liable-forestry",
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "R22=1.7289067597046133\n"
         ]
        }
       ],
       "source": [
        "R22 = (4*R21 - R11)/3\n",
        "print(f\"{R22=}\")"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "streaming-tucson",
       "metadata": {},
       "source": [
        "Calcul de R31 par la méthode itérative des trapèzes:"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 42,
       "id": "cathedral-investigation",
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "R31=1.6574582026781939\n"
         ]
        }
       ],
       "source": [
        "h = (x_n - x_0)/2 # espace entre les nouveaux points\n",
        "x = x_0 + h/2 # position x du premier point\n",
        "R31 = R21/2 + (f(x) + f(x+h)) * h/2\n",
        "print(f\"{R31=}\")"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "sacred-pension",
       "metadata": {},
       "source": [
        "calcul de R32 par Richardson avec $p=2$ puisque l'erreur sur R21 et R31 est dominée par $h^2$:"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 43,
       "id": "compliant-zimbabwe",
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "R32=1.708297534402714\n"
         ]
        }
       ],
       "source": [
        "R32 = (4*R31 - R21)/3\n",
        "print(f\"{R32=}\")"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "elect-desktop",
       "metadata": {},
       "source": [
        "calcul de R33 par Richardson avec $p=4$ puisque l'erreur sur R22 et R32 est dominée par $h^4$:"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 44,
       "id": "floppy-alignment",
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "R33=1.706923586049254\n"
         ]
        }
       ],
       "source": [
        "R33 = (16*R32 - R22)/15\n",
        "print(f\"{R33=}\")"
       ]
      },
      {
       "cell_type": "markdown",
       "id": "surgical-syndrome",
       "metadata": {},
       "source": [
        "Même chose en utilisant les fonctions définies au cours:"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 45,
       "id": "green-league",
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "R11=0.8330405509046936\n",
          "R21=1.5049402075046334\n",
          "R22=1.7289067597046133\n",
          "R31=1.6574582026781939\n",
          "R32=1.708297534402714\n",
          "R33=1.706923586049254\n",
          "R41=1.6947487165588795\n",
          "R42=1.7071788878524412\n",
          "R43=1.7071043114157562\n",
          "R44=1.7071071800723674\n",
          "\n",
          "En utilisant la formule analytique:\n",
          "INT=1.7071067811865475\n"
         ]
        }
       ],
       "source": [
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "from math import sin, pi, sqrt\n",
        "def trapezes_rec(f, x_0, x_n, i_old, k):\n",
        "    if k == 1: \n",
        "        return (f(x_0) + f(x_n))*(x_n - x_0)/2\n",
        "    else:\n",
        "        n = 2**(k-2)\n",
        "        h = (x_n - x_0)/n\n",
        "        x = x_0 + h/2\n",
        "        sum = 0\n",
        "        for i in range(n):\n",
        "            sum += f(x)\n",
        "            x += h\n",
        "        return i_old/2 + h*sum/2\n",
        "\n",
        "    \n",
        "def richardson(i_1, i_2, k):\n",
        "    fact = 2**k\n",
        "    return (fact*i_2 - i_1)/(fact - 1)\n",
        "\n",
        "# ligne 1\n",
        "R11 = trapezes_rec(np.sin, 0, 3*pi/4, 0, 1)\n",
        "print(f\"{R11=}\")\n",
        "\n",
        "# ligne 2\n",
        "R21 = trapezes_rec(np.sin, 0, 3*pi/4, R11, 2)\n",
        "print(f\"{R21=}\")\n",
        "R22 = richardson(R11, R21, 2)\n",
        "print(f\"{R22=}\")\n",
        "\n",
        "# ligne 3\n",
        "R31 = trapezes_rec(np.sin, 0, 3*pi/4, R21, 3)\n",
        "print(f\"{R31=}\")\n",
        "R32 = richardson(R21, R31, 2)\n",
        "print(f\"{R32=}\")\n",
        "R33 = richardson(R22, R32, 4)\n",
        "print(f\"{R33=}\")\n",
        "\n",
        "# ligne 4\n",
        "R41 = trapezes_rec(np.sin, 0, 3*pi/4, R31, 4)\n",
        "print(f\"{R41=}\")\n",
        "R42 = richardson(R31, R41, 2)\n",
        "print(f\"{R42=}\")\n",
        "R43 = richardson(R32, R42, 4)\n",
        "print(f\"{R43=}\")\n",
        "R44 = richardson(R33, R43, 6)\n",
        "print(f\"{R44=}\")\n",
        "\n",
        "print(\"\\nEn utilisant la formule analytique:\")\n",
        "print('INT={}'.format(1+1/math.sqrt(2)))"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "id": "leading-montana",
       "metadata": {},
       "outputs": [],
       "source": []
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "id": "printable-neutral",
       "metadata": {},
       "outputs": [],
       "source": []
      }
     ],
     "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.8.10"
      },
      "varInspector": {
       "cols": {
        "lenName": 16,
        "lenType": 16,
        "lenVar": 40
       },
       "kernels_config": {
        "python": {
         "delete_cmd_postfix": "",
         "delete_cmd_prefix": "del ",
         "library": "var_list.py",
         "varRefreshCmd": "print(var_dic_list())"
        },
        "r": {
         "delete_cmd_postfix": ") ",
         "delete_cmd_prefix": "rm(",
         "library": "var_list.r",
         "varRefreshCmd": "cat(var_dic_list()) "
        }
       },
       "types_to_exclude": [
        "module",
        "function",
        "builtin_function_or_method",
        "instance",
        "_Feature"
       ],
       "window_display": false
      }
     },
     "nbformat": 4,
     "nbformat_minor": 5
    }