{ "cells": [ { "cell_type": "markdown", "id": "f6fd6083-f549-46d6-b1ce-484ab7dbd226", "metadata": {}, "source": [ "## MT09 - TP2 Préparation - Automne 2024\n", "### Elimination de Gauss et factorisation LU\n", "\n", "1. Ecrire une fonction python \n", "```\n", "x = solsup(U,b)\n", "```\n", "qui, étant donné $U$ une matrice triangulaire supérieure de taille $n\\times n$ et $b$ vecteur colonne de taille $n$, calcule $x$ solution de $Ux=b$." ] }, { "cell_type": "code", "execution_count": null, "id": "b770ac94-6d48-4363-a9d6-8648dd15ca61", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "def solsup(U, b):\n", " \"\"\" Résolution du système triangulaire supérieur Ux=b \"\"\"\n", " # A COMPLETER\n", " n = np.size(b);\n", " x = np.zeros((n,1));\n", " # ...\n", " return x" ] }, { "cell_type": "markdown", "id": "b06b87e7-515f-4629-a32d-6382db063829", "metadata": {}, "source": [ "Application : utiliser la fonction `solsup()` pour déterminer la solution $x$ de \n", "\n", "$$\n", "\\begin{pmatrix} 1 & 2 & 3 \\\\ 0 & 4 & 8 \\\\ 0 & 0 & 5 \\end{pmatrix} x = \\begin{pmatrix} 6 \\\\ 16 \\\\ 15 \\end{pmatrix}.\n", "$$\n", "\n", "Vérifiez que $Ux-b=0$. Comparer aussi à la solution donnée par ```solve(U,b)``` du module\n", "```numpy.linalg```." ] }, { "cell_type": "code", "execution_count": null, "id": "9feaa0be-921a-4398-8272-8fdac75cf4a6", "metadata": { "tags": [] }, "outputs": [], "source": [ "U = np.array([[1, 2, 3], [0, 4, 8], [0, 0, 5]], dtype=np.float64);\n", "b = np.array([[6, 16, 15]], dtype=np.float64).T;\n", "x = solsup(U, b);\n", "print('x = ', x)\n", "# Verif\n", "print(U@x, b)\n", "print(\"Ux-b = \", U@x - b)\n", "import numpy.linalg as la\n", "xverif = la.solve(U,b)\n", "print(\"xverif = \", xverif)" ] }, { "cell_type": "markdown", "id": "e6031d8a-afbd-4f15-9d29-dd4f8de2a167", "metadata": {}, "source": [ "2. Ecrire une fonction python\n", "```\n", "U, e = trisup(A,b)\n", "```\n", "qui, étant donné $A$ matrice carrée de taille $n\\times n$ et $b$ vecteur colonne de taille $n$, déterminer par la méthode d'élimination de Gauss, quand c'est possible, la matrice $U$ triangulaire supérieure et le vecteur colonne $e$ tel que $Ax=b$\n", "$\\Leftrightarrow$ $Ux = e$.\n", "\n", "On rappelle l'algorithme d'élimination de Gauss:" ] }, { "cell_type": "markdown", "id": "04b59759-9488-4db8-a379-c4d6dee06077", "metadata": {}, "source": [ "#### Algorithme de Gauss" ] }, { "cell_type": "code", "execution_count": null, "id": "d8d803a1-5b37-4915-a875-60ba4a438015", "metadata": {}, "outputs": [], "source": [ "import sys\n", "def trisup(A, b):\n", "# Méthode d'elimination de Gauss\n", " Am = np.copy(A);\n", " bm = np.copy(b) \n", " n = np.size(b);\n", " for k in np.arange(0,n):\n", " if # A COMPLETER:\n", " print(\"ERROR : Pivot nul, k=\", k, '\\n');\n", " sys.exit(1)\n", " for i in np.arange(k+1, n):\n", " # A COMPLETER\n", " if # A COMPLETER\n", " print(\"ERROR : Pivot A_nn nul\\n\");\n", " sys.exit(1);\n", " return Am, bm" ] }, { "cell_type": "markdown", "id": "5309d9f0-d2ca-4d76-962a-69105d902acd", "metadata": {}, "source": [ "Application : utiliser la fonction `trisup()` pour déterminer un système d'équations triangulaire $Ux=e$ équivalent au système $Ax=b$ suivant :\n", "\n", "$$\n", "\\begin{pmatrix} 3 & 1 & 2 \\\\ 3 & 2 & 6 \\\\ 6 & 1 & -1 \\end{pmatrix} x = \\begin{pmatrix} 2 \\\\ 1 \\\\ 4 \\end{pmatrix}.\n", "$$\n", "\n", "Affichez $U$ et $e$. Utilisez ```trisup()``` pour résoudre $Ux=e$.\n", "Affichez la solution $x$ trouvée.\n", "Vérifiez que $Ux-e = 0$ et $Ax-b=0$." ] } ], "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.11.5" }, "toc-autonumbering": false, "toc-showcode": true, "toc-showmarkdowntxt": false }, "nbformat": 4, "nbformat_minor": 5 }