{ "cells": [ { "cell_type": "markdown", "id": "4491e638-15f8-4074-aaba-9cdd6c75d86b", "metadata": {}, "source": [ "---\n", "## MT94 - P2026\n", "### Chapitre Problèmes non linéaires 1 (F. De Vuyst)\n", "### TP n° 1 - 19 mars 2026\n", "---" ] }, { "cell_type": "markdown", "id": "dd4dfe4c-fd95-4b4d-986a-36a49a0dc453", "metadata": {}, "source": [ "#### Exercice 1 - Dichotomie\n", "\n", "Ecrire une fonction\n", "\n", "```\n", "function [xstar, n] = dichotomie(f, a, b, tol)\n", "```\n", "\n", "qui retourne une solution approchée $x^\\star$ de $f(x)=0$ dans l'intervalle $[a,b]$ sous la condition de sortie $|f(x)|< \\text{tol}$. On retournera aussi le nombre d'itérations $n$ nécessaire pour atteindre la condition de sortie. \n", "\n", "On retournera un message d'erreur si $f(a).f(b)\\geq 0$. " ] }, { "cell_type": "code", "execution_count": 131, "id": "8ff75e0e-83ea-4bfd-aae2-df8537d0ffa1", "metadata": {}, "outputs": [], "source": [ "function [xstar, n] = dichotomie(f, a, b, tol)\n", " // Verif si f(a).f(b) < 0.0 :\n", " if (f(a)*f(b)>=0) then\n", " error(\"Dichotomie : f(a)*f(b)>=0. Stopped\")\n", " end\n", " n = 0\n", " xG = a\n", " xD = b\n", " xM = 0.5*(xG+xD)\n", "\n", " while (abs(f(xM)) > tol)\n", " if (f(xM)*f(xD)<0.0) then\n", " xG = xM\n", " else\n", " xD = xM\n", " end\n", " xM = 0.5*(xG+xD)\n", " n = n + 1\n", " end\n", " xstar = xM\n", "endfunction" ] }, { "cell_type": "markdown", "id": "38c37d70-54d4-4b20-b5bd-14dce49ba887", "metadata": {}, "source": [ "Appliquer à\n", "\n", "$$\n", "f(x) = e^x - 2.\n", "$$\n", "\n", "avec $a=0$ et $b=1$, et vérifier que $x^\\star$ est proche de $\\ln(2)$.\n", "\n", "Comparer $n$ à l'estimation du cours\n", "\n", "$$\n", "n = \\text{int}\\Big[ \\log_2(\\frac{b-a}{\\text{tol}}) \\Big] + 1.\n", "$$" ] }, { "cell_type": "code", "execution_count": 132, "id": "6489914b-582c-4d43-a831-050375d95e49", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " xstar = \n", " 0.6931472\n", " n = \n", " 38.\n", "[xstar, n] = 6.931472e-01, \t 38\n", "|xstar - ln(2)| = 1.724176e-13 " ] } ], "source": [ "function y = f(x)\n", " y = exp(x) - 2.0\n", "endfunction\n", "\n", "a = 0.0;\n", "b = 1.0;\n", "tol = 1e-12;\n", "\n", "[xstar, n] = dichotomie(f, a, b, tol)\n", "\n", "mtlb_fprintf(\"[xstar, n] = %e, \\t %d\\n\", xstar, n);\n", "mtlb_fprintf(\"|xstar - ln(2)| = %e \\n\", abs(xstar - log(2)));" ] }, { "cell_type": "code", "execution_count": 37, "id": "35a14107-0cb1-4d04-a762-6e4db01b8188", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " n_estim = \n", " 40." ] } ], "source": [ "n_estim = int(log((b-a)/tol) / log(2)) + 1" ] }, { "cell_type": "markdown", "id": "812d8a48-de98-4703-9fca-b937b23d24e1", "metadata": {}, "source": [ "---\n", "#### Exercice 2 - Point fixe, Newton, sécante et autres\n", "#### Mesure expérimentale de la vitesse de convergence" ] }, { "cell_type": "markdown", "id": "7b3dfe74-02d6-4742-b7d4-921fce918052", "metadata": {}, "source": [ "#### a) Méthode de point fixe de Picard\n", "\n", "Soit $g(x) = \\dfrac{x+2}{x+1}$. Vérifier sur papier que les points fixes de $g$ vérifient\n", "$f(x) = x^2-2 = 0$.\n", "\n", "Ecrire une fonction `Scilab`\n", "\n", "```\n", "function [xstar, n, err] = pointFixe(g, x0, tol, nmax)\n", "```\n", "\n", "qui met en oeuvre la méthode de point fixe de Picard\n", "\n", "$$\n", "x_{n+1} = g(x_n)\n", "$$\n", "\n", "partant de $x_0$, avec un nombre maximum d'itérations `nmax` (en cas de non-convergence), un critère d'arrêt\n", "\n", "$$\n", "|x_{n} - g(x_n)| < \\text{tol},\n", "$$\n", "\n", "et qui retourne le point fixe $x_n$ à l'itération de sortie $n$, ainsi que la résidu\n", "$\\text{err} = |x_{n} - g(x_n)|$. Appliquer à la fonction $g(x) = \\dfrac{x+2}{x+1}$\n", "en partant de $x_0=2$." ] }, { "cell_type": "code", "execution_count": 133, "id": "3f3e3a09-90f2-4e7e-83d8-95451748b867", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Attention : redéfinition de la fonction pointFixe . Utiliser funcprot(0) pour éviter ce message" ] } ], "source": [ "function [xstar, n, err] = pointFixe(g, x0, tol, nmax)\n", " xn = x0\n", " n = 0\n", " while ( abs(g(xn) - xn) > tol ) && (n tol ) && (n tol ) && (n tol ) && (n0$. En passant au logarithme, on a\n", "\n", "$$\n", "\\ln(e_{n+1}) \\sim \\ln(C) + p\\, \\ln(e_n).\n", "$$\n", "\n", "Notant $w_n = \\ln(e_n)$ et $K=\\ln(C)$, on a donc\n", "\n", "$$\n", "w_{n+1} = K + p\\, w_n.\n", "$$\n", "\n", "En déduire une méthodologie pour estimer numériquement $p$.\n", "\n", "Appliquer cette méthodologique pour évaluer $p$ pour les trois méthodes respectives : méthode de point fixe, méthode de Newton, et méthode de sécante.\n", "\n", "ps : on peut montrer théoriquement que la vitesse de convergence de la méthode de la sécante est le nombre d'or $\\phi = \\dfrac{1+\\sqrt{5}}{2}\\approx 1.62~!!$" ] }, { "cell_type": "code", "execution_count": 139, "id": "c29cac1e-a2dc-4ef7-bd3d-55ad8ddc77dd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ordre Point fixe = 0.9976" ] }, { "data": { "image/svg+xml": [ "0-10-12-8-6-4-2-11-9-7-5-3-10-10-14-12-8-6-4-2" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "xstar = sqrt(2);\n", "w = log10(abs(xstor_PF - xstar));\n", "X = w(1:$-1);\n", "Y = w(2:$);\n", "plot(X, Y, '.-')\n", "xgrid()\n", "\n", "[p, K, sig] = reglin(X, Y);\n", "\n", "mtlb_fprintf(\"\\nOrdre Point fixe = %8.4f\\n\", p);" ] }, { "cell_type": "markdown", "id": "0b33fc83-e018-4ccd-8501-c1304f6ee190", "metadata": {}, "source": [ "---\n", "#### Newton\n", "---\n" ] }, { "cell_type": "code", "execution_count": 140, "id": "23d43890-1a73-4177-9c84-d95c650a402d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Attention : redéfinition de la fonction Newton . Utiliser funcprot(0) pour éviter ce message\n", " xstar = \n", " 1.4142136\n", " n = \n", " 5.\n", " residu = \n", " 4.441D-16\n", " xstor_Newton = [1x6 double]\n", " 2. 1.5 1.4166667 1.4142157 1.4142136 1.4142136\n", "\n", "Ordre Newton = 1.9882" ] }, { "data": { "image/svg+xml": [ "0-10-12-8-6-4-2-11-9-7-5-3-10-20-10-15-5" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "function [xstar, n, residu, xstor] = Newton(ffprim, x0, tol, nmax)\n", " xn = x0\n", " n = 0\n", " xstor = xn\n", " [fx, fprimx] = ffprim(xn)\n", " while ( abs(fx) > tol ) && (n0-8-6-4-2-7-5-3-10-10-14-12-8-6-4-2" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "function [xstar, n, residu, xstor] = Secante(f, x0, x1, tol, nmax)\n", " xnm1 = x0\n", " xn = x1 \n", " xstor = xn\n", " n = 0\n", " while ( abs(f(xn)) > tol ) && (n0-10-12-8-6-4-2-11-9-7-5-3-10-20-10-15-5Point fixeNewtonSecante" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plot(X, Y, '.-r')\n", "plot(X_Newton, Y_Newton, '.-g')\n", "plot(X_secante, Y_secante, '.-b')\n", "xlabel('$e_n$')\n", "ylabel('$e_{n+1}$')\n", "legend(['Point fixe','Newton','Secante'], 4)\n", "xgrid()" ] }, { "cell_type": "markdown", "id": "baeeaaa5-9b7e-49fd-addc-e8c3af609c75", "metadata": {}, "source": [ "#### Variante : méthode combinée Newton-Secante\n", "\n", "\\begin{align}\n", "& \\hat x_{n+1} = x_n - \\frac{f(x_n)}{f'(x_n)}, \\\\[1.3ex]\n", "& x_{n+1} = x_{n} - \\frac{\\hat x_{n+1}-x_n}{f(\\hat x_{n+1})-f(x_n)}\\, f(x_{n}).\n", "\\end{align}" ] }, { "cell_type": "code", "execution_count": 143, "id": "2635433e-fe33-4f4d-9533-deb4692828a6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " xstar = \n", " 1.4142136\n", " n = \n", " 5.\n", " residu = \n", " 4.441D-16\n", " xstor_NewtonSecante = [1x6 double]\n", " 2. 1.5 1.4166667 1.4142157 1.4142136 1.4142136" ] }, { "data": { "image/svg+xml": [ "0-10-12-8-6-4-2-11-9-7-5-3-10-10-12-8-6-4-2" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "function [xstar, n, residu, xstor] = NewtonSecante(ffprim, x0, tol, nmax)\n", " xn = x0\n", " n = 0\n", " xstor = xn\n", " [fn, fprimn] = ffprim(xn)\n", "\n", " while ( abs(fx) > tol ) && (n0-6-4-2-5-3-1-5.5-4.5-3.5-2.5-1.5-0.50-10-12-8-6-4-2" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "w = log10(abs(xstor_NewtonSecante - sqrt(2)));\n", "X_NewtonSecante = w(1:$-2);\n", "Y_NewtonSecante = w(2:$-1);\n", "plot(X_NewtonSecante, Y_NewtonSecante, '.-')\n", "xgrid()\n", "\n", "[p, K, sig] = reglin(X_NewtonSecante, Y_NewtonSecante);\n", "\n", "mtlb_fprintf(\"\\nOrdre Newton-Secante = %8.4f\\n\", p);" ] }, { "cell_type": "code", "execution_count": null, "id": "03b7edb7-4413-4d80-bd56-0d11a30ebfec", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Scilab", "language": "scilab", "name": "scilab" }, "language_info": { "file_extension": ".sci", "help_links": [ { "text": "MetaKernel Magics", "url": "https://metakernel.readthedocs.io/en/latest/source/README.html" } ], "mimetype": "text/x-scilab", "name": "scilab", "version": "0.10.2" } }, "nbformat": 4, "nbformat_minor": 5 }