+# We test if a key already exists. If yes, then it will just display it. If no, then it will first create the key and then display it.
+if [ -r /home/.ssh/id_rsa.pub ]; then echo "Your SSH public key file is: /home/.ssh/id_rsa.pub"; echo "Here is the public key you need to export to your remote git server:"; show_ssh_key; else echo "It looks like you don't have an SSH key pair yet. Creating one for you:"; mkdir -p /home/.ssh; ssh-keygen -q -b 2048 -t rsa -N '' -f /home/.ssh/id_rsa; show_ssh_key; fi
+# Display the name and email address as they are now set up
+git config --global user.name
+git config --global user.email
+# Display the name and email address as they are now set up
+git config --global user.name
+git config --global user.email
+# Set the name and email address on git /!\ CHANGE THE VALUES in red BEFORE EXECUTING
+git config --global user.name "CHANGE Your Name Here"
+ "**Cours de Mathématiques Spéciales** (semestre de printemps 2021)<br>\n",
+ "*Roger Sauser*"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "### Le but de cette séance d'exercices est de vous permettre de consolider vos connaissances concernant la méthode de Picard et de vous familiariser avec trois méthodes de point fixe : \n",
+ "* la **méthode de Newton** (aussi appelée **méthode de Newton-Raphson** ou **de la tangente**) ;\n",
+ "* la **méthode de la corde** (aussi appelée **méthode de Newton-corde**) ;\n",
+ "* la **méthode de la parallèle**. \n",
+ "\n",
+ "### Nous reviendrons également sur une autre méthode itérative : la méthode dite de la sécante."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "deletable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "outputs": [],
+ "source": [
+ "# nous allons utiliser numpy et matplotlib :\n",
+ "import numpy as np\n",
+ "import matplotlib.pyplot as plt"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "# Exercices de révision (méthode de Picard)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 1\n",
+ "Dans le TP précédent, nous avons constaté que les fonctions \n",
+ "sont $K$-contractantes sur $\\mathcal{R}$ dans le cas de $f(x)$ et sur, par exemple, \n",
+ "$I_\\pi=\\displaystyle\\left]-\\frac{\\pi}{2},\\frac{\\pi}{2}\\right[$ dans le cas de $g(x)$.\n",
+ "\n",
+ "Dans cet exercice, on vous propose de :\n",
+ "\n",
+ "a) vérifier le théorème du point fixe de Banach dans le cas particulier de la fonction $f$ en :\n",
+ " * représentant graphiquement la fonction $f$ ;\n",
+ " * vérifiant l'existence d'un unique point fixe ;\n",
+ " * trouvant une valeur approchée à 5 décimales de ce point fixe grâce à la méthode de Picard.\n",
+ "\n",
+ "b) vérifier de manière analogue le théorème 10 du polycopié dans le cas particulier de la fonction $g$ sur $I_\\pi$. Ce théorème s'applique-t'il dans le cas de cette fonction sur l'intervalle $\\displaystyle\\left]-\\frac{\\pi}{2},0\\right[$ ?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# a)\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# b)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 2\n",
+ "\n",
+ "Soit $g(x)$ une fonction **de $\\mathcal{R}$ dans $\\mathcal{R}$** telle que $g^3$ est **$K$-contractante**. \n",
+ "Montrer que $g$ possède un et un seul point fixe.\n",
+ "\n",
+ "Illustrer ce résultat en représentant graphiquement la fonction $g^3$ pour le cas particulier où $g(x)=0.5x+3$.\n",
+ "\n",
+ "Attention : la notation $g^3$ signifie ici que la fonction $g^3$ est obtenue par **composition répétée** de la fonction $g$ avec elle-même trois fois."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "# Exercices sur la méthode de Newton"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 3\n",
+ "Ci-dessous, à l'exercice 4, nous allons implémenter la méthode de Newton en supposant connue la dérivée de la fonction dont on cherche le zéro. Toutefois, il pourrait être commode (si l'on manque de temps ou d'aisance pour déterminer la dérivée \"à la main\") d'obtenir une estimation numérique de la dérivée.\n",
+ "\n",
+ "Numériquement, il est possible d'approximer la valeur du nombre dérivée d'une fonction en un point en calculant la pente de la courbe en ce point avec un des deux quotients suivants :\n",
+ "$$\n",
+ "\\frac{f(a+h)-f(a)}{h}~~~~\n",
+ "\\hbox{ou}~~~~\n",
+ "\\frac{f(a)-f(a-h)}{h}\\,.\n",
+ "$$\n",
+ "\n",
+ "Souvent, on privilégie la moyenne de ces deux formules et on écrit :\n",
+ "\n",
+ "$$\n",
+ "f'(a) \\cong \\frac{f(a+h)-f(a-h)}{2h}\\,.\n",
+ "$$\n",
+ "\n",
+ "Dans cet exercice, on vous propose d'écrire une fonction `derivee` ayant trois arguments : une fonction $f$ à dériver, l'abscisse $a$ où la dérivée doit être approximée et la valeur du pas $h$ (on posera $h=0.005$ comme valeur par défaut). On vous demande de vérifier votre implémentation en représentant les fonction $f_1(x)=\\sin{(x)}$ et $f_2(x)=(x+1)^2(x-\\frac{3}{2})$ accompagnées de leur dérivée respective dans l'intervalle $[-2,2]$. Calculez l'erreur commise en utilisant votre fonction pour évaluer la dérivée de $f_1$ en $x=1.1$.\n",
+ "\n",
+ "Remarque : dans l'exercice 2 du TP14, nous avons calculé la vitesse et l'accélération d'un objet à partir d'un ensemble de mesures expérimentales des positions de cet objet."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "scrolled": false
+ },
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 4\n",
+ "Dans cet exercice, on se propose de déterminer numériquement les zéros de la fonction \n",
+ "$f_2(x)=(x+1)^2(x-\\frac{3}{2})$ définie à l'exercice 3. \n",
+ "\n",
+ "Pour ce faire, nous allons implémenter la **méthode de Newton** en définissant une fonction `resoudre_par_Newton`$\\dots$\n",
+ "* $\\dots$ dont les arguments sont la fonction `f` étudiée, le point de départ `x_0` de la méthode, la fonction dérivée `fprime`, la tolérance `eps` et le nombre maximum d'itérations autorisées `n_max` ;\n",
+ "* $\\dots$ qui calcule par itérations une approximation $\\bar x$ d'un zéro de $f$ ;\n",
+ "* $\\dots$ qui s'arrête :\n",
+ " - soit quand la valeur absolue de $f(\\bar x)$ est inférieure à la tolérance ; \n",
+ " - soit quand le nombre maximum d'itérations est atteint ; \n",
+ " - si la valeur de la dérivée utilisée à chaque itération est inférieure à `1e-10` pour éviter tout problème de divergence dans la formule utilisée ;\n",
+ "* $\\dots$ qui retourne une valeur approchée d'un zéro de $f$, ainsi qu'une liste de tuples, ces tuples contenant les couples $(x_n,f(x_n))$ utilisés à chaque itération $n$. \n",
+ "\n",
+ "Tester votre implémentation en recherchant les deux zéros de la fonction $f_2$. Expérimentez l'efficacité de la méthode selon le point de départ $x_0$ choisi et selon la nature du zéro considéré. Constatez-vous des différences notables ? Si oui, essayez d'en expliquer la raison.\n",
+ "\n",
+ "Vérifier que la méthode de Newton est une **méthode de point fixe** en affichant la différence\n",
+ "$$\n",
+ "x_{n} - x_{n-1}\n",
+ "$$\n",
+ "pour chacune des itérations. Quelles conclusions peut-on tirer de la comparaison de cette différence à la dernière itération avec la tolérance (sur $f(\\bar x)$) que nous avons considérée ?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "scrolled": false
+ },
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "# Exercice sur les méthodes de la corde, de la parallèle et de la sécante (comparaison avec la méthode de la bissection et la méthode de Newton)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 5\n",
+ "Dans cet exercice, nous allons revenir sur la fonction $f_2(x)=(x+1)^2(x-\\frac{3}{2})$ déjà étudiée plus haut. En particulier, on vous demande :\n",
+ "* d'implémenter les méthodes **de la corde** (**Newton-corde**), **de la parallèle** (en donnant la valeur de $\\lambda$ en paramètre), **de la sécante** et **de la bissection** en vous inspirant de ce qui a été fait à l'exercice 4 ;\n",
+ "* de comparer le nombre d'itérations nécessaires à chacune des méthodes pour arriver à approximer les deux zéros de $f_2$ en assurant une **précision de 10 décimales** et en partant d'un **même $x_0$** pour chaque méthode (vous pouvez par exemple démarrer à une distance 0.5 d'un zéro) ;\n",
+ "* quelles conclusions pouvez-vous tirer de vos expérimentations ?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "scrolled": false
+ },
+ "outputs": [],
+ "source": [
+ "# méthode de Newton-corde\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "scrolled": false
+ },
+ "outputs": [],
+ "source": [
+ "# méthode de la parallèle\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# méthode de la sécante\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# méthode de la bissection\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "celltoolbar": "Format de la Cellule Texte Brut",
+ "**Cours de Mathématiques Spéciales** (semestre de printemps 2021)<br>\n",
+ "*Roger Sauser*"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "### Le but de cette séance d'exercices est de vous permettre de consolider vos connaissances concernant la méthode de Picard et de vous familiariser avec trois méthodes de point fixe : \n",
+ "* la **méthode de Newton** (aussi appelée **méthode de Newton-Raphson** ou **de la tangente**) ;\n",
+ "* la **méthode de la corde** (aussi appelée **méthode de Newton-corde**) ;\n",
+ "* la **méthode de la parallèle**. \n",
+ "\n",
+ "### Nous reviendrons également sur une autre méthode itérative : la méthode dite de la sécante."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "deletable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "outputs": [],
+ "source": [
+ "# nous allons utiliser numpy et matplotlib :\n",
+ "import numpy as np\n",
+ "import matplotlib.pyplot as plt"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "# Exercices de révision (méthode de Picard)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 1\n",
+ "Dans le TP précédent, nous avons constaté que les fonctions \n",
+ "sont $K$-contractantes sur $\\mathcal{R}$ dans le cas de $f(x)$ et sur, par exemple, \n",
+ "$I_\\pi=\\displaystyle\\left]-\\frac{\\pi}{2},\\frac{\\pi}{2}\\right[$ dans le cas de $g(x)$.\n",
+ "\n",
+ "Dans cet exercice, on vous propose de :\n",
+ "\n",
+ "a) vérifier le théorème du point fixe de Banach dans le cas particulier de la fonction $f$ en :\n",
+ " * représentant graphiquement la fonction $f$ ;\n",
+ " * vérifiant l'existence d'un unique point fixe ;\n",
+ " * trouvant une valeur approchée à 5 décimales de ce point fixe grâce à la méthode de Picard.\n",
+ "\n",
+ "b) vérifier de manière analogue le théorème 10 du polycopié dans le cas particulier de la fonction $g$ sur $I_\\pi$. Ce théorème s'applique-t'il dans le cas de cette fonction sur l'intervalle $\\displaystyle\\left]-\\frac{\\pi}{2},0\\right[$ ?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# a)\n",
+ "# définition et représentation graphique de la fonction f\n",
+ "print('Point fixe trouvé par la méthode de Picard : x =', resultat[0])\n",
+ "print(\"Le nombre d'itérations effectuées est : n =\", resultat[1])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "La fonction $g$ est $K$-contractante sur $I=\\displaystyle\\left]-\\frac{\\pi}{2},0\\right[$, mais $\\exists x \\in I$ tel que $g(x)\\not\\in I$. Toutes les hypothèses du théorème ne sont donc pas satisfaites."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 2\n",
+ "\n",
+ "Soit $g(x)$ une fonction **de $\\mathcal{R}$ dans $\\mathcal{R}$** telle que $g^3$ est **$K$-contractante**. \n",
+ "Montrer que $g$ possède un et un seul point fixe.\n",
+ "\n",
+ "Illustrer ce résultat en représentant graphiquement la fonction $g^3$ pour le cas particulier où $g(x)=0.5x+3$.\n",
+ "\n",
+ "Attention : la notation $g^3$ signifie ici que la fonction $g^3$ est obtenue par **composition répétée** de la fonction $g$ avec elle-même trois fois."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "Par hypothèse, $g^3$ est $K$-contractante sur $\\mathcal{R}$. Par le théorème de Banach, $g^3$ possède donc un unique point fixe que nous allons noter $\\bar x$ (autrement dit, $\\bar x$ est tel que $g^3(\\bar x) = \\bar x$).\n",
+ "\n",
+ "Nous allons commencer par montrer que $g$ possède un point fixe. Notons $g(\\bar x)\\equiv y$. \n",
+ "Nous constatons que $y$ est un point fixe de $g^3$. Par unicité du point fixe de $g^3$, on a donc que $y=\\bar x$, autrement dit $g(\\bar x)=\\bar x$. Nous concluons à l'**existence** d'un point fixe pour la fonction $g$.\n",
+ "\n",
+ "On se convainc que ce point est unique en observant que tout point fixe de $g$ est également un point fixe de $g^3$ (point fixe qui est unique) et est donc égal à $\\bar x$."
+ "Ci-dessous, à l'exercice 4, nous allons implémenter la méthode de Newton en supposant connue la dérivée de la fonction dont on cherche le zéro. Toutefois, il pourrait être commode (si l'on manque de temps ou d'aisance pour déterminer la dérivée \"à la main\") d'obtenir une estimation numérique de la dérivée.\n",
+ "\n",
+ "Numériquement, il est possible d'approximer la valeur du nombre dérivée d'une fonction en un point en calculant la pente de la courbe en ce point avec un des deux quotients suivants :\n",
+ "$$\n",
+ "\\frac{f(a+h)-f(a)}{h}~~~~\n",
+ "\\hbox{ou}~~~~\n",
+ "\\frac{f(a)-f(a-h)}{h}\\,.\n",
+ "$$\n",
+ "\n",
+ "Souvent, on privilégie la moyenne de ces deux formules et on écrit :\n",
+ "\n",
+ "$$\n",
+ "f'(a) \\cong \\frac{f(a+h)-f(a-h)}{2h}\\,.\n",
+ "$$\n",
+ "\n",
+ "Dans cet exercice, on vous propose d'écrire une fonction `derivee` ayant trois arguments : une fonction $f$ à dériver, l'abscisse $a$ où la dérivée doit être approximée et la valeur du pas $h$ (on posera $h=0.005$ comme valeur par défaut). On vous demande de vérifier votre implémentation en représentant les fonction $f_1(x)=\\sin{(x)}$ et $f_2(x)=(x+1)^2(x-\\frac{3}{2})$ accompagnées de leur dérivée respective dans l'intervalle $[-2,2]$. Calculez l'erreur commise en utilisant votre fonction pour évaluer la dérivée de $f_1$ en $x=1.1$.\n",
+ "\n",
+ "Remarque : dans l'exercice 2 du TP14, nous avons calculé la vitesse et l'accélération d'un objet à partir d'un ensemble de mesures expérimentales des positions de cet objet."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "scrolled": false
+ },
+ "outputs": [],
+ "source": [
+ "def f_1(x):\n",
+ " return np.sin(x)\n",
+ "def f_2(x):\n",
+ " return (x+1)**2*(x-1.5)\n",
+ "\n",
+ "def derivee(f,a,h=0.005):\n",
+ " '''\n",
+ " Résumé : Cette fonction permet de trouver approximation de f'(a) avec une valeur donnée de h.\n",
+ " Paramètres : \n",
+ " f : la fonction pour laquelle nous cherchons à trouver une approximation de f'(a)\n",
+ " a : un nombre \n",
+ " h : un nombre (pas intervenant dans le calcul de la différence)\n",
+ " Retourne : \n",
+ " fprimea : un nombre correspondant à la formule de différence centrée : (f(a+h) - f(a-h))/(2h)\n",
+ "# estimation de l'erreur commise (avec le pas par défaut h=0.005)\n",
+ "print(derivee(f_1,1.1)-np.cos(1.1))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 4\n",
+ "Dans cet exercice, on se propose de déterminer numériquement les zéros de la fonction \n",
+ "$f_2(x)=(x+1)^2(x-\\frac{3}{2})$ définie à l'exercice 3. \n",
+ "\n",
+ "Pour ce faire, nous allons implémenter la **méthode de Newton** en définissant une fonction `resoudre_par_Newton`$\\dots$\n",
+ "* $\\dots$ dont les arguments sont la fonction `f` étudiée, le point de départ `x_0` de la méthode, la fonction dérivée `fprime`, la tolérance `eps` et le nombre maximum d'itérations autorisées `n_max` ;\n",
+ "* $\\dots$ qui calcule par itérations une approximation $\\bar x$ d'un zéro de $f$ ;\n",
+ "* $\\dots$ qui s'arrête :\n",
+ " - soit quand la valeur absolue de $f(\\bar x)$ est inférieure à la tolérance ; \n",
+ " - soit quand le nombre maximum d'itérations est atteint ; \n",
+ " - si la valeur de la dérivée utilisée à chaque itération est inférieure à `1e-10` pour éviter tout problème de divergence dans la formule utilisée ;\n",
+ "* $\\dots$ qui retourne une valeur approchée d'un zéro de $f$, ainsi qu'une liste de tuples, ces tuples contenant les couples $(x_n,f(x_n))$ utilisés à chaque itération $n$. \n",
+ "\n",
+ "Tester votre implémentation en recherchant les deux zéros de la fonction $f_2$. Expérimentez l'efficacité de la méthode selon le point de départ $x_0$ choisi et selon la nature du zéro considéré. Constatez-vous des différences notables ? Si oui, essayez d'en expliquer la raison.\n",
+ "\n",
+ "Vérifier que la méthode de Newton est une **méthode de point fixe** en affichant la différence\n",
+ "$$\n",
+ "x_{n} - x_{n-1}\n",
+ "$$\n",
+ "pour chacune des itérations. Quelles conclusions peut-on tirer de la comparaison de cette différence à la dernière itération avec la tolérance (sur $f(\\bar x)$) que nous avons considérée ?"
+ "Le dernier paragraphe de ce notebook fournit une comparaison des différentes méthodes et revient sur les différences observées dans la convergence selon la nature du zéro considéré. La méthode de Newton converge plus rapidement dans le cas d'un zéro simple.\n",
+ "\n",
+ "D'autre part, on constate qu'une condition d'arrêt basée sur la distance entre deux approximations successives est particulièrement indiquée dans le cas d'un zéro simple. "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "# Exercice sur les méthodes de la corde, de la parallèle et de la sécante (comparaison avec la méthode de la bissection et la méthode de Newton)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 5\n",
+ "Dans cet exercice, nous allons revenir sur la fonction $f_2(x)=(x+1)^2(x-\\frac{3}{2})$ déjà étudiée plus haut. En particulier, on vous demande :\n",
+ "* d'implémenter les méthodes **de la corde** (**Newton-corde**), **de la parallèle** (en donnant la valeur de $\\lambda$ en paramètre), **de la sécante** et **de la bissection** en vous inspirant de ce qui a été fait à l'exercice 4 ;\n",
+ "* de comparer le nombre d'itérations nécessaires à chacune des méthodes pour arriver à approximer les deux zéros de $f_2$ en assurant une **précision de 10 décimales** et en partant d'un **même $x_0$** pour chaque méthode (vous pouvez par exemple démarrer à une distance 0.5 d'un zéro) ;\n",
+ "* quelles conclusions pouvez-vous tirer de vos expérimentations ?"
+ "print('Le zéro (double) de f_2(x) obtenu (après %g itérations, en partant de x_0 = %g et avec une valeur lambda = %g) est x = %g .' % (len(evolution)-1,x_0,Lambda,x))\n",
+ "print('Le zéro (simple) de f_2(x) obtenu (après %g itérations, en partant de x_0 = %g et avec une valeur lambda = %g) est x = %g .' % (len(evolution)-1,x_0,Lambda,x))\n",
+ "print('Le zéro (double) de f_2(x) obtenu (après %g itérations, en partant de x_0 = %g et x_1 = %g) est x = %g .' % (len(evolution)-1,x_0,x_1,x))\n",
+ "print('Le zéro (simple) de f_2(x) obtenu (après %g itérations, en partant de x_0 = %g et x_1 = %g) est x = %g .' % (len(evolution)-1,x_0,x_1,x))\n",
+ "Le tableau ci-dessous résume les résultats que nous avons obtenus en précisant le nombre d'itérations nécessaires pour chacune des méthodes. La **méthode de Newton** se montre particulièrement efficace, alors que la **méthode de la parallèle** est très sensible au choix de la pente `lambda`.\n",
+ "\n",
+ "<table>\n",
+ " <tr>\n",
+ " <td>\n",
+ "<b>Méthode</b>\n",
+ " </td>\n",
+ " <td>\n",
+ "<b>Zéro simple</b>\n",
+ " </td>\n",
+ " <td>\n",
+ "<b>Zéro double</b>\n",
+ " </td>\n",
+ " </tr>\n",
+ " <tr>\n",
+ " <td>\n",
+ "Newton\n",
+ " </td>\n",
+ " <td>\n",
+ "6\n",
+ " </td>\n",
+ " <td>\n",
+ "17\n",
+ " </td>\n",
+ " </tr>\n",
+ " <tr>\n",
+ " <td>\n",
+ "Newton-corde\n",
+ " </td>\n",
+ " <td>\n",
+ "13\n",
+ " </td>\n",
+ " <td>\n",
+ "50 (=n_max)\n",
+ " </td>\n",
+ " </tr>\n",
+ " <tr>\n",
+ " <td>\n",
+ "parallèle\n",
+ " </td>\n",
+ " <td>\n",
+ "50 (=n_max)\n",
+ " </td>\n",
+ " <td>\n",
+ "50 (=n_max)\n",
+ " </td> \n",
+ " </tr>\n",
+ " <tr>\n",
+ " <td>\n",
+ "sécante\n",
+ " </td>\n",
+ " <td>\n",
+ "6\n",
+ " </td>\n",
+ " <td>\n",
+ "30\n",
+ " </td>\n",
+ " </tr>\n",
+ " <tr>\n",
+ " <td>\n",
+ "bissection\n",
+ " </td>\n",
+ " <td>\n",
+ "33\n",
+ " </td>\n",
+ " <td>\n",
+ "pas applicable \n",
+ " \n",
+ "ici (Bolzano)\n",
+ " </td>\n",
+ " </tr>\n",
+ "</table>\n",
+ "La vitesse de convergence est plus rapide pour un zéro simple que pour un zéro double. Pour la méthode de Newton, nous avons vu que cette convergence est quadratique dans le cas d'un zéro simple."
+ ]
+ }
+ ],
+ "metadata": {
+ "celltoolbar": "Format de la Cellule Texte Brut",
+ "This documentation, contained in your personal disk, will provide you with some information on the most advanced features available on . Should you need basic information to get started with Jupyter/JupyterLab, please refer to the corresponding [online documentation](https://jupyterlab.readthedocs.io/en/stable/).\n",
+ "\n",
+ "## A quick overview\n",
+ "\n",
+ "For all users connecting to  (`noto.epfl.ch`), a **single user web server** is launched. Your **web browser** is communicating with your single user server through the **Jupyter Hub**.\n",
+ "\n",
+ "<img src=\"Images/JupyterHub.png\">\n",
+ "<center>figure: Alice, Bob and Chris are connected to their single user server.</center> \n",
+ "<br>\n",
+ "\n",
+ "This personal web server provides you with a web interface where you can see your notebooks, files and more.  lets you choose between the [JupyterLab](https://jupyterlab.readthedocs.io/en/stable/) interface or the classic notebook interface.\n",
+ "\n",
+ "<img src=\"Images/JupyterLab.png\">\n",
+ "<center>figure: Alice works on her files and notebooks, etc. with JupytherLab.</center>\n",
+ "\n",
+ "## Your files\n",
+ "Your notebooks (and any other files) are stored in your personal disk, which is organised in **files and directories** like a normal disk. Everything you create or install is stored on your disk, so that you can retreive all your work the next time you connect to .\n",
+ "\n",
+ "## Your notebooks\n",
+ "Notebooks contains cells that are either **code** cells or **text** cells. Text cells contain [markdown](https://en.wikipedia.org/wiki/Markdown) formating instructions (including links, images and more) and [$\\LaTeX$](https://www.latex-project.org/)-like formulas thanks to [MathJax](http://docs.mathjax.org/en/latest/index.html). e.g.:\n",
+ "\n",
+ "$$\n",
+ "\\begin{align}\n",
+ "\\dot{x} & = \\sigma(y-x) \\\\\n",
+ "\\dot{y} & = \\rho x - y - xz \\\\\n",
+ "\\dot{z} & = -\\beta z + xy\n",
+ "\\end{align}\n",
+ "$$\n",
+ "\n",
+ "Code cell contain code (Python, R, etc.) that the **kernel** executes. Tipically, you write Python code and you use a Python kernel to execute it - but you can change that if you want.\n",
+ "The following cell contains code that you can execute, change and execute again. To execute a cell:\n",
+ "\n",
+ "1. Select it\n",
+ "1. Press the \"play\" button in the notebook's toolbar (or just type **CTRL**+**Enter** or **SHIFT**+**Enter**)"
+ "for activity_time in sorted(activities.keys()):\n",
+ " if hour <= activity_time:\n",
+ " print('It\\'s {} o\\'clock, I guess you are {} right now.'.format(hour, activities[activity_time]))\n",
+ " break\n",
+ "else:\n",
+ " print('You should be sleeping at {} o\\'clock.'.format(hour))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Using the terminal\n",
+ "\n",
+ "<img src=\"Images/Terminal.png\">\n",
+ "\n",
+ "From the Jupyter environment, you have access to a terminal with a [bash shell](https://www.gnu.org/software/bash/). You can use the terminal to have a direct access to your files, run bash scripts, do your [git](https://git-scm.com/) work and more.  provides you with a complete and configured bash environment with specific commands to help you on jupyter-related tasks.\n",
+ "\n",
+ "To edit text files from the terminal, you can use `vi` ([vim](https://www.vim.org/)) ; other text editors are available (e.g. `nano`)."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Choosing your web interface\n",
+ "You can choose between JupyterLab or the classic notebook interfaces. Just open the file called `jnb.conf` (located at the root of your `HOME` directory, double click on it) and then change the `INTERFACE` variable as indicated within the file itself. e.g.:\n",
+ "```\n",
+ "INTERFACE=labhub\n",
+ "```\n",
+ "To effectively switch to the new interface, you need to go to the *Control Panel* and *Stop* your server that is currenlty running and the *start* the server again (or just *logout/login*)."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Leaving JupyterHub\n",
+ "\n",
+ "When leaving , several options are available:\n",
+ "\n",
+ "1. By just **closing the window**, you will stay connected and your single user server will keep on running (for some time). Just point your browser to `noto.epfl.ch` and you will get back to where you left. This is not a secure way of proceeding, especially if you are working on a computer that you are sharing.\n",
+ "\n",
+ "1. **Logout**: when you logout, your single user server will be left running, and you will need to login again the next time you are back.\n",
+ "\n",
+ "In all cases, a single user server that is left unused for 30 minutes will get stopped automatically.\n",
+ "\n",
+ "The conclusion is: for **security**, you should always **always logout**."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Next!\n",
+ "\n",
+ "Read the next notebook to learn about [virtual environments and kernels](./10_Envs_and_kernels.ipynb)."
+ "With , you get a complete Python 3.6 **environment** (a set of *packages* and *libraries*) with a Python **Jupyter kernel** (this is the software that *executes* the code in your notebook's cells).\n",
+ "\n",
+ "This readily available environment and kernel will allow you to go forward without installing anything.\n",
+ "\n",
+ "In the event you need to **extend** this provided default environment, you can do so by creating **virtual environments** and add the Python packages you need ; this is also a way to install some non-Python kernels.\n",
+ "\n",
+ "<img src=\"Images/Environment.png\">\n",
+ "<center>figure: Cell, kernel and environment interactions</center>\n",
+ "\n",
+ "#### Keywords\n",
+ "\n",
+ "* An **environment** is composed by a set of packages and libraries\n",
+ "*  gives you a **default Python 3.6 environment** with many libraries\n",
+ "* You can build you own environments (called **virtual environments**)\n",
+ "* Jupyter **kernels** execute your notebook's cells\n",
+ "* You can **install more kernels** in your own virtual environments\n",
+ "\n",
+ "\n",
+ "**Note**: it is *highly* recommended to only install extra python packages inside a virtual environment. Installing incompatible/untested Python packages in the default environment can break you default Python environment (the one that runs your jupyter server). For that reason, the access to the `pip` command outside of virtual environments has been disabled - you will get a nice friendly reminder if you try to use it.\n",
+ "\n",
+ "#### Keywords\n",
+ "\n",
+ "* `pip` is the Python packaging tool that is used for `noto.epfl.ch`\n",
+ "* Run the `pip` command in your Jupyter terminal to install packages\n",
+ "* Opening Jupyter terminal: by clicking on <img src=\"Images/Terminal.png\"> from the launcher, you are redirected to a complete `bash` command line interface, where you can navigate through your files and directories and run commands."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# This pip command is not possible in the default environment\n",
+ "# because all your new packages MUST be installed in a virtual environment.\n",
+ "pip install mysuperlib"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Still, we allow you to run the `pip list` command in the default environment, so that you can see what packages are already available in the default environment: "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# This is allowed in the default environment:\n",
+ "pip list --format=columns"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Use cases for virtual environments and jupyter kernels\n",
+ "\n",
+ "Sometimes, you need specific Python packages or a Jupyter kernels that  does not provide by default.\n",
+ "\n",
+ "An elegant way of solving that problems is a two-step process:\n",
+ "\n",
+ "1. You *create* a suitable environment (an environment where you install the python packages you are missing).\n",
+ "1. You *install* a jupyter kernel in that envrionment.\n",
+ "\n",
+ "Sometimes, you might just need a Jupyter kernel other that the default one (e.g. a non-Python kernel, like the `bash` kernel this notebook is using right now), and no specific environment is required.\n",
+ "<center>figure: New virtual environment (using librairies from the default environment)<br>with a new library (\"libX\") and a new jupyter kernel, so that your notebooks can use \"libX\".</center>\n",
+ "<br>\n",
+ "<br>\n",
+ "Read on the next sections to learn how to handle environments and jupyter kernels.\n",
+ "\n",
+ "## Dealing with Python's virtual environments\n",
+ "\n",
+ "The following `bash` commands are available (you type those commands in jupyter's terminal) and will help you managing your virtual environmments:\n",
+ "\n",
+ "- `my_venvs_list`\n",
+ "- `my_venvs_create`\n",
+ "- `my_venvs_activate`\n",
+ "- `my_venvs_remove`\n",
+ "\n",
+ "These commands are just using Python's built-in functions with the right parameters to create environments that will be built on top of the default environment. This means that any new virtual environment already provides the same packages and libraries than the default environment.\n",
+ "\n",
+ "Of course, you are free to type the standard Python command yourself in Jupyter terminal:\n",
+ "If you do so, **use** the parameter `--system-site-packages` to make use of all the packages that are already installed - otherwise you would create an empty virtual environment, which would not be able to run a jupyter kernel."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# List all your virtual environments:\n",
+ "my_venvs_list"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Get help on all my_venvs_* commands:\n",
+ "my_venvs_help"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Dealing with Jupyter kernels\n",
+ "\n",
+ "The following `bash` commands are available (from your Jupyter terminal) to help you managing your Python kernels:\n",
+ "\n",
+ "- `my_kernels_help`: gives you help on kernel creation/removal\n",
+ "- `my_kernels_list`: lists the currently installed kernels\n",
+ "- `my_kernels_create`: creates a new kernel in the currently active virtual environment\n",
+ "- `my_kernels_remove`: removes an installed kernel"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# List all installed kernels\n",
+ "my_kernels_list"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Kernels written in yellow (if any) are the kernels **you** installed, red ones are the default kernels provided with ."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Get help on all my_kernels_* commands:\n",
+ "my_kernels_help"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Next! #\n",
+ "For some hands-on experience, check the [virtual environments tutorial](./11_Tutorial_Envs.ipynb) out!"
+ "# Tutorial on virtual environments and Jupyter kernels\n",
+ "\n",
+ "**Use case study**:\n",
+ "You want to use the Python library `pint`.\n",
+ "> Pint is a Python package to define, operate and manipulate physical quantities: the product of a numerical value and a unit of measurement. It allows arithmetic operations between them and conversions from and to different units.\n",
+ "\n",
+ "So far, `pint` package is **not** available in . Maybe this situation will change in the future, but before requesting this library to be available to everyone on , you want to give it a try - fair enough.\n",
+ "\n",
+ "A typical workflow to solve this would be to go through the following steps:\n",
+ "\n",
+ "1. Create a new virtual environment (`my_venvs_create pint_env`)\n",
+ "1. Activate this new virtual environment (`my_venvs_activate pint_env`)\n",
+ "1. Create a new Jupyter kernel (`my_kernels_create py3_pint \"Py3 Pint\"`)\n",
+ "1. Deactivate the virtual environment when you are done (`deactivate`)\n",
+ "\n",
+ "You just have to run the appropriate commands in your Jupyter Terminal.\n",
+ "\n",
+ "## Step by step\n",
+ "\n",
+ "### Creating a virtual environment and a Jupyter kernel for `pint` package\n",
+ "\n",
+ "You can run the following commands to create the `pint_env` virtual environment and a kernel called `py3_pint` that will run in this new virtual environment.\n",
+ "Type in those commands in your Jupyter terminal, one by one (or copy/paste them):\n",
+ "\n",
+ "```\n",
+ "my_venvs_create pint_env\n",
+ "my_venvs_activate pint_env\n",
+ "pip install pint\n",
+ "my_kernels_create py3_pint \"Py3 Pint\"\n",
+ "my_venvs_deactivate\n",
+ "```\n",
+ "\n",
+ "At this point, your new kernel (**Py3 Pint**) and your environment (**pint_env**) are ready.\n",
+ "\n",
+ "**Important**: If you want JupyterLab to be aware of this new kernel, you need to refresh your browser's page (JupyterLab is not able to refresh its kernel list on its own yet).\n",
+ "\n",
+ "### Using the `pint` package\n",
+ "\n",
+ "Once your web broswer refresh is done, you will see the following icon in your launcher:\n",
+ "\n",
+ "<img src=\"Images/Py3_Pint.png\">\n",
+ "\n",
+ "This means that you are ready to create a notebook using the `Py3 Pint` kernel by clicking on this icon. You can also use any existing notebook and **change** the kernel by click on the kernel button (top right of your notebook).\n",
+ "\n",
+ "### Removing a virtual environment and a Jupyter kernel for `pint` package\n",
+ "\n",
+ "If you decide that you no longer need to use the `pint` package, you need to remove the virtual environment and kernel. You can do so by typing the following commands in your Jupyter terminal:\n",
+ "\n",
+ "```\n",
+ "my_kernels_remove py3_pint\n",
+ "my_venvs_remove pint_env\n",
+ "```\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Quick building of a kernel\n",
+ "Sometimes, you just need an extra `pip` package - or a complete list of packages - to make a notebook work. A quick way of doing this is to use the `kbuilder_create` (and `kbuilder_remove`) commands.\n",
+ "\n",
+ "### For just a single missing package (`pint`)\n",
+ "Let's say you need the `pint` package for your notebook. While you could use the above procedure,  provides a fast lane for this exact situation. Type the following command in your Jupyter terminal:\n",
+ "```\n",
+ "kbuilder_create p3_pint pint\n",
+ "```\n",
+ "This will create the virtual environment and kernel required so that you can use `pint` from your notebook.\n",
+ "\n",
+ "**Note:** Once the virtual environment and kernel are ready, you can add even more packages by activating the virtual environment and running more `pip` commands from the terminal:\n",
+ "```\n",
+ "my_venvs_activate p3_pint\n",
+ "pip install asymcript\n",
+ "my_venvs_deactivate\n",
+ "```\n",
+ "\n",
+ "### For a more specific environment\n",
+ "Should you need a very specific Python environment, you will most probably be provided with a `requirements.txt` file (see [documentation here](https://pip.readthedocs.io/en/stable/reference/pip_install/#example-requirements-file)). This text file is just a list of all the packages that are required to run the notebook properly. Once the `requirements.txt` file (its name is not important) is uploaded to your disk on  and visible from the file browser view, run in your terminal (for example):\n",
+ "```\n",
+ "kbuilder_create p3-spec requirements.txt\n",
+ "```\n",
+ "\n",
+ "### Remove virtual environment and kernel\n",
+ "Just run in your terminal, for example:\n",
+ "```\n",
+ "kbuilder_remove p3_pint\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Next! #\n",
+ "Learn more about the available [Python and Non-Python kernels](./20_Non-Python_kernels.ipynb)."
+ "The following kernels are available from . They are ready to use, you don't have to install them.\n",
+ "\n",
+ "### Python kernel\n",
+ "Run the following cell to list all available Python (3.6) packages."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "/usr/bin/pip3 list --format=columns"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Bash kernel\n",
+ "By using this kernel, you can execute **bash** code cells in your notebooks. Please note that the bash environment inside Bash kernel notebooks is initialized just like your terminal's shell."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "bash --version"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Octave kernel\n",
+ "[Octave](https://www.gnu.org/software/octave/) is a scientific programming language."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "octave --no-gui --version"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### C kernel\n",
+ "The C kernel allows you to compile and run [C](https://en.wikipedia.org/wiki/C_(programming_language)) code."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "gcc --version"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### R kernel\n",
+ "[R](https://www.r-project.org/) is a programming language for statistical computing"
+ "`git` is a [source code](https://git-scm.com/) [management system](https://en.wikipedia.org/wiki/Version_control). When working on a project that is made of text files (like notebooks), using a versioning system can be very handy, even if you are working alone. It allows you to keep track of the changes you are making to the files (content, names, location), and to have all the history of your files available at anytime.\n",
+ "\n",
+ "When a group of persons is working simultaneously on the same set of files, a versioning system will help you merging changes from your collegues and resolving conflicts (when multiple colleagues are working on the same part of a file).\n",
+ "\n",
+ "Understanding how a versionning system works is important: *all* (mostly computer related) science projects deal with text files (programming source code, text documents, research documents, etc.) and use versionning systems.\n",
+ "\n",
+ "Do yourself a favor and learn the [basics](https://rogerdudler.github.io/git-guide/) of versionning systems and git :-)\n",
+ "\n",
+ "## `git` and \n",
+ "The `git` command is available from Jupiter terminal ; on , the [jupyterlab-git](https://github.com/jupyterlab/jupyterlab-git) extension is activated in the JupyterLab interface, providing you with some nice buttons to do the most common git operations (clone, pull, commit, push, etc.).\n",
+ "\n",
+ "The [nbgitpuller](https://github.com/jupyterhub/nbgitpuller) service is also activated on , allowing anyone to share git links.\n",
+ "\n",
+ "# `git` servers\n",
+ "\n",
+ "There are many `git` servers available around the internet and the `git` client installed on  is certainly compatible with all them - none the less, we encourage you to use [c4science](https://c4science.ch/) which will hosts your `git` data and respect its privacy.\n",
+ "\n",
+ "# Authentication with remote `git` servers\n",
+ "\n",
+ "Authentication is not required to clone/pull from **public** `git` repositories. If you want to send your changes to remote `git` repositories (push), you will have to authenticate yourself on the `git` server using your credentials (username/password or SSH key).\n",
+ "\n",
+ "Passwordless authentication (with an SSH key) is available from . The first time you connect to , SSH key pairs are generated for you. All you need to enable passwordless authentication with your `git` server is to export your **public** key (file `/home/.ssh/id_rsa.pub`) over to the remote `git` server.\n",
+ "\n",
+ "Run the following cell to get **your** SSH public key:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "if [ -r /home/.ssh/id_rsa.pub ]; then\n",
+ " echo \"Your SSH public key file is: /home/.ssh/id_rsa.pub\"\n",
+ " echo \"Here is the public key you need to export to your remote git server:\"\n",
+ " echo -en ${Green}\n",
+ " cat /home/.ssh/id_rsa.pub\n",
+ " echo -en ${NoColor}\n",
+ "else\n",
+ " echo \"It looks like you don't have an SSH key pair yet.\"\n",
+ " echo -e \"Run the command ${Green}ssh-keygen${NoColor} from your terminal to generate a new key pair.\"\n",
+ "fi"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Setting up your name and email address\n",
+ "\n",
+ "It is also nice to set up a **full name** and **email address** so that anytime you commit changes on a repository, those changes get tagged with you name and email address.\n",
+ "\n",
+ "First, let's check your current settings on :"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "git config --global user.name\n",
+ "git config --global user.email"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "If the output from the above cell is empty, this means that your name and email address are not set up in `git`. To fix that (or to update them), use the following cell.\n",
+ "\n",
+ "**Please update your name and email address before executing the cell!**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "git config --global user.name \"Your Name Here\"\n",
+ "Then you can run again the following commands to check that the changes have been applied."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "git config --global user.name\n",
+ "git config --global user.email"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "If you are happy with the current configuation, you are ready to go !\n",
+ "\n",
+ "**Note:** you have to configure `git` on all the computers you are using - you can reuse the same commands. You can perfectly work from  and from your laptop ; just make sure both are configured correctly."
+ "*This notebook contains communications from the Noto team to the Noto users.*\n",
+ "\n",
+ "If you wish to contact the Noto team to report a bug or request new features, send an email to [noto-support@groupes.epfl.ch](mailto:noto-support@groupes.epfl.ch).\n",
+ "\n",
+ "## Latest news\n",
+ "\n",
+ "### Noto is now running to JupyterLab 3.x\n",
+ "\n",
+ "During the summer 2021, the Noto platform has been migrated to a newer software stack, including:\n",
+ "- JupyterLab 3.1.4\n",
+ "- Python 3.8\n",
+ "- R 4.1.0\n",
+ "- GCC 9\n",
+ "\n",
+ "The rest of the Python stack (matplotlib, plotly, etc.) has also been updated.\n",
+ "\n",
+ "While we hope that this transition will be easy on our users, there could still be bugs and imcompatibilies that slipped in under our radar.\n",
+ "\n",
+ "**We encourage you to test your notebooks and repport any regression at [noto-support@groupes.epfl.ch](mailto:noto-support@groupes.epfl.ch).**\n",
+ "\n",
+ "We also encourage you to **adapt your notebook to match the upgraded Python libraries**, if need be.\n",
+ "\n",
+ "### Required migrations\n",
+ "\n",
+ "Because of the aforementioned changes, there are actions required on the user side. Namely, user will have to migrate their virtual environments and personal R libraries.\n",
+ "* See the [migration documentation entry point](./Migrations/00_Migration_Introduction.ipynb)\n",
+ "* See the [virtual environment migration guide](./Migrations/10_Migration_VirtualEnvs.ipynb) if you want to keep using your virtual environments.\n",
+ "* See the [R libraries migration guide](./Migrations/20_Migration_R_Libs.ipynb) if you want to port your R libraries."
+ "**Note**: if XKCD font (available [here](https://github.com/ekmett/arcade/raw/master/static/fonts/xkcd.ttf)) has been installed (in your directory `/home/.local/share/fonts/`) **after** matplotlib was ever run, you have to clean (i.e. remove) matplotlib's font cache by running in your terminal:\n",
+ "During the summer 2021, **Noto** has been upgraded to a newer software stack.\n",
+ "\n",
+ "While many Python packages have been updated, there should not a huge gap with respect to the previous versions avaible previously.\n",
+ "\n",
+ "However, **the following components have been updated and do have a impact on your existing Noto workspace**.\n",
+ "\n",
+ "## Python has been updated from version 3.6 to version 3.8\n",
+ "\n",
+ "The main consequence for you is that your previous virtual environments (Python 3.6 based) will no longer work.\n",
+ "When activated, those virtual environment will totally be ignored by Python 3.8.\n",
+ "\n",
+ "Those previous virtual environments need to be migrated to Python 3.8.\n",
+ "The migration consists in:\n",
+ "- Renaming the old virtual environments\n",
+ "- Creating new virtual environments\n",
+ "- Extracting a list of python modules from the old virtual environment\n",
+ "- Installing the python modules in the new virtual environment\n",
+ "- Removing the old virtual environment\n",
+ "\n",
+ "We made a notebook to guide you through the migration: check the [virtual environment migration guide](./10_Migration_VirtualEnvs.ipynb).\n",
+ "\n",
+ "## R has been updated from version 3.6.3 to version 4.1.0\n",
+ "\n",
+ "When you are installing extra libraries for R, they get installed in the folder folder:\n",
+ "```\n",
+ "/home/.local/R/x.y/lib/xxx\n",
+ "```\n",
+ "Where `x.y` is the R version and `xxx` is the name of the extra library (*e.g.* `/home/.local/R/3.6/lib/date`).\n",
+ "\n",
+ "It is not recommended to simply copy a lib from `/home/.local/R/3.6/lib/date` to `/home/.local/R/4.1/lib/date`: R libs are designed to work with a specific R version, and some are compiled against system libraried that have changed.\n",
+ "\n",
+ "The goal for a successful R migration is to:\n",
+ "- Get a list of the R libraries that have been previously installed\n",
+ "- Install those libraries again\n",
+ "\n",
+ "We made a notebook to guide you through the migration: check the [R libraries migration guide](./20_Migration_R_Libs.ipynb)."
+ "* If you do not have any virtual environment using Python 3.6 (*i.e.* previous cell's output is empty), then you do not need to migrate anything!\n",
+ "\n",
+ "* If you no longer need any of the virtual environments, run the command in a terminal to a virtual environment:\n",
+ "\n",
+ "`my_venvs_remove [venv_name]`\n",
+ "\n",
+ "Once, this is done, you can come back to this notebook and migrate the remaining virtual environments to Python 3.8 by following the next instructions."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "7b9ef41d-524d-4f60-958b-298ed05c0496",
+ "metadata": {},
+ "source": [
+ "## Let's rename your old virtual environments\n",
+ "\n",
+ "Run the next cell to rename you old Python 3.6 virtual environments."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "8c2d033f-6e9d-4ba3-98a2-e39083479dd8",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "for d in $(find /home/my_venvs/ -maxdepth 3 -mindepth 3 -type d -name 'python3.6' | cut -d '/' -f -4 | grep -v '_p36$'); do mv -v ${d} ${d}_p36; done"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "09d61595-93e5-472e-9ebc-17c710debf30",
+ "metadata": {},
+ "source": [
+ "Let's now check the new name of the virtual environments that need to be migrated:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "5f4eea04-54dd-47df-b463-cda455b8401e",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "list_p36_venvs"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "270d8c12-9e06-4892-b66b-ae436e988250",
+ "metadata": {},
+ "source": [
+ "## Let's create brand new virtual environments with Python 3.8\n",
+ "\n",
+ "The next cell will create a new Python 3.8 virtual environment for each of your old Python 3.6 virtual environment. Note that this will take some time (around 90 seconds per virtual environment).\n",
+ "It will also write a list of the python modules that were installed in a file called `requirements.txt` in each new virtual environments."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "f1693180-15ab-4a7c-8637-c43e56b18547",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "for old_v in $(list_p36_venvs); do\n",
+ " new_v=$(echo $old_v | sed 's/_p36$//')\n",
+ " \n",
+ " # Loop on all p36 venvs and create a new p38 venv\n",
+ " echo \"* Creating $new_v for $old_v\"\n",
+ " if [[ ! -d /home/my_venvs/$new_v ]]; then\n",
+ " my_venvs_create $new_v\n",
+ " fi\n",
+ " \n",
+ " # Get a list of installed packages and build a requirement.txt file\n",
+ "**Cours de Mathématiques Spéciales** (semestre de printemps 2021)<br>\n",
+ "*Roger Sauser*"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "### Le but de cette séance d'exercices est de vous permettre de consolider vos connaissances concernant la méthode de Picard et de vous familiariser avec trois méthodes de point fixe : \n",
+ "* la **méthode de Newton** (aussi appelée **méthode de Newton-Raphson** ou **de la tangente**) ;\n",
+ "* la **méthode de la corde** (aussi appelée **méthode de Newton-corde**) ;\n",
+ "* la **méthode de la parallèle**. \n",
+ "\n",
+ "### Nous reviendrons également sur une autre méthode itérative : la méthode dite de la sécante."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "deletable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "outputs": [],
+ "source": [
+ "# nous allons utiliser numpy et matplotlib :\n",
+ "import numpy as np\n",
+ "import matplotlib.pyplot as plt"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "# Exercices de révision (méthode de Picard)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 1\n",
+ "Dans le TP précédent, nous avons constaté que les fonctions \n",
+ "sont $K$-contractantes sur $\\mathcal{R}$ dans le cas de $f(x)$ et sur, par exemple, \n",
+ "$I_\\pi=\\displaystyle\\left]-\\frac{\\pi}{2},\\frac{\\pi}{2}\\right[$ dans le cas de $g(x)$.\n",
+ "\n",
+ "Dans cet exercice, on vous propose de :\n",
+ "\n",
+ "a) vérifier le théorème du point fixe de Banach dans le cas particulier de la fonction $f$ en :\n",
+ " * représentant graphiquement la fonction $f$ ;\n",
+ " * vérifiant l'existence d'un unique point fixe ;\n",
+ " * trouvant une valeur approchée à 5 décimales de ce point fixe grâce à la méthode de Picard.\n",
+ "\n",
+ "b) vérifier de manière analogue le théorème 10 du polycopié dans le cas particulier de la fonction $g$ sur $I_\\pi$. Ce théorème s'applique-t'il dans le cas de cette fonction sur l'intervalle $\\displaystyle\\left]-\\frac{\\pi}{2},0\\right[$ ?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# a)\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# b)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 2\n",
+ "\n",
+ "Soit $g(x)$ une fonction **de $\\mathcal{R}$ dans $\\mathcal{R}$** telle que $g^3$ est **$K$-contractante**. \n",
+ "Montrer que $g$ possède un et un seul point fixe.\n",
+ "\n",
+ "Illustrer ce résultat en représentant graphiquement la fonction $g^3$ pour le cas particulier où $g(x)=0.5x+3$.\n",
+ "\n",
+ "Attention : la notation $g^3$ signifie ici que la fonction $g^3$ est obtenue par **composition répétée** de la fonction $g$ avec elle-même trois fois."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "# Exercices sur la méthode de Newton"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 3\n",
+ "Ci-dessous, à l'exercice 4, nous allons implémenter la méthode de Newton en supposant connue la dérivée de la fonction dont on cherche le zéro. Toutefois, il pourrait être commode (si l'on manque de temps ou d'aisance pour déterminer la dérivée \"à la main\") d'obtenir une estimation numérique de la dérivée.\n",
+ "\n",
+ "Numériquement, il est possible d'approximer la valeur du nombre dérivée d'une fonction en un point en calculant la pente de la courbe en ce point avec un des deux quotients suivants :\n",
+ "$$\n",
+ "\\frac{f(a+h)-f(a)}{h}~~~~\n",
+ "\\hbox{ou}~~~~\n",
+ "\\frac{f(a)-f(a-h)}{h}\\,.\n",
+ "$$\n",
+ "\n",
+ "Souvent, on privilégie la moyenne de ces deux formules et on écrit :\n",
+ "\n",
+ "$$\n",
+ "f'(a) \\cong \\frac{f(a+h)-f(a-h)}{2h}\\,.\n",
+ "$$\n",
+ "\n",
+ "Dans cet exercice, on vous propose d'écrire une fonction `derivee` ayant trois arguments : une fonction $f$ à dériver, l'abscisse $a$ où la dérivée doit être approximée et la valeur du pas $h$ (on posera $h=0.005$ comme valeur par défaut). On vous demande de vérifier votre implémentation en représentant les fonction $f_1(x)=\\sin{(x)}$ et $f_2(x)=(x+1)^2(x-\\frac{3}{2})$ accompagnées de leur dérivée respective dans l'intervalle $[-2,2]$. Calculez l'erreur commise en utilisant votre fonction pour évaluer la dérivée de $f_1$ en $x=1.1$.\n",
+ "\n",
+ "Remarque : dans l'exercice 2 du TP14, nous avons calculé la vitesse et l'accélération d'un objet à partir d'un ensemble de mesures expérimentales des positions de cet objet."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 4\n",
+ "Dans cet exercice, on se propose de déterminer numériquement les zéros de la fonction \n",
+ "$f_2(x)=(x+1)^2(x-\\frac{3}{2})$ définie à l'exercice 3. \n",
+ "\n",
+ "Pour ce faire, nous allons implémenter la **méthode de Newton** en définissant une fonction `resoudre_par_Newton`$\\dots$\n",
+ "* $\\dots$ dont les arguments sont la fonction `f` étudiée, le point de départ `x_0` de la méthode, la fonction dérivée `fprime`, la tolérance `eps` et le nombre maximum d'itérations autorisées `n_max` ;\n",
+ "* $\\dots$ qui calcule par itérations une approximation $\\bar x$ d'un zéro de $f$ ;\n",
+ "* $\\dots$ qui s'arrête :\n",
+ " - soit quand la valeur absolue de $f(\\bar x)$ est inférieure à la tolérance ; \n",
+ " - soit quand le nombre maximum d'itérations est atteint ; \n",
+ " - si la valeur de la dérivée utilisée à chaque itération est inférieure à `1e-10` pour éviter tout problème de divergence dans la formule utilisée ;\n",
+ "* $\\dots$ qui retourne une valeur approchée d'un zéro de $f$, ainsi qu'une liste de tuples, ces tuples contenant les couples $(x_n,f(x_n))$ utilisés à chaque itération $n$. \n",
+ "\n",
+ "Tester votre implémentation en recherchant les deux zéros de la fonction $f_2$. Expérimentez l'efficacité de la méthode selon le point de départ $x_0$ choisi et selon la nature du zéro considéré. Constatez-vous des différences notables ? Si oui, essayez d'en expliquer la raison.\n",
+ "\n",
+ "Vérifier que la méthode de Newton est une **méthode de point fixe** en affichant la différence\n",
+ "$$\n",
+ "x_{n} - x_{n-1}\n",
+ "$$\n",
+ "pour chacune des itérations. Quelles conclusions peut-on tirer de la comparaison de cette différence à la dernière itération avec la tolérance (sur $f(\\bar x)$) que nous avons considérée ?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "# Exercice sur les méthodes de la corde, de la parallèle et de la sécante (comparaison avec la méthode de la bissection et la méthode de Newton)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 5\n",
+ "Dans cet exercice, nous allons revenir sur la fonction $f_2(x)=(x+1)^2(x-\\frac{3}{2})$ déjà étudiée plus haut. En particulier, on vous demande :\n",
+ "* d'implémenter les méthodes **de la corde** (**Newton-corde**), **de la parallèle** (en donnant la valeur de $\\lambda$ en paramètre), **de la sécante** et **de la bissection** en vous inspirant de ce qui a été fait à l'exercice 4 ;\n",
+ "* de comparer le nombre d'itérations nécessaires à chacune des méthodes pour arriver à approximer les deux zéros de $f_2$ en assurant une **précision de 10 décimales** et en partant d'un **même $x_0$** pour chaque méthode (vous pouvez par exemple démarrer à une distance 0.5 d'un zéro) ;\n",
+ "* quelles conclusions pouvez-vous tirer de vos expérimentations ?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# méthode de Newton-corde\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# méthode de la parallèle\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# méthode de la sécante\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# méthode de la bissection\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "celltoolbar": "Format de la Cellule Texte Brut",
+ "**Cours de Mathématiques Spéciales** (semestre de printemps 2021)<br>\n",
+ "*Roger Sauser*"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "### Le but de cette séance d'exercices est de vous permettre de consolider vos connaissances concernant la méthode de Picard et de vous familiariser avec trois méthodes de point fixe : \n",
+ "* la **méthode de Newton** (aussi appelée **méthode de Newton-Raphson** ou **de la tangente**) ;\n",
+ "* la **méthode de la corde** (aussi appelée **méthode de Newton-corde**) ;\n",
+ "* la **méthode de la parallèle**. \n",
+ "\n",
+ "### Nous reviendrons également sur une autre méthode itérative : la méthode dite de la sécante."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "deletable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "outputs": [],
+ "source": [
+ "# nous allons utiliser numpy et matplotlib :\n",
+ "import numpy as np\n",
+ "import matplotlib.pyplot as plt"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "# Exercices de révision (méthode de Picard)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 1\n",
+ "Dans le TP précédent, nous avons constaté que les fonctions \n",
+ "sont $K$-contractantes sur $\\mathcal{R}$ dans le cas de $f(x)$ et sur, par exemple, \n",
+ "$I_\\pi=\\displaystyle\\left]-\\frac{\\pi}{2},\\frac{\\pi}{2}\\right[$ dans le cas de $g(x)$.\n",
+ "\n",
+ "Dans cet exercice, on vous propose de :\n",
+ "\n",
+ "a) vérifier le théorème du point fixe de Banach dans le cas particulier de la fonction $f$ en :\n",
+ " * représentant graphiquement la fonction $f$ ;\n",
+ " * vérifiant l'existence d'un unique point fixe ;\n",
+ " * trouvant une valeur approchée à 5 décimales de ce point fixe grâce à la méthode de Picard.\n",
+ "\n",
+ "b) vérifier de manière analogue le théorème 10 du polycopié dans le cas particulier de la fonction $g$ sur $I_\\pi$. Ce théorème s'applique-t'il dans le cas de cette fonction sur l'intervalle $\\displaystyle\\left]-\\frac{\\pi}{2},0\\right[$ ?"
+ "print('Point fixe trouvé par la méthode de Picard : x =', resultat[0])\n",
+ "print(\"Le nombre d'itérations effectuées est : n =\", resultat[1])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "La fonction $g$ est $K$-contractante sur $I=\\displaystyle\\left]-\\frac{\\pi}{2},0\\right[$, mais $\\exists x \\in I$ tel que $g(x)\\not\\in I$. Toutes les hypothèses du théorème ne sont donc pas satisfaites."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 2\n",
+ "\n",
+ "Soit $g(x)$ une fonction **de $\\mathcal{R}$ dans $\\mathcal{R}$** telle que $g^3$ est **$K$-contractante**. \n",
+ "Montrer que $g$ possède un et un seul point fixe.\n",
+ "\n",
+ "Illustrer ce résultat en représentant graphiquement la fonction $g^3$ pour le cas particulier où $g(x)=0.5x+3$.\n",
+ "\n",
+ "Attention : la notation $g^3$ signifie ici que la fonction $g^3$ est obtenue par **composition répétée** de la fonction $g$ avec elle-même trois fois."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "Par hypothèse, $g^3$ est $K$-contractante sur $\\mathcal{R}$. Par le théorème de Banach, $g^3$ possède donc un unique point fixe que nous allons noter $\\bar x$ (autrement dit, $\\bar x$ est tel que $g^3(\\bar x) = \\bar x$).\n",
+ "\n",
+ "Nous allons commencer par montrer que $g$ possède un point fixe. Notons $g(\\bar x)\\equiv y$. \n",
+ "Nous constatons que $y$ est un point fixe de $g^3$. Par unicité du point fixe de $g^3$, on a donc que $y=\\bar x$, autrement dit $g(\\bar x)=\\bar x$. Nous concluons à l'**existence** d'un point fixe pour la fonction $g$.\n",
+ "\n",
+ "On se convainc que ce point est unique en observant que tout point fixe de $g$ est également un point fixe de $g^3$ (point fixe qui est unique) et est donc égal à $\\bar x$."
+ "Ci-dessous, à l'exercice 4, nous allons implémenter la méthode de Newton en supposant connue la dérivée de la fonction dont on cherche le zéro. Toutefois, il pourrait être commode (si l'on manque de temps ou d'aisance pour déterminer la dérivée \"à la main\") d'obtenir une estimation numérique de la dérivée.\n",
+ "\n",
+ "Numériquement, il est possible d'approximer la valeur du nombre dérivée d'une fonction en un point en calculant la pente de la courbe en ce point avec un des deux quotients suivants :\n",
+ "$$\n",
+ "\\frac{f(a+h)-f(a)}{h}~~~~\n",
+ "\\hbox{ou}~~~~\n",
+ "\\frac{f(a)-f(a-h)}{h}\\,.\n",
+ "$$\n",
+ "\n",
+ "Souvent, on privilégie la moyenne de ces deux formules et on écrit :\n",
+ "\n",
+ "$$\n",
+ "f'(a) \\cong \\frac{f(a+h)-f(a-h)}{2h}\\,.\n",
+ "$$\n",
+ "\n",
+ "Dans cet exercice, on vous propose d'écrire une fonction `derivee` ayant trois arguments : une fonction $f$ à dériver, l'abscisse $a$ où la dérivée doit être approximée et la valeur du pas $h$ (on posera $h=0.005$ comme valeur par défaut). On vous demande de vérifier votre implémentation en représentant les fonction $f_1(x)=\\sin{(x)}$ et $f_2(x)=(x+1)^2(x-\\frac{3}{2})$ accompagnées de leur dérivée respective dans l'intervalle $[-2,2]$. Calculez l'erreur commise en utilisant votre fonction pour évaluer la dérivée de $f_1$ en $x=1.1$.\n",
+ "\n",
+ "Remarque : dans l'exercice 2 du TP14, nous avons calculé la vitesse et l'accélération d'un objet à partir d'un ensemble de mesures expérimentales des positions de cet objet."
+ "# estimation de l'erreur commise (avec le pas par défaut h=0.005)\n",
+ "print(derivee(f_1,1.1)-np.cos(1.1))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 4\n",
+ "Dans cet exercice, on se propose de déterminer numériquement les zéros de la fonction \n",
+ "$f_2(x)=(x+1)^2(x-\\frac{3}{2})$ définie à l'exercice 3. \n",
+ "\n",
+ "Pour ce faire, nous allons implémenter la **méthode de Newton** en définissant une fonction `resoudre_par_Newton`$\\dots$\n",
+ "* $\\dots$ dont les arguments sont la fonction `f` étudiée, le point de départ `x_0` de la méthode, la fonction dérivée `fprime`, la tolérance `eps` et le nombre maximum d'itérations autorisées `n_max` ;\n",
+ "* $\\dots$ qui calcule par itérations une approximation $\\bar x$ d'un zéro de $f$ ;\n",
+ "* $\\dots$ qui s'arrête :\n",
+ " - soit quand la valeur absolue de $f(\\bar x)$ est inférieure à la tolérance ; \n",
+ " - soit quand le nombre maximum d'itérations est atteint ; \n",
+ " - si la valeur de la dérivée utilisée à chaque itération est inférieure à `1e-10` pour éviter tout problème de divergence dans la formule utilisée ;\n",
+ "* $\\dots$ qui retourne une valeur approchée d'un zéro de $f$, ainsi qu'une liste de tuples, ces tuples contenant les couples $(x_n,f(x_n))$ utilisés à chaque itération $n$. \n",
+ "\n",
+ "Tester votre implémentation en recherchant les deux zéros de la fonction $f_2$. Expérimentez l'efficacité de la méthode selon le point de départ $x_0$ choisi et selon la nature du zéro considéré. Constatez-vous des différences notables ? Si oui, essayez d'en expliquer la raison.\n",
+ "\n",
+ "Vérifier que la méthode de Newton est une **méthode de point fixe** en affichant la différence\n",
+ "$$\n",
+ "x_{n} - x_{n-1}\n",
+ "$$\n",
+ "pour chacune des itérations. Quelles conclusions peut-on tirer de la comparaison de cette différence à la dernière itération avec la tolérance (sur $f(\\bar x)$) que nous avons considérée ?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Le zéro (double) de f_2(x) obtenu (après 17 itérations, en partant de x_0 = -1.5) est x = -1 .\n",
+ "\n",
+ "\n",
+ "Itération 0 : f(-1.5)=-0.75\n",
+ "Itération 1 : f(-1.26923)=-0.200728\n",
+ "Itération 2 : f(-1.14086)=-0.0523955\n",
+ "Itération 3 : f(-1.07226)=-0.0134301\n",
+ "Itération 4 : f(-1.03663)=-0.00340337\n",
+ "Itération 5 : f(-1.01845)=-0.000856898\n",
+ "Itération 6 : f(-1.00926)=-0.000215003\n",
+ "Itération 7 : f(-1.00464)=-5.38496e-05\n",
+ "Itération 8 : f(-1.00232)=-1.34748e-05\n",
+ "Itération 9 : f(-1.00116)=-3.37027e-06\n",
+ "Itération 10 : f(-1.00058)=-8.42763e-07\n",
+ "Itération 11 : f(-1.00029)=-2.10715e-07\n",
+ "Itération 12 : f(-1.00015)=-5.26818e-08\n",
+ "Itération 13 : f(-1.00007)=-1.31708e-08\n",
+ "Itération 14 : f(-1.00004)=-3.29276e-09\n",
+ "Itération 15 : f(-1.00002)=-8.23195e-10\n",
+ "Itération 16 : f(-1.00001)=-2.058e-10\n",
+ "Itération 17 : f(-1)=-5.145e-11\n",
+ "\n",
+ "\n",
+ "Différence |x_1 - x_0| = 0.230769\n",
+ "Différence |x_2 - x_1| = 0.128375\n",
+ "Différence |x_3 - x_2| = 0.0685985\n",
+ "Différence |x_4 - x_3| = 0.0356283\n",
+ "Différence |x_5 - x_4| = 0.0181833\n",
+ "Différence |x_6 - x_5| = 0.00918926\n",
+ "Différence |x_7 - x_6| = 0.00461976\n",
+ "Différence |x_8 - x_7| = 0.00231626\n",
+ "Différence |x_9 - x_8| = 0.00115974\n",
+ "Différence |x_10 - x_9| = 0.000580271\n",
+ "Différence |x_11 - x_10| = 0.000290236\n",
+ "Différence |x_12 - x_11| = 0.000145143\n",
+ "Différence |x_13 - x_12| = 7.2578e-05\n",
+ "Différence |x_14 - x_13| = 3.62906e-05\n",
+ "Différence |x_15 - x_14| = 1.81457e-05\n",
+ "Différence |x_16 - x_15| = 9.07295e-06\n",
+ "Différence |x_17 - x_16| = 4.5365e-06\n",
+ "\n",
+ "\n",
+ "========================================\n",
+ "\n",
+ "\n",
+ "Le zéro (simple) de f_2(x) obtenu (après 6 itérations, en partant de x_0 = 1) est x = 1.5 .\n",
+ "Le dernier paragraphe de ce notebook fournit une comparaison des différentes méthodes et revient sur les différences observées dans la convergence selon la nature du zéro considéré. La méthode de Newton converge plus rapidement dans le cas d'un zéro simple.\n",
+ "\n",
+ "D'autre part, on constate qu'une condition d'arrêt basée sur la distance entre deux approximations successives est particulièrement indiquée dans le cas d'un zéro simple. "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "# Exercice sur les méthodes de la corde, de la parallèle et de la sécante (comparaison avec la méthode de la bissection et la méthode de Newton)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "deletable": false,
+ "editable": false,
+ "run_control": {
+ "frozen": true
+ }
+ },
+ "source": [
+ "## Exercice 5\n",
+ "Dans cet exercice, nous allons revenir sur la fonction $f_2(x)=(x+1)^2(x-\\frac{3}{2})$ déjà étudiée plus haut. En particulier, on vous demande :\n",
+ "* d'implémenter les méthodes **de la corde** (**Newton-corde**), **de la parallèle** (en donnant la valeur de $\\lambda$ en paramètre), **de la sécante** et **de la bissection** en vous inspirant de ce qui a été fait à l'exercice 4 ;\n",
+ "* de comparer le nombre d'itérations nécessaires à chacune des méthodes pour arriver à approximer les deux zéros de $f_2$ en assurant une **précision de 10 décimales** et en partant d'un **même $x_0$** pour chaque méthode (vous pouvez par exemple démarrer à une distance 0.5 d'un zéro) ;\n",
+ "* quelles conclusions pouvez-vous tirer de vos expérimentations ?"
+ "print('Le zéro (double) de f_2(x) obtenu (après %g itérations, en partant de x_0 = %g et avec une valeur lambda = %g) est x = %g .' % (len(evolution)-1,x_0,Lambda,x))\n",
+ "print('Le zéro (simple) de f_2(x) obtenu (après %g itérations, en partant de x_0 = %g et avec une valeur lambda = %g) est x = %g .' % (len(evolution)-1,x_0,Lambda,x))\n",
+ "print('Le zéro (double) de f_2(x) obtenu (après %g itérations, en partant de x_0 = %g et x_1 = %g) est x = %g .' % (len(evolution)-1,x_0,x_1,x))\n",
+ "print('Le zéro (simple) de f_2(x) obtenu (après %g itérations, en partant de x_0 = %g et x_1 = %g) est x = %g .' % (len(evolution)-1,x_0,x_1,x))\n",
+ "Le tableau ci-dessous résume les résultats que nous avons obtenus en précisant le nombre d'itérations nécessaires pour chacune des méthodes. La **méthode de Newton** se montre particulièrement efficace, alors que la **méthode de la parallèle** est très sensible au choix de la pente `lambda`.\n",
+ "\n",
+ "<table>\n",
+ " <tr>\n",
+ " <td>\n",
+ "<b>Méthode</b>\n",
+ " </td>\n",
+ " <td>\n",
+ "<b>Zéro simple</b>\n",
+ " </td>\n",
+ " <td>\n",
+ "<b>Zéro double</b>\n",
+ " </td>\n",
+ " </tr>\n",
+ " <tr>\n",
+ " <td>\n",
+ "Newton\n",
+ " </td>\n",
+ " <td>\n",
+ "6\n",
+ " </td>\n",
+ " <td>\n",
+ "17\n",
+ " </td>\n",
+ " </tr>\n",
+ " <tr>\n",
+ " <td>\n",
+ "Newton-corde\n",
+ " </td>\n",
+ " <td>\n",
+ "13\n",
+ " </td>\n",
+ " <td>\n",
+ "50 (=n_max)\n",
+ " </td>\n",
+ " </tr>\n",
+ " <tr>\n",
+ " <td>\n",
+ "parallèle\n",
+ " </td>\n",
+ " <td>\n",
+ "50 (=n_max)\n",
+ " </td>\n",
+ " <td>\n",
+ "50 (=n_max)\n",
+ " </td> \n",
+ " </tr>\n",
+ " <tr>\n",
+ " <td>\n",
+ "sécante\n",
+ " </td>\n",
+ " <td>\n",
+ "6\n",
+ " </td>\n",
+ " <td>\n",
+ "30\n",
+ " </td>\n",
+ " </tr>\n",
+ " <tr>\n",
+ " <td>\n",
+ "bissection\n",
+ " </td>\n",
+ " <td>\n",
+ "33\n",
+ " </td>\n",
+ " <td>\n",
+ "pas applicable \n",
+ " \n",
+ "ici (Bolzano)\n",
+ " </td>\n",
+ " </tr>\n",
+ "</table>\n",
+ "La vitesse de convergence est plus rapide pour un zéro simple que pour un zéro double. Pour la méthode de Newton, nous avons vu que cette convergence est quadratique dans le cas d'un zéro simple."
+ ]
+ }
+ ],
+ "metadata": {
+ "celltoolbar": "Format de la Cellule Texte Brut",
+ "evalue": "Value of 'x' is not the name of a column in 'data_frame'. Expected one of ['country', 'continent', 'year', 'lifeExp', 'pop', 'gdpPercap', 'iso_alpha', 'iso_num'] but received: system",
+ "File \u001b[0;32m/usr/local/lib/python3.8/dist-packages/plotly/express/_core.py:1405\u001b[0m, in \u001b[0;36mbuild_dataframe\u001b[0;34m(args, constructor)\u001b[0m\n\u001b[1;32m 1402\u001b[0m args[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcolor\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 1403\u001b[0m \u001b[38;5;66;03m# now that things have been prepped, we do the systematic rewriting of `args`\u001b[39;00m\n\u001b[0;32m-> 1405\u001b[0m df_output, wide_id_vars \u001b[38;5;241m=\u001b[39m \u001b[43mprocess_args_into_dataframe\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1406\u001b[0m \u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwide_mode\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvar_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue_name\u001b[49m\n\u001b[1;32m 1407\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1409\u001b[0m \u001b[38;5;66;03m# now that `df_output` exists and `args` contains only references, we complete\u001b[39;00m\n\u001b[1;32m 1410\u001b[0m \u001b[38;5;66;03m# the special-case and wide-mode handling by further rewriting args and/or mutating\u001b[39;00m\n\u001b[1;32m 1411\u001b[0m \u001b[38;5;66;03m# df_output\u001b[39;00m\n\u001b[1;32m 1413\u001b[0m count_name \u001b[38;5;241m=\u001b[39m _escape_col_name(df_output, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcount\u001b[39m\u001b[38;5;124m\"\u001b[39m, [var_name, value_name])\n",
+ "File \u001b[0;32m/usr/local/lib/python3.8/dist-packages/plotly/express/_core.py:1207\u001b[0m, in \u001b[0;36mprocess_args_into_dataframe\u001b[0;34m(args, wide_mode, var_name, value_name)\u001b[0m\n\u001b[1;32m 1205\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m argument \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mindex\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 1206\u001b[0m err_msg \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m To use the index, pass it in directly as `df.index`.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m-> 1207\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(err_msg)\n\u001b[1;32m 1208\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m length \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(df_input[argument]) \u001b[38;5;241m!=\u001b[39m length:\n\u001b[1;32m 1209\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 1210\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAll arguments should have the same length. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1211\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe length of column argument `df[\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m]` is \u001b[39m\u001b[38;5;132;01m%d\u001b[39;00m\u001b[38;5;124m, whereas the \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1218\u001b[0m )\n\u001b[1;32m 1219\u001b[0m )\n",
+ "\u001b[0;31mValueError\u001b[0m: Value of 'x' is not the name of a column in 'data_frame'. Expected one of ['country', 'continent', 'year', 'lifeExp', 'pop', 'gdpPercap', 'iso_alpha', 'iso_num'] but received: system"