{ "cells": [ { "cell_type": "markdown", "id": "e1b2cd28-f333-4f8b-b01d-97068fceedf3", "metadata": { "tags": [] }, "source": [ "## MT09 - TP1 - Automne 2024\n", "### Prise en main de Python/Numpy/Matplotlib (pas d'évaluation)\n", "#### Arrays\n", "1. Éxécutez les différents scripts python ci-dessous (bouton \"play\" ou touches Shift+Enter pour exécuter les cellules) et interprétez les résultats:" ] }, { "cell_type": "code", "execution_count": 22, "id": "8e75fcae-4bea-4969-a465-2c95ca6eaf1e", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4 5 6 7 8 9]\n", "[1 2 3 4 5 6]\n", "[9 8 7 6 5 4 3 2 1 0]\n", "[9 8 7 6 5 4 3 2 1]\n", "[0. 0.5 1. 1.5 2. 2.5 3. 3.5]\n", "[-10. -7.77777778 -5.55555556 -3.33333333 -1.11111111\n", " 1.11111111 3.33333333 5.55555556 7.77777778 10. ]\n" ] } ], "source": [ "import numpy as np\n", "x = np.arange(10)\n", "print(x)\n", "y = np.arange(1,7)\n", "print(y)\n", "z = np.arange(10)[::-1]\n", "print(z)\n", "w = x[10:0:-1]\n", "print(w)\n", "t = np.arange(0, 4, 0.5)\n", "print(t)\n", "a = -10; b = 10;\n", "u = np.linspace(a, b, 10);\n", "print(u)" ] }, { "cell_type": "code", "execution_count": null, "id": "5445b2f4-6213-49b9-b473-2f28155357f8", "metadata": { "tags": [] }, "outputs": [], "source": [ "help(np.linspace)" ] }, { "cell_type": "code", "execution_count": null, "id": "1c6b7f22-8900-4cfc-aad8-fc8d8ef84b95", "metadata": {}, "outputs": [], "source": [ "for s in u:\n", " print(s)\n", " print('hello')\n", "print('fin boucle')\n", "# Attention : bien respecter les tabulations pour la portée du 'for\"" ] }, { "cell_type": "code", "execution_count": null, "id": "8791b41c-2c02-4af4-a1a3-cbf897e2651b", "metadata": { "tags": [] }, "outputs": [], "source": [ "for s in u:\n", " if (s>0):\n", " print(s)\n", "print(\"Done !\\n\");" ] }, { "cell_type": "markdown", "id": "6d3cec60-a356-4de0-8221-c920bba3a8b6", "metadata": {}, "source": [ "#### Opérations sur vecteurs\n", "\n", "2. Éxécutez les différents codes python ci-dessous (Shift+Enter pour exécuter) et analysez les résultats obtenus:" ] }, { "cell_type": "code", "execution_count": null, "id": "b770ac94-6d48-4363-a9d6-8648dd15ca61", "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "x = np.array([1, 2, 3], dtype=np.float64)\n", "y = np.array([3, -2, 1], dtype=np.float64)" ] }, { "cell_type": "code", "execution_count": null, "id": "606697ab-90c4-4dbf-aaef-d6214b5d9f37", "metadata": {}, "outputs": [], "source": [ "print(x*y)" ] }, { "cell_type": "code", "execution_count": null, "id": "4fad0f0c-c81f-469a-b79f-ddaec78fb360", "metadata": {}, "outputs": [], "source": [ "print(2*x)" ] }, { "cell_type": "code", "execution_count": null, "id": "0c46b7f5-b19e-4db1-bf51-21c04c191be6", "metadata": {}, "outputs": [], "source": [ "print(y.T @x)" ] }, { "cell_type": "code", "execution_count": null, "id": "310cc461-38be-4015-b636-8c52c2cdaa52", "metadata": {}, "outputs": [], "source": [ "print(y@x)\n", "print(x @ y.T)" ] }, { "cell_type": "code", "execution_count": null, "id": "4d961ddf-a4f7-4703-9e93-d5f3ff1fe7c6", "metadata": {}, "outputs": [], "source": [ "x.shape=(3,1); y.shape=(3,1);\n", "print(x)\n", "print(y)\n", "print(x.T)" ] }, { "cell_type": "code", "execution_count": null, "id": "9f249f4b-39a7-44f6-bf14-793866682766", "metadata": {}, "outputs": [], "source": [ "z = y.T @ x\n", "print(z)\n", "z.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "c4ccdace-be3d-4868-84e0-e29aa7699eb8", "metadata": { "tags": [] }, "outputs": [], "source": [ "x = np.array([[1,2,3]]).T\n", "y = np.array([[4,5,6]]).T\n", "print(x.T@y)\n", "print( float(x.T@y) )\n", "print(x@y.T)\n", "A = np.array([[2,-1,0],[-1,2,-1],[0,-1,2]])\n", "print(x.T@A@x)\n", "x = np.array([1,2,3])\n", "print(x@A@x)" ] }, { "cell_type": "code", "execution_count": null, "id": "6f8a186b-c42a-4c50-a298-4881bb7a6fae", "metadata": {}, "outputs": [], "source": [ "print(x @ y.T)" ] }, { "cell_type": "markdown", "id": "898e7836-79b2-48da-9ae7-70043876fd73", "metadata": {}, "source": [ "#### ! Références et copies\n", "3. Éxécuter le code python ci-dessous et comprendre ce qui se passe:" ] }, { "cell_type": "code", "execution_count": null, "id": "63d71a80-dcd7-423d-a587-166c711d3178", "metadata": {}, "outputs": [], "source": [ "x = np.array([1, 2, 3], dtype=np.float64)\n", "y = x\n", "y[0] = 5\n", "print(y)\n", "print(x,\"\\n\")" ] }, { "cell_type": "code", "execution_count": null, "id": "a829894d-31ad-4293-b01a-23d7822ed4b4", "metadata": { "tags": [] }, "outputs": [], "source": [ "z = x + 0\n", "z[1] = 6\n", "print(z)\n", "print(x,\"\\n\")" ] }, { "cell_type": "code", "execution_count": null, "id": "2c3c2b11-f6f7-4b0e-8452-f75745833f25", "metadata": { "tags": [] }, "outputs": [], "source": [ "x = np.array([1, 2, 3], dtype=np.float64)\n", "y = 2*x\n", "print(y)\n", "print(x,\"\\n\")\n", "z = x.copy()\n", "z[0] = 5\n", "print(z)\n", "print(x)" ] }, { "cell_type": "markdown", "id": "4b755297-07b0-4381-bb21-90db676f4d7d", "metadata": {}, "source": [ "#### Matrices, multiplications de matrices\n", "4. Éxécutez les différents codes python ci-dessous et analysez les résultats:" ] }, { "cell_type": "code", "execution_count": null, "id": "06f39ebe-af65-4837-9278-ae51bfb4611b", "metadata": {}, "outputs": [], "source": [ "x = np.array([1, 2, 3], dtype=np.float64) # tableau (array 1d)\n", "print(x.shape)\n", "print(x) # tableau\n", "print(x[:, np.newaxis]) # matrice (vecteur colonne)\n", "x = x[:, np.newaxis]\n", "print(x.shape)" ] }, { "cell_type": "code", "execution_count": 16, "id": "c5983b77-90fa-4900-abe7-e5d281c1886c", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'np' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[16], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m A \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([[\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m], [\u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m4\u001b[39m], [\u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m6\u001b[39m], [\u001b[38;5;241m7\u001b[39m, \u001b[38;5;241m8\u001b[39m]], dtype\u001b[38;5;241m=\u001b[39mnp\u001b[38;5;241m.\u001b[39mfloat64);\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(A)\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(A\u001b[38;5;241m.\u001b[39mndim)\n", "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" ] } ], "source": [ "A = np.array([[1, 2], [3, 4], [5, 6], [7, 8]], dtype=np.float64);\n", "print(A)\n", "print(A.ndim)\n", "print(A.size)\n", "r, c = A.shape\n", "print(r, \" \", c)\n", "A.shape=(8,)\n", "print(A)\n", "newarr = A.reshape(4, 2)\n", "print(newarr)\n", "A = A.reshape(4, 2)" ] }, { "cell_type": "code", "execution_count": null, "id": "81d68946-9ff5-4c7a-afd4-2638d070d0ea", "metadata": { "tags": [] }, "outputs": [], "source": [ "B = A.T; # transposee\n", "print(B);" ] }, { "cell_type": "code", "execution_count": null, "id": "074482f2-a4db-4a8c-b94e-ce99dbf67ea7", "metadata": { "tags": [] }, "outputs": [], "source": [ "print(A@B)" ] }, { "cell_type": "code", "execution_count": null, "id": "168b4294-6083-47e1-8463-8f109f08bbca", "metadata": { "tags": [] }, "outputs": [], "source": [ "print(B@A)" ] }, { "cell_type": "code", "execution_count": null, "id": "1f6b88ae-016b-4154-a7a8-0a433138fe8b", "metadata": { "tags": [] }, "outputs": [], "source": [ "print(A*B)" ] }, { "cell_type": "code", "execution_count": null, "id": "bc852186-a653-45cc-8471-5c580c4d83cd", "metadata": { "tags": [] }, "outputs": [], "source": [ "S = A @ A.T;\n", "print(S)" ] }, { "cell_type": "code", "execution_count": null, "id": "f3ab74e4-1850-4f82-867b-47ed0d9ac1c0", "metadata": {}, "outputs": [], "source": [ "A = np.ones((3,4)); print(A)" ] }, { "cell_type": "code", "execution_count": null, "id": "31668f85-5870-48cd-b26f-dbb5105f7081", "metadata": {}, "outputs": [], "source": [ "B = np.zeros((2,3)); print(B)" ] }, { "cell_type": "code", "execution_count": null, "id": "59436841-ded9-40fe-8d30-2320d0ad1b2f", "metadata": {}, "outputs": [], "source": [ "C = np.eye(4); print(C)" ] }, { "cell_type": "code", "execution_count": null, "id": "ecee8023-56a0-492c-8a6f-bd88952099cf", "metadata": {}, "outputs": [], "source": [ "D = np.diag(np.array([4, 5, 6])); print(D)" ] }, { "cell_type": "code", "execution_count": null, "id": "6b2df91f-0a55-4730-a0b2-4ebb8b30af01", "metadata": {}, "outputs": [], "source": [ "E = np.diag(np.array([4, 5, 6]), 0) + np.diag(np.array([-1, -1]), 1); print(E)" ] }, { "cell_type": "markdown", "id": "af6cd88c-3588-4162-a75c-a5a5ad47ea96", "metadata": {}, "source": [ "#### Stacking\n", "5. Les fonctions ```np.hstack()``` et ```np.vstack()``` sont utiles pour empiler des veteurs dans une matrice. Exécutez les scripts suivants :" ] }, { "cell_type": "code", "execution_count": null, "id": "e6bc26df-e290-4ac6-9d6e-f90fe7f82cdc", "metadata": { "tags": [] }, "outputs": [], "source": [ "# vecteurs colonnes\n", "q1 = np.array([[1,2,3]]).T\n", "q2 = np.array([[4,5,6]]).T\n", "q3 = np.array([[7,8,9]]).T\n", "Q = np.hstack((q1,q2,q3))\n", "print(Q, \"\\n\")\n", "# vecteurs lignes\n", "r1 = np.array([1,2,3])\n", "r2 = np.array([4,5,6])\n", "r3 = np.array([7,8,9])\n", "R = np.vstack((r1,r2,r3))\n", "print(R)" ] }, { "cell_type": "markdown", "id": "ee1ebcf0-2366-4c4b-a85b-ad2ebabac405", "metadata": {}, "source": [ "#### Accesseurs\n", "6. Éxécutez les scripts python suivants :" ] }, { "cell_type": "code", "execution_count": null, "id": "f919fdca-52d2-4c61-a9ea-894465fd37eb", "metadata": {}, "outputs": [], "source": [ "A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], dtype=np.float64);\n", "print(A)\n", "print(A[1:3,1:3])\n", "print(A[1,:])\n", "print(A[:,2]) # ! tableau 1d\n", "print(A[:,2:3]) # ! slicing\n", "A[-1,-1] = 100\n", "print(A)\n", "print(A[2,::-1])\n" ] }, { "cell_type": "markdown", "id": "c9809a30-7211-46b7-818d-3aa397de839f", "metadata": {}, "source": [ "#### Module d'algèbre linéaire\n", "7. Le module d'algèbre linéaire `linalg` de `numpy` donne accès à différents outils et méthodes usuelles telles que la résolution de systèmes linéaires, la définition de norme, le conditionnement de matrices, le calcul de valeurs et vecteurs propres, le déterminant, l'inverse de matrices, etc. Testez les différents scripts suivants:" ] }, { "cell_type": "code", "execution_count": null, "id": "3ed9c6d4-2556-4439-af05-64de517488f2", "metadata": {}, "outputs": [], "source": [ "import numpy.linalg as la\n", "b = np.array([1, 1, 1], dtype=np.float64)\n", "A = np.array([[3, -1, -1], [-1, 3, -1], [-1, -1, 3]], dtype=np.float64)\n", "x = la.solve(A, b)\n", "# Verif\n", "print(x[:,np.newaxis], A@x-b)" ] }, { "cell_type": "code", "execution_count": null, "id": "e85a84da-0114-49cd-8a65-b1440c2f99cb", "metadata": {}, "outputs": [], "source": [ "D, V = la.eig(A)\n", "print(D)\n", "print(V)\n", "print(V.T @ V) " ] }, { "cell_type": "code", "execution_count": null, "id": "b3d995d4-a725-41b9-bf58-04a26937b6c0", "metadata": {}, "outputs": [], "source": [ "print(la.inv(A), '\\n', A @ la.inv(A))" ] }, { "cell_type": "code", "execution_count": null, "id": "dfbc1599-5620-45b9-8ac9-110ff31643ff", "metadata": {}, "outputs": [], "source": [ "print(la.norm(x))" ] }, { "cell_type": "code", "execution_count": null, "id": "8ff95720-df13-4b37-b9ce-6679e54919ab", "metadata": { "tags": [] }, "outputs": [], "source": [ "A = np.array([[1,0,0,0], [-1, 1, 0,0],[0, -1,1,0], [0,0,-1,1]]);\n", "print(A)\n", "C = la.inv(A)\n", "print(C)" ] }, { "cell_type": "markdown", "id": "122134b2-b7fe-4174-8537-a258b7a6099e", "metadata": {}, "source": [ "#### Fonctions\n", "8. En python, la définition de fonctions est simple. Exécutez les scripts ci-dessous:" ] }, { "cell_type": "code", "execution_count": null, "id": "bd07330f-ca69-471b-ac7f-f53174a24e3b", "metadata": {}, "outputs": [], "source": [ "def myFunction(x):\n", " fx = x * np.sin(np.pi *x) - np.exp(-x)\n", " return fx\n", "print(myFunction(3))" ] }, { "cell_type": "code", "execution_count": null, "id": "0db87bc2-01da-42ca-9147-77d149b5547f", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Avec 2 valeurs de sortie\n", "def myFun2(x):\n", " fx = x**3\n", " fprimx = 3*x**2\n", " return fx, fprimx\n", "f1, fprim1 = myFun2(1.0)\n", "print(f1, fprim1)" ] }, { "cell_type": "code", "execution_count": 18, "id": "8c1a3038-18b4-40ad-bc33-16afe1b5b3ed", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function fact in module __main__:\n", "\n", "fact(n)\n", " Fonction factorielle fact(n) = n!\n", "\n", "120\n" ] }, { "ename": "TypeError", "evalue": "('Argument non entier: ', )", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[18], line 12\u001b[0m\n\u001b[1;32m 10\u001b[0m help(fact)\n\u001b[1;32m 11\u001b[0m \u001b[38;5;28mprint\u001b[39m(fact(\u001b[38;5;241m5\u001b[39m))\n\u001b[0;32m---> 12\u001b[0m \u001b[38;5;28mprint\u001b[39m(fact(\u001b[38;5;241m2.3\u001b[39m))\n", "Cell \u001b[0;32mIn[18], line 4\u001b[0m, in \u001b[0;36mfact\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Fonction factorielle fact(n) = n!\"\"\"\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(n,\u001b[38;5;28mint\u001b[39m):\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mArgument non entier: \u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28mtype\u001b[39m(n))\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (n\u001b[38;5;241m==\u001b[39m\u001b[38;5;241m0\u001b[39m):\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m1\u001b[39m\n", "\u001b[0;31mTypeError\u001b[0m: ('Argument non entier: ', )" ] } ], "source": [ "def fact(n):\n", " \"\"\"Fonction factorielle fact(n) = n!\"\"\"\n", " if not isinstance(n,int):\n", " raise TypeError(\"Argument non entier: \", type(n))\n", " if (n==0):\n", " return 1\n", " else:\n", " return (n*fact(n-1))\n", "\n", "help(fact)\n", "print(fact(5))\n", "print(fact(2.3))" ] }, { "cell_type": "markdown", "id": "c97b9a9b-1274-46e4-84f5-4e65be297a12", "metadata": {}, "source": [ "#### Graphiques avec Matplotlib\n", "9. Le module Matplotlib donne accès à de nombreux outils de visualisation graphique. Dans MT09, nous nous limiterons essentiellement à l'utilisation de la commande `plot()`. Exécutez le script ci-dessous et faites varier les paramètres." ] }, { "cell_type": "code", "execution_count": null, "id": "2d06d880-58a0-4444-920c-fbd2084aa860", "metadata": { "tags": [] }, "outputs": [], "source": [ "def myfunc(x):\n", " return (np.sin(x**2) * np.exp(-x/2))\n", "\n", "import matplotlib.pyplot as plt\n", "\n", "Npt = 200\n", "x = np.linspace(0, 3*np.pi, Npt);\n", "plt.plot(x, myfunc(x), color='darkgreen', marker='^', linestyle='-',\n", " linewidth=2, markersize=4)\n", "plt.grid()\n", "plt.xlabel('x'); plt.ylabel('y=f(x)'); plt.title('A nice function')" ] }, { "cell_type": "markdown", "id": "5dc8d17e-f26e-4633-b6f7-dd199e12352f", "metadata": {}, "source": [ "### Exercice 1\n", "On définit la fonction\n", "$$\n", "f(x) = 2\\left( \\sqrt{x^2+1}-x \\right).\n", "$$\n", "On considère le vecteur\n", "$$\n", "x = [10^0\\ 10^1\\ 10^2\\ 10^3\\ ...\\ 10^{16}]\n", "$$\n", "Dans un graphique, tracer les différents points $(i, f(x_i))$, $i=0,...,16$. \n", "\n", "Affichez aussi le tableau des $f(x_i)$ avec `print()`. Le résultat vous paraît-il normal ?" ] }, { "cell_type": "markdown", "id": "aa1b65c4-cf2d-42c2-b2b8-a5bc5ce78238", "metadata": {}, "source": [ "En multipliant par la quantité conjuguée, on remarque que la fonction $f$ est équivalente à la fonction\n", "$$\n", "g(x) = \\frac{2}{\\sqrt{x^2+1}+x} \\quad (=f(x)).\n", "$$\n", "La fonction $f$ est équivalente à quelle fonction simple quand $x$ est grand ?\n", "Affichez le tableau des $g(x_i)$ avec `print()`. Que remarquez-vous ?\n", "Dans un autre graphique, tracer les différents points $(x_i, g(x_i))$, $i=0,...,16$ en échelle $\\log$\n", "(on utilisera pour cela la fonction `plt.loglog()`)." ] }, { "cell_type": "markdown", "id": "5d35ea4b-30f6-41d9-83cf-71a138443f02", "metadata": {}, "source": [ "Éxécutez les lignes suivantes. Qu'en concluez-vous ?" ] }, { "cell_type": "code", "execution_count": 33, "id": "ec417581-890a-4d4c-83cf-b1edc5e95fc3", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'f' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[33], line 5\u001b[0m\n\u001b[1;32m 3\u001b[0m ii \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marange(\u001b[38;5;241m17\u001b[39m); \u001b[38;5;66;03m#print(ii)\u001b[39;00m\n\u001b[1;32m 4\u001b[0m xint \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m10\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mii; \u001b[38;5;66;03m# print(xint**2+1)\u001b[39;00m\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(f(xint))\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(f(xfloat32))\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(f(x))\n", "\u001b[0;31mNameError\u001b[0m: name 'f' is not defined" ] } ], "source": [ "I = np.arange(0, 17, dtype=np.float64)\n", "xfloat32 = np.exp(I*np.log(10), dtype=np.float32)\n", "ii = np.arange(17); #print(ii)\n", "xint = 10**ii; # print(xint**2+1)\n", "print(f(xint))\n", "print(f(xfloat32))\n", "print(f(x))\n", "print(g(x))" ] }, { "cell_type": "markdown", "id": "83e58381-1ac8-4a9c-9ae8-ac409a71a0c3", "metadata": {}, "source": [ "### Exercice 2\n", "On peut approcher la valeur de la dérivée d''une fonction en un point $x_0$ \n", "par une formule dite de différences finies:\n", "\n", "$$\n", "\\Delta_h(x_0) = \\frac{f(x_0+h)-f(x_0)}{h}\\quad (\\approx f'(x_0))\n", "$$\n", "pour un pas $h\\neq 0$ assez petit. Pour $f(x)=\\sqrt{x}$ et $x_0=1$, on a $f'(x_0)=\\frac{1}{2}$.\n", "En python, on considère le vecteur de pas $h=(h_i)_i$ défini par\n", "\n", "$$\n", "h = [10^0\\ 10^{-1}\\ 10^{-2}\\ 10^{-3}\\ ...\\ 10^{-19}\\ 10^{-20}]\n", "$$\n", "et le vecteur d'erreur d'approximation $e_h=(e_{h,i})_i$ où\n", "\n", "$$\n", "e_{h,i}(x_0) = |\\Delta_{h_i}(x_0) - f'(x_0)|.\n", "$$\n", "\n", "Tracez un graphique qui trace les points $(h_i, e_{h_i}(x_0)$ en échelle log. Que constatez-vous ? Interprétez." ] }, { "cell_type": "code", "execution_count": null, "id": "bf9dcde9-f69a-4ff3-9d98-52bcf160df8c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c7aba128-2742-48c8-a83c-7d6e785475d3", "metadata": {}, "source": [ "### Exercice 3\n", "\n", "Soit une matrice $A\\in\\mathcal{M}_{n,n}$ et deux entiers $c$ et $l$. Écrire une fonction prodmatn(A, c, l) qui retourne la matrice $A_c x \\underline{A}_{l}$ (produit de la colonne $c$ de $A$ par la ligne $l$ de $A$)." ] }, { "cell_type": "code", "execution_count": null, "id": "453184a3-f6ad-4022-a3aa-1913d3202d7b", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.4" }, "toc-autonumbering": false, "toc-showcode": true, "toc-showmarkdowntxt": false }, "nbformat": 4, "nbformat_minor": 5 }