diff --git a/Chapitre 1 - Systemes equations lineaires/1.X. Notation Matricielle.ipynb b/Chapitre 1 - Systemes equations lineaires/1.X. Notation Matricielle.ipynb index 9971149..05942dd 100644 --- a/Chapitre 1 - Systemes equations lineaires/1.X. Notation Matricielle.ipynb +++ b/Chapitre 1 - Systemes equations lineaires/1.X. Notation Matricielle.ipynb @@ -1,361 +1,715 @@ { "cells": [ { "cell_type": "markdown", "metadata": {}, - "source": [] + "source": [ + "TEXT FOR MATRICES" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "8ce7c16dd2fd4cd199c4bfd7549b142c", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(Text(value='Ligne i', description='i'), Text(value='Ligne j', description='j'), Output()…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import AL_Fct as al\n", + "import numpy as np\n", + "from ipywidgets import interact , widgets, fixed\n", + "\n", + "Mat=[[2,2],[-2,3], [1,0]]\n", + "b=[3,2,1]\n", + "M=[Mat[i]+[b[i]] for i in range(0,len(Mat))]\n", + "M=np.array(M)\n", + "@interact(i='Ligne i', j='Ligne j', M=fixed(M))\n", + "def Eij(M, i,j): #matrice elementaire, echange la ligne i avec la ligne j\n", + " M[[i,j],:]=M[[j,i],:]\n", + " return M" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{ccc} 1 & 1 & 1.5 \\\\0 & 5 & 5 \\\\0 & -1 & -0.5 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{ccc} 1 & 1 & 1.5 \\\\0 & 1 & 1 \\\\0 & 0 & 0.5 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La matrice est sous la forme échelonnée\n" + ] + }, + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{ccc} 2 & 2 & 3 \\\\-2 & 3 & 2 \\\\1 & 0 & 1 \\end{array}\\right)\\quad \\sim \\quad\\left(\\begin{array}{ccc} 1 & 1 & 1.5 \\\\0 & 1 & 1 \\\\0 & 0 & 0.5 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n", + "3 3\n", + "i= 2\n", + "here\n", + "i= 1 j= 1 Mat(i,:) [0. 1. 1.]\n", + "k= 0\n" + ] + }, + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{ccc} 1 & 0 & 0.5 \\\\0 & 1 & 1 \\\\0 & 0 & 0.5 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La matrice est sous la forme échelonnée réduite\n" + ] + }, + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{cc} 2 & 2 \\\\-2 & 3 \\\\1 & 0 \\end{array}\\right)\\quad \\sim \\quad\\left(\\begin{array}{ccc} 1 & 0 & 0.5 \\\\0 & 1 & 1 \\\\0 & 0 & 0.5 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import AL_Fct as al\n", + "import numpy as np\n", + "Mat=[[2,2],[-2,3], [1,0]]\n", + "b=[3,2,1]\n", + "\n", + "Mat=al.echelonMat(Mat, b) #attention différence entre ech [A | b] ou [A]\n", + "Mat=np.array(Mat)\n", + "print(Mat[3:len(Mat),:])\n", + "Mat[3:len(Mat),:]=0\n", + "print(len(Mat), len(Mat[1,:]))\n", + "i=(len(Mat)-1)\n", + "while i>=1:\n", + " print('i=',i)\n", + " while all(abs(Mat[i,:len(Mat[0])-1])<1e-15) and i!=0:#if ligne (or rest of) is zero, take next ligne\n", + " i-=1\n", + " print('here')\n", + " #we have a lign with one non-nul element\n", + " j=i #we can start at pos ij at least the pivot is there\n", + " if abs(Mat[i,j])<1e-15: #if element Aij=0 take next one --> find pivot\n", + " j+=1\n", + " #Aij!=0 and Aij==1 if echelonMat worked\n", + " print('i=',i,'j=',j, 'Mat(i,:)', Mat[i,:]) \n", + " for k in range(i): #put zeros above pivot (which is 1 now)\n", + " print('k=',k)\n", + " Mat=al.Eijalpha(Mat, k,i, -Mat[k,j])\n", + " i-=1\n", + " al.printA(Mat)\n", + "\n", + "print(\"La matrice est sous la forme échelonnée réduite\")\n", + "al.printEquMatrices([MatCoeff, Mat])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{ccc} 1 & -1.5 & -1 \\\\0 & 1.5 & 2 \\\\0 & 2 & 3 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{ccc} 1 & -1.5 & -1 \\\\0 & 1 & 1.333 \\\\0 & 0 & 0.333 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La matrice est sous la forme échelonnée\n" + ] + }, + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{ccc} 0 & 2 & 3 \\\\-2 & 3 & 2 \\\\1 & 0 & 1 \\end{array}\\right)\\quad \\sim \\quad\\left(\\begin{array}{ccc} 1 & -1.5 & -1 \\\\0 & 1 & 1.333 \\\\0 & 0 & 0.333 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{ccc} 1 & 0 & 1 \\\\0 & 1 & 1.333 \\\\0 & 0 & 0.333 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La matrice est sous la forme échelonnée réduite\n" + ] + }, + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{ccc} 0 & 2 & 3 \\\\-2 & 3 & 2 \\\\1 & 0 & 1 \\end{array}\\right)\\quad \\sim \\quad\\left(\\begin{array}{ccc} 1 & 0 & 1 \\\\0 & 1 & 1.333 \\\\0 & 0 & 0.333 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import AL_Fct as al\n", + "import numpy as np\n", + "Mat=[[0,2],[-2,3], [1,0]]\n", + "b=[3,2,1]\n", + "\n", + "rrefM=al.echelonRedMat(Mat, b)" + ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import AL_Fct as al\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "from IPython.core.magic import register_cell_magic\n", "\n", "@register_cell_magic\n", "def background(color, cell):\n", " set_background(color)\n", "from IPython.display import HTML, display\n", "\n", "def set_background(color): \n", " script = (\n", " \"var cell = this.closest('.jp-CodeCell');\"\n", " \"var editor = cell.querySelector('.jp-Editor');\"\n", " \"editor.style.background='{}';\"\n", " \"this.parentNode.removeChild(this)\"\n", " ).format(color)\n", "\n", " display(HTML(''.format(script)))\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "EXERCICE 1:\n", + "\n", + "Parmi les systèmes ci-dessous, lesquels ont le même ensemble de solutions?\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Votre système est de la forme\n" + ] + }, + { + "data": { + "text/latex": [ + "$\\begin{cases}3x_1 + 3x_2 + -1x_3=15\\\\1x_1 + 3x_2 + -2x_3=8\\\\5x_1 + 3x_2 + 1x_3=21\\\\\\end{cases}$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La matrice correspondante est\n" + ] + }, + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{cccc} 3 & 3 & -1 & 15 \\\\1 & 3 & -2 & 8 \\\\5 & 3 & 1 & 21 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{cccc} 1 & 1 & -0.333 & 5 \\\\0 & 2 & -1.667 & 3 \\\\0 & -2 & 2.667 & -4 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{cccc} 1 & 1 & -0.333 & 5 \\\\0 & 1 & -0.833 & 1.5 \\\\0 & 0 & 1.0 & -1 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/latex": [ + "$\\left(\\begin{array}{cccc} 1 & 1 & -0.333 & 5 \\\\0 & 1 & -0.833 & 1.5 \\\\0 & 0 & 1 & -1.0 \\end{array}\\right)$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "MatCoeff=[[3,3,-1],[1,3,-2],[5,3,1]]\n", + "b=[15,8,21]\n", + "m=3\n", + "n=3\n", + "print(\"Votre système est de la forme\")\n", + "al.printSyst(m,n,MatCoeff,b)\n", + "\n", + "print(\"La matrice correspondante est\")\n", + "al.printA(MatCoeff)\n", + "\n", + "al.echelonMat(MatCoeff)" + ] + }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Votre système est de la forme\n" ] }, { "data": { "text/latex": [ "$\\begin{cases}a_{11}x_1 + a_{12}x_2 = b_1\\\\a_{21}x_1 + a_{22}x_2 = b_2\\\\a_{31}x_1 + a_{32}x_2 = b_3\\\\\\end{cases}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "3 3\n", "True\n", "Votre système est de la forme\n" ] }, { "data": { "text/latex": [ "$\\begin{cases}1x_1 + 2x_2=3\\\\-2x_1 + 3x_2=3\\\\1x_1 + 0x_2=2\\\\\\end{cases}$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "La matrice correspondante est\n" ] }, { "data": { "text/latex": [ "$\\left(\\begin{array}{ccc} 1 & 2 & 3 \\\\-2 & 3 & 3 \\\\1 & 0 & 2 \\end{array}\\right)$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$\\left(\\begin{array}{ccc} 1 & 2 & 3 \\\\0 & 7 & 9 \\\\0 & -2 & -1 \\end{array}\\right)$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$\\left(\\begin{array}{ccc} 1 & 2 & 3 \\\\0 & 1 & 1.286 \\\\0 & 0 & 1.571 \\end{array}\\right)$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "La matrice est sous la forme échelonnée\n" ] }, { "data": { "text/latex": [ "$\\left(\\begin{array}{ccc} 1 & 2 & 3 \\\\-2 & 3 & 3 \\\\1 & 0 & 2 \\end{array}\\right)\\quad \\sim \\quad\\left(\\begin{array}{ccc} 1 & 2 & 3 \\\\0 & 1 & 1.286 \\\\0 & 0 & 1.571 \\end{array}\\right)$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "n=2\n", "m=3\n", "\n", "print(\"Votre système est de la forme\")\n", "al.printSyst(m,n, '')\n", "\n", "MatCoeff=[[1,2,3],[-2,3,3], [1,0,2]]\n", - "print(len(MatCoeff[:]), len(MatCoeff))\n", - "print(len(MatCoeff[1])==n+1 and len(MatCoeff)==m)\n", + "\n", "print(\"Votre système est de la forme\")\n", "al.printSyst(m,n,MatCoeff)\n", "\n", "print(\"La matrice correspondante est\")\n", "al.printA(MatCoeff)\n", "\n", "al.echelonMat(MatCoeff)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "ename": "NameError", "evalue": "name 'go' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlinspace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mMatCoeff\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mMatCoeff\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m f=go.FigureWidget(data=[go.Scatter(x=x, y= (MatCoeff[0,2]-MatCoeff[0,0]*x)/MatCoeff[0,1])],\n\u001b[0m\u001b[1;32m 15\u001b[0m layout=go.Layout(xaxis=dict(\n\u001b[1;32m 16\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name 'go' is not defined" ] } ], "source": [ "import plotly\n", "import plotly.plotly as py\n", "import numpy as np\n", "plotly.offline.init_notebook_mode(connected=True)\n", "\n", "\n", "from ipywidgets import interactive, HBox, VBox, widgets, interact\n", "\n", "\n", "MatCoeff = [ [1,-2,-1] ]\n", "\n", "x=np.linspace(-5,5,10)\n", "MatCoeff=np.array(MatCoeff)\n", "f=go.FigureWidget(data=[go.Scatter(x=x, y= (MatCoeff[0,2]-MatCoeff[0,0]*x)/MatCoeff[0,1])],\n", " layout=go.Layout(xaxis=dict(\n", " range=[-5, 5]\n", " ),\n", " yaxis=dict(\n", " range=[-10, 10]\n", " ) )\n", " \n", ")\n", "def update_y(coeff):\n", " f.data[0].y=(MatCoeff[0,2]-coeff*x)/MatCoeff[0,1]\n", "\n", "freq_slider = interactive(update_y, coeff=(-10, 10, 1))\n", "\n", "vb = VBox((f, freq_slider))\n", "vb.layout.align_items = 'center'\n", "vb" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "\n", "\n", "\n", "\n", "\n", "%%background honeydew" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }