diff --git "a/Chapitre 1 - Systemes equations lineaires/1.2. Nombre de solution d'un syst\303\250me.ipynb" "b/Chapitre 1 - Systemes equations lineaires/1.2. Nombre de solution d'un syst\303\250me.ipynb" index 9ba8f71..ea33f6b 100644 --- "a/Chapitre 1 - Systemes equations lineaires/1.2. Nombre de solution d'un syst\303\250me.ipynb" +++ "b/Chapitre 1 - Systemes equations lineaires/1.2. Nombre de solution d'un syst\303\250me.ipynb" @@ -1,593 +1,575 @@ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Concept(s)-clé(s) et théorie\n", "\n", "On considère un système d'équations linéaires aux inconnues $x_1,\\ldots,x_n$\n", "\n", "$$S=\\left\\{\\begin{array}{ccccccc}\n", "a_{11}x_1 &+a_{12}x_2 & + &\\cdots &+a_{1n}x_n &= &b_1 \\\\\n", "a_{21}x_1 &+a_{22}x_2 & + &\\cdots &+a_{2n}x_n &= &b_2 \\\\\n", "\\vdots & & & &\\vdots & \\vdots &\\vdots \\\\\n", "a_{m1}x_1 &+a_{m2}x_2 & + &\\cdots &+a_{mn}x_n &= &b_m\n", "\\end{array},\\right. $$\n", "\n", "où $a_{ij},b_i\\in \\mathbb{R}$ pour tout $1\\leq i\\leq m$ et tout $1\\leq j\\leq n.$ \n", "\n", "---\n", "Un système d'équations linéaires à coefficients réels satisfait précisément une des conditions suivantes.\n", "\n", "1.Le système ne possède aucune solution.\n", "\n", "2.Le système possède une solution unique.\n", "\n", "3.Le système possède une infinité de solutions.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import Librairie.AL_Fct as al\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", - "import plotly\n", + "import plotly as py\n", "import plotly.graph_objs as go\n", "import pandas as pd\n", "import random\n", "from IPython.core.magic import register_cell_magic\n", - "from IPython.display import HTML, display" + "from IPython.display import HTML, display\n", + "from ipywidgets import interactive, HBox, VBox, widgets, interact, FloatSlider\n", + "\n", + "py.offline.init_notebook_mode(connected=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### EXEMPLE 1\n", "\n", "Nous allons travailler avec un système de $2$ inconnues, $2$ équations:\n", "\n", "$$\n", "\\begin{cases}\n", "a_{11}x_1 + a_{12}x_2=b_1\\\\\n", "a_{21}x_1 + a_{22}x_2=b_2.\n", "\\end{cases}\n", "$$\n", "\n", "On utilise *la syntaxe suivante pour entrer les coefficients du système*\n", "\n", "$$\n", "\\begin{align*}\n", "A&=\\quad [\\quad [ a_{11} , a_{12}, \\ldots, a_{1n} ], \\quad [ a_{21}, a_{22}, \\ldots, a_{2n}]\\quad, \\ldots , \\quad[a_{m1}, a_{m2}, \\ldots, a_{mn}]\\quad]\\\\\n", "b&=\\quad [\\quad b_1, b_2, \\ldots, b_m \\quad]\n", "\\end{align*}$$\n", "\n", "---\n", "Entrer le système ci-dessous - ou n'importe quel système de votre choix. Après avoir étudié les deux droites, entrer une solution du système\n", "\n", "$$\n", "\\begin{cases}\n", "-2x_1 + x_2=2\\\\\n", "2x_1 -2x_2=1.\n", "\\end{cases}\n", "$$\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "al.bgc('seashell')\n", "#Par défaut, les coefficients sont fixés à 1 et n=2, m=2\n", "\n", "A = [ [-2,1] , [2,-2]]\n", "b = [2,1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "al.printSyst(A,b)\n", "\n", "al.Plot2DSys(-7,7,15,A,b)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "al.bgc('seashell')\n", "\n", "alpha=[-2.5, -3]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "al.SolOfSyst(alpha, A,b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **EXERCICE 1**\n", "\n", "Pour chacun des cas ci-dessous, déterminer la/les solution(s) des systèmes. \n", "\n", " $$ \n", "a)\\quad \\begin{cases}\n", "\\dfrac{3}{2}x_1 - 2x_2&=0\\\\\n", "-3x_1 + 5 x_2&=3\n", "\\end{cases} \\hspace{1cm} b) \\quad \\begin{cases}\n", "x_1 - x_2&=\\phantom{-}2\\\\\n", "-3x_1 - 2x_2&=-1\\\\\n", "2x_1 + 3x_2&=\\phantom{-}4\n", "\\end{cases}\n", "$$\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "al.bgc('seashell')\n", "#Par défaut, les coefficients sont fixés à 1 \n", "#Attention d'adapter les valeurs ci-dessous pour a) et b)\n", "\n", "A = [[1, 1], [1, 1]]\n", "b =[1,1]\n", "alpha=[1,1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "al.printSyst(A,b)\n", "al.SolOfSyst(alpha, A,b)\n", "al.Plot2DSys(-7,7,15,A,b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **EXERCICE 2**\n", "\n", "Pour chacun des cas ci-dessous, déterminer la/les solution(s) des systèmes. \n", "\n", "$$ \n", "a)\\quad \\begin{cases}\n", "x_1 - x_2 + 2x_3 &=0\\\\\n", "-3x_1 &=1\\\\\n", "2x_1 + 5x_2 &=-2\n", "\\end{cases} \\hspace{1cm} b) \\quad \\begin{cases}\n", "x_1 + x_2 -2x_3 &=0\\\\\n", "3x_2 - x_3 &=1\\\\\n", "-2x_1 -2x_2 + 4x_3 &=2\n", "\\end{cases} \\hspace{1cm} c) \\quad \\begin{cases}\n", "x_1 + x_2 -2x_3 &=-1\\\\\n", "3x_2 - x_3 &=1\\\\\n", "-2x_1 -2x_2 + 4x_3 &=2\n", "\\end{cases} \\hspace{1cm}\n", "$$\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "al.bgc('seashell')\n", "#Par défaut, les coefficients sont fixés à 1 \n", "\n", "A = [[ 1, 1, 1] ,[1, 1, 1] ,[1, 1, 1]]\n", "b= [1, 1, 1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "al.printSyst(A,b)\n", "al.Plot3DSys(-10,10,100,A,b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "alpha = [1, 1, 1]\n", "al.SolOfSyst(alpha, A,b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **EXERCICE 3**\n", - "#code 1.2.Ex3\n", "\n", "À l'aide des graphes interactifs ci-dessous, trouver les valeurs des paramètres $h$ et $k$ pour que les systèmes a), b), c) et d) admettent, si possible,\n", "1. aucune solution\n", "2. une unique solution\n", "3. une infinité de solution \n", "\n", "\n", "$$ \n", "a)\\begin{cases}\n", "x_1 +5 x_2&=h\\\\\n", "-2x_1 -10 x_2&=18\n", "\\end{cases} \\hspace{1cm} b) \\begin{cases}\n", "hx_1 + x_2&=3\\\\\n", "-2x_1 - x_2&=-1\n", "\\end{cases}\\hspace{1cm} c) \\begin{cases}\n", "3x_1 + hx_2&=1\\\\\n", "x_1 + 3x_2&=h\n", "\\end{cases}\\hspace{1cm} d) \\begin{cases}\n", "kx_1 + x_2&=h\\\\\n", "3x_1 -5x_2&=2\n", "\\end{cases}\n", "$$" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import plotly as py\n", - "import numpy as np\n", - "plotly.offline.init_notebook_mode(connected=True)\n", - "import plotly.graph_objs as go\n", - "from ipywidgets import interactive, HBox, VBox, widgets, interact, FloatSlider" - ] - }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CASE a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A=[[1, 5], [-2, -10]] # we initialize the problem. The values of h and k must be fixed to one specific value here\n", "b=[0, 18]\n", "al.printSyst(A, b) # the system printed below shows the values of h and k inserted here!!\n", "\n", "data=[]\n", "x=np.linspace(-5,5,11)\n", "m=len(A)\n", "MatCoeff = [A[i]+[b[i]]for i in range(m)] #becomes augmented matrix\n", "MatCoeff=np.array(MatCoeff)\n", "\n", "for i in range(len(MatCoeff)):\n", " trace=go.Scatter(x=x, y=(MatCoeff[i,2]-MatCoeff[i,0]*x)/MatCoeff[i,1], name='a) Droite %d'%(i+1))\n", " data.append(trace)\n", "\n", "f=go.FigureWidget(data=data,\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, cof):\n", - " # MatCoeff= [[coeff, 1, cof]]\n", - " # MatCoeff=np.array(MatCoeff)\n", - " # f.data[0].y=(MatCoeff[0,2]-MatCoeff[0,0]*x)/MatCoeff[0,1]\n", + "\n", "def update_y(h):\n", " MatCoeff = [[1, 5, h]]\n", " MatCoeff=np.array(MatCoeff)\n", " f.data[0].y=(MatCoeff[0,2]-MatCoeff[0,0]*x)/MatCoeff[0,1]\n", " \n", "freq_slider = interactive(update_y, h=(-15, 15, 1/2)) \n", "vb = VBox((f, freq_slider))\n", "vb.layout.align_items = 'center'\n", "vb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CASE b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A=[[1, 1], [-2,-1]] # we initialize the problem. The value of h is fixed\n", "b=[3, -1]\n", "al.printSyst(A, b) # the system printed below shows the value of h inserted here\n", "\n", "m=len(A)\n", "MatCoeff = [A[i]+[b[i]]for i in range(m)] #becomes augmented matrix\n", "MatCoeff=np.array(MatCoeff)\n", "data=[]\n", "x=np.linspace(-5,5,11)\n", "MatCoeff=np.array(MatCoeff)\n", "for i in range(len(MatCoeff)):\n", " trace=go.Scatter(x=x, y= (MatCoeff[i,2]-MatCoeff[i,0]*x)/MatCoeff[i,1], name='b) Droite %d'%(i+1))\n", " data.append(trace)\n", " \n", "f=go.FigureWidget(data=data,\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, cof):\n", - " # MatCoeff= [[coeff, 1, cof]]\n", - " # MatCoeff=np.array(MatCoeff)\n", - " # f.data[0].y=(MatCoeff[0,2]-MatCoeff[0,0]*x)/MatCoeff[0,1]\n", - "#coefficients=[[h,1,3]]\n", + "\n", "def mat(k):\n", " h=k\n", " MatCoeff=[[h,1,3]]\n", " return MatCoeff\n", "def update_y(h):\n", " MatCoeff= mat(h)\n", " MatCoeff=np.array(MatCoeff)\n", " f.data[0].y=(MatCoeff[0,2]-MatCoeff[0,0]*x)/MatCoeff[0,1]\n", " \n", "freq_slider = interactive(update_y, h=(-10, 10, 1/2)) \n", "\n", "vb = VBox((f, freq_slider))\n", "vb.layout.align_items = 'center'\n", "vb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CASE c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A=[[3, 1], [1, 3]] # we initialize the problem. The values of h and k are fixed\n", "b=[1, 1]\n", "al.printSyst(A, b) # the system printed below shows the values of h and k inserted here\n", "\n", "m=len(A)\n", "MatCoeff = [A[i]+[b[i]]for i in range(m)] #becomes augmented matrix\n", "MatCoeff=np.array(MatCoeff)\n", "data=[]\n", "x=np.linspace(-25,25,101)\n", "MatCoeff=np.array(MatCoeff)\n", "for i in range(len(MatCoeff)):\n", " trace=go.Scatter(x=x, y= (MatCoeff[i,2]-MatCoeff[i,0]*x)/MatCoeff[i,1], name='c) Droite %d'%(i+1))\n", " data.append(trace)\n", " \n", "f=go.FigureWidget(data=data,\n", " layout=go.Layout(xaxis=dict(\n", " range=[-25, 25]\n", " ),\n", " yaxis=dict(\n", " range=[-50, 50]\n", " ) )\n", " \n", ")\n", - "#def update_y(coeff, cof):\n", - " # MatCoeff= [[coeff, 1, cof]]\n", - " # MatCoeff=np.array(MatCoeff)\n", - " # f.data[0].y=(MatCoeff[0,2]-MatCoeff[0,0]*x)/MatCoeff[0,1]\n", + "\n", "def mat(h):\n", " MatCoeff= [[h, 3, 1],[3, 1, h] ]\n", " return MatCoeff\n", "\n", "def update_y(h):\n", " MatCoeff= mat(h)\n", " MatCoeff=np.array(MatCoeff)\n", " f.data[0].y=(MatCoeff[0,2]-MatCoeff[0,0]*x)/MatCoeff[0,1]\n", " f.data[1].y=(MatCoeff[1,2]-MatCoeff[1,0]*x)/MatCoeff[1,1]\n", "\n", "freq_slider = interactive(update_y, h=(-20, 20, 1)) \n", "\n", "vb = VBox((f, freq_slider))\n", "vb.layout.align_items = 'center'\n", "vb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CASE d)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A=[[1, 1], [3, -5]] # we initialize the problem. The values of h and k are fixed\n", "b=[1, 2]\n", "al.printSyst(A, b) # the system printed below shows the values of h and k inserted here\n", "\n", "m=len(A)\n", "MatCoeff = [A[i]+[b[i]]for i in range(m)] #becomes augmented matrix\n", "MatCoeff=np.array(MatCoeff)\n", "data=[]\n", "x=np.linspace(-15,15,101)\n", "MatCoeff=np.array(MatCoeff)\n", "for i in range(len(MatCoeff)):\n", " trace=go.Scatter(x=x, y= (MatCoeff[i,2]-MatCoeff[i,0]*x)/MatCoeff[i,1], name='d) Droite %d'%(i+1))\n", " data.append(trace)\n", " \n", "f=go.FigureWidget(data=data,\n", " layout=go.Layout(xaxis=dict(\n", " range=[-15, 15]\n", " ),\n", " yaxis=dict(\n", " range=[-10, 10]\n", " ) )\n", " \n", ")\n", - "#def update_y(coeff, cof):\n", - " # MatCoeff= [[coeff, 1, cof]]\n", - " # MatCoeff=np.array(MatCoeff)\n", - " # f.data[0].y=(MatCoeff[0,2]-MatCoeff[0,0]*x)/MatCoeff[0,1]\n", + "\n", "def update_y(h, k):\n", " MatCoeff= [[k, 1, h],[2, -5, 5] ]\n", " MatCoeff=np.array(MatCoeff)\n", " f.data[0].y=(MatCoeff[0,2]-MatCoeff[0,0]*x)/MatCoeff[0,1]\n", "\n", " \n", " f.data[1].y=(MatCoeff[1,2]-MatCoeff[1,0]*x)/MatCoeff[1,1]\n", "\n", "freq_slider = interactive(update_y, h=(-10, 10, 1/10),k=(-10, 10, 1))\n", "\n", "vb = VBox((f, freq_slider))\n", "vb.layout.align_items = 'center'\n", "vb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercice 4\n", "\n", "À l'aide des graphes interactifs ci-dessous, trouver les valeurs des paramètre $h$ pour que le système admette, si possible,\n", "1. aucune solution\n", "2. une unique solution\n", "3. une infinité de solution \n", "\n", "$$ \n", "\\begin{cases}\n", "hx_1 -2 x_2 &=4\\\\\n", "-2x_1 +h x_2&=-4\\\\\n", "x_1 - x_2 &=h\n", "\\end{cases} \\hspace{1cm}\n", "$$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ + "np.seterr(divide='ignore', invalid='ignore')\n", + "\n", "A=[[1, -2], [-2, 1], [1, -1]] # we initialize the problem. The values of h and k are fixed\n", - "b=[4, -4, 2]\n", + "b=[4, -4, 0]\n", "al.printSyst(A, b) # the system printed below shows the values of h and k inserted here\n", "\n", "m=len(A)\n", "MatCoeff = [A[i]+[b[i]]for i in range(m)] #becomes augmented matrix\n", "MatCoeff=np.array(MatCoeff)\n", "data=[]\n", "x=np.linspace(-25,25,101)\n", "MatCoeff=np.array(MatCoeff)\n", "for i in range(len(MatCoeff)):\n", + " if MatCoeff[i,1] == 0:\n", + " MatCoeff[i,1] += 1e-3\n", " trace=go.Scatter(x=x, y=(MatCoeff[i,2]-MatCoeff[i,0]*x)/MatCoeff[i,1], name='c) Droite %d'%(i+1))\n", " data.append(trace)\n", " \n", "f=go.FigureWidget(data=data,\n", " layout=go.Layout(xaxis=dict(\n", " range=[-25, 25]\n", " ),\n", " yaxis=dict(\n", - " range=[-50, 50]\n", + " range=[-100, 100]\n", " ) )\n", " \n", ")\n", - "#def update_y(coeff, cof):\n", - " # MatCoeff= [[coeff, 1, cof]]\n", - " # MatCoeff=np.array(MatCoeff)\n", - " # f.data[0].y=(MatCoeff[0,2]-MatCoeff[0,0]*x)/MatCoeff[0,1]\n", + "\n", "def mat(h):\n", " MatCoeff= [[h, -2, 4], [-2, h, -4], [1, -1, h]]\n", " return MatCoeff\n", "\n", - "# Do something to avoid dividing by 0 (even if the graph still working, error is printed)\n", "def update_y(h):\n", " MatCoeff= mat(h)\n", " MatCoeff=np.array(MatCoeff)\n", + " if MatCoeff[0,1] == 0:\n", + " MatCoeff[0,1] += 1e-3\n", " f.data[0].y=(MatCoeff[0,2]-MatCoeff[0,0]*x)/MatCoeff[0,1]\n", + " if MatCoeff[1,1] == 0:\n", + " MatCoeff[1,1] += 1e-3\n", " f.data[1].y=(MatCoeff[1,2]-MatCoeff[1,0]*x)/MatCoeff[1,1]\n", + " if MatCoeff[2,1] == 0:\n", + " MatCoeff[2,1] += 1e-3\n", " f.data[2].y=(MatCoeff[2,2]-MatCoeff[2,0]*x)/MatCoeff[2,1]\n", "\n", - "freq_slider = interactive(update_y, h=(-20, 20, 1)) \n", + "freq_slider = interactive(update_y, h=(-20, 20, 0.5)) \n", "\n", "vb = VBox((f, freq_slider))\n", "vb.layout.align_items = 'center'\n", "vb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Passez au notebook du chapitre 1.3-4: Notation Matricielle](./1.3-4.%20Notation%20Matricielle.ipynb)" ] } ], "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.2" + "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }