Refactor code in numerical methods notebooks

- Updated import order in Point_Fixe.ipynb for consistency.
- Changed lambda functions to regular function definitions for clarity in Point_Fixe.ipynb.
- Added numpy import in TP1_EDO_EulerExp.ipynb, TP2_Lokta_Volterra.ipynb, and TP3_Convergence.ipynb for better readability.
- Modified for loops in TP1_EDO_EulerExp.ipynb and TP2_Lokta_Volterra.ipynb to include strict=False for compatibility with future Python versions.
This commit is contained in:
2025-09-01 16:14:53 +02:00
parent dfee405ea0
commit 8cf328e18a
31 changed files with 177 additions and 156 deletions

View File

@@ -9,8 +9,8 @@
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'retina'\n",
"\n",
"import numpy as np # pour les numpy array\n",
"import matplotlib.pyplot as plt # librairie graphique\n",
"import numpy as np # pour les numpy array\n",
"from scipy.integrate import odeint # seulement odeint"
]
},

View File

@@ -19,8 +19,8 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt"
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{

View File

@@ -19,10 +19,10 @@
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from scipy.special import roots_legendre\n",
"from scipy.integrate import quad\n",
"import matplotlib.pyplot as plt"
"from scipy.special import roots_legendre"
]
},
{
@@ -710,14 +710,14 @@
"\n",
"for N in range(1, 11):\n",
" approx_errors = []\n",
" for k in [x for x in range(0, 11, 2)]:\n",
" for k in list(range(0, 11, 2)):\n",
" I_approx = gauss(lambda x: f(x, k), N)\n",
" I_exact = 2 / (k + 1) if k % 2 == 0 else 0\n",
" approx_error = np.abs(I_approx - I_exact)\n",
" approx_errors.append(approx_error)\n",
" print(\n",
" \"{:5d} | \".format(N)\n",
" + \" \".join(\"{:.3f} \".format(e) for e in approx_errors)\n",
" f\"{N:5d} | \"\n",
" + \" \".join(f\"{e:.3f} \" for e in approx_errors)\n",
" )"
]
},
@@ -768,14 +768,14 @@
"\n",
"for N in range(1, 11):\n",
" approx_errors = []\n",
" for k in [x for x in range(0, 11, 2)]:\n",
" for k in list(range(0, 11, 2)):\n",
" I_approx = fejer(lambda x: f(x, k), N)\n",
" I_exact = 2 / (k + 1) if k % 2 == 0 else 0\n",
" approx_error = np.abs(I_approx - I_exact)\n",
" approx_errors.append(approx_error)\n",
" print(\n",
" \"{:5d} | \".format(N)\n",
" + \" \".join(\"{:.3f} \".format(e) for e in approx_errors)\n",
" f\"{N:5d} | \"\n",
" + \" \".join(f\"{e:.3f} \" for e in approx_errors)\n",
" )"
]
},

View File

@@ -22,8 +22,8 @@
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'retina'\n",
"\n",
"import numpy as np # pour les numpy array\n",
"import matplotlib.pyplot as plt # librairie graphique"
"import matplotlib.pyplot as plt # librairie graphique\n",
"import numpy as np # pour les numpy array"
]
},
{
@@ -331,7 +331,8 @@
}
],
"source": [
"f = lambda x: 1 / (1 + x**2)\n",
"def f(x):\n",
" return 1 / (1 + x**2)\n",
"a, b = -5, 5\n",
"xx = np.linspace(a, b, 200)\n",
"\n",
@@ -372,7 +373,8 @@
}
],
"source": [
"f = lambda x: 1 / (1 + x**2)\n",
"def f(x):\n",
" return 1 / (1 + x**2)\n",
"a, b = -5, 5\n",
"xx = np.linspace(a, b, 200)\n",
"\n",

File diff suppressed because one or more lines are too long

View File

@@ -20,8 +20,8 @@
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'retina'\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
@@ -63,11 +63,16 @@
},
"outputs": [],
"source": [
"f1 = lambda x: np.exp(x) - 1 - x\n",
"f2 = lambda x: x - np.sin(x)\n",
"f3 = lambda x: x + np.sin(x)\n",
"f4 = lambda x: x + np.cos(x) - 1\n",
"f5 = lambda x: x - np.cos(x) + 1"
"def f1(x):\n",
" return np.exp(x) - 1 - x\n",
"def f2(x):\n",
" return x - np.sin(x)\n",
"def f3(x):\n",
" return x + np.sin(x)\n",
"def f4(x):\n",
" return x + np.cos(x) - 1\n",
"def f5(x):\n",
" return x - np.cos(x) + 1"
]
},
{

View File

@@ -100,8 +100,8 @@
],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"\n",
"# Fonction f définissant l'EDO\n",
@@ -189,8 +189,8 @@
],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"\n",
"# Fonction f définissant l'EDO\n",
@@ -367,7 +367,7 @@
"T = 1\n",
"y0 = 1\n",
"\n",
"for f, uex in zip([f1, f2], [uex1, uex2]):\n",
"for f, uex in zip([f1, f2], [uex1, uex2], strict=False):\n",
" plt.figure()\n",
" t = np.arange(0, 1, 1e-3)\n",
" y = uex(t, y0)\n",

View File

@@ -92,14 +92,14 @@
],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"\n",
"# Fonction F définissant l'EDO\n",
"def F(Y):\n",
" x = Y[0]\n",
" y = Y[1]\n",
" Y[0]\n",
" Y[1]\n",
" A = np.array([[0, 1], [-2, -3]])\n",
" return np.dot(A, Y)\n",
"\n",
@@ -132,7 +132,7 @@
"## Représentation des solutions pour chaque valeur de la donnée initiale\n",
"tt = np.linspace(-10, 10, 100)\n",
"t0 = tt[0]\n",
"for x, y in zip([1, -2, 0, 1, 3], [2, -2, -4, -2, 4]):\n",
"for x, y in zip([1, -2, 0, 1, 3], [2, -2, -4, -2, 4], strict=False):\n",
" sol = uex(tt, t0, [x, y])\n",
" plt.plot(sol[0], sol[1], label=f\"$((x0, y0) = ({x}, {y})$\")\n",
" plt.scatter(x, y)\n",

View File

@@ -73,8 +73,8 @@
"outputs": [],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"\n",
"## Question 1\n",
@@ -746,8 +746,9 @@
"outputs": [],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"# Question 3\n",
"\n",
"\n",

View File

@@ -358,7 +358,7 @@
"H, E = np.array(H), np.array(E)\n",
"\n",
"plt.xlabel(\"$h$\")\n",
"plt.ylabel(\"$\\max_{j=0,\\dots,M+1}|u(x_j)-u_j|$\")\n",
"plt.ylabel(r\"$\\max_{j=0,\\dots,M+1}|u(x_j)-u_j|$\")\n",
"plt.legend(fontsize=7)\n",
"plt.title(\n",
" \"Différence en valeur absolue entre la solution exacte et la solution approchée\"\n",
@@ -589,7 +589,7 @@
" H.append(h)\n",
"\n",
"plt.xlabel(\"$h$\")\n",
"plt.ylabel(\"$\\max_{j=0,\\dots,M+1}|u(x_j)-u_j|$\")\n",
"plt.ylabel(r\"$\\max_{j=0,\\dots,M+1}|u(x_j)-u_j|$\")\n",
"plt.legend(fontsize=7)\n",
"plt.title(\n",
" \"Différence en valeur absolue entre la solution exacte et la solution approchée\"\n",
@@ -833,7 +833,7 @@
" H.append(h)\n",
"\n",
"plt.xlabel(\"$h$\")\n",
"plt.ylabel(\"$\\max_{j=0,\\dots,M+1}|u(x_j)-u_j|$\")\n",
"plt.ylabel(r\"$\\max_{j=0,\\dots,M+1}|u(x_j)-u_j|$\")\n",
"plt.legend(fontsize=7)\n",
"plt.title(\n",
" \"Différence en valeur absolue entre la solution exacte et la solution approchée\"\n",

View File

@@ -21,9 +21,10 @@
}
],
"source": [
"import numpy as np\n",
"import itertools\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import itertools"
"import numpy as np"
]
},
{
@@ -139,7 +140,7 @@
" return S * 2 / (self.Ntot**2)\n",
"\n",
" def simuler(self, T=400, move_satisfaits=True):\n",
" for t in range(1, int((1 - self.p) * self.M**2 * T)):\n",
" for _t in range(1, int((1 - self.p) * self.M**2 * T)):\n",
" agents = [\n",
" (i, j)\n",
" for i, row in enumerate(self.grille)\n",

View File

@@ -9,8 +9,8 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import scipy.stats as stats"
]
},

View File

@@ -9,11 +9,11 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"import scipy.stats as stats\n",
"import scipy.special as sp\n",
"import matplotlib.pyplot as plt\n",
"import scipy.optimize as opt"
"import numpy as np\n",
"import scipy.optimize as opt\n",
"import scipy.special as sp\n",
"import scipy.stats as stats"
]
},
{