diff options
| author | marcellus <msimon_fr@hotmail.com> | 2025-06-13 17:14:39 +0200 |
|---|---|---|
| committer | marcellus <msimon_fr@hotmail.com> | 2025-06-13 17:14:39 +0200 |
| commit | ce772c048c1b6b91ec710986de451334e2290852 (patch) | |
| tree | 9682b06e69e1f47e6d5c47bd9f047fa3962977a4 /PVCM | |
| parent | 1ac965e92ac869e56c54c34e4b769ff5fdf2e1ea (diff) | |
| parent | 1f5fb2fbdf3b67fcb34eed5777d8f858349ed13d (diff) | |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'PVCM')
| -rw-r--r-- | PVCM/cama/fr/ma54 Gradient pour résoudre Ax = b -- Exercice.ipynb | 161 |
1 files changed, 114 insertions, 47 deletions
diff --git a/PVCM/cama/fr/ma54 Gradient pour résoudre Ax = b -- Exercice.ipynb b/PVCM/cama/fr/ma54 Gradient pour résoudre Ax = b -- Exercice.ipynb index 2a7cf27..46ec5a8 100644 --- a/PVCM/cama/fr/ma54 Gradient pour résoudre Ax = b -- Exercice.ipynb +++ b/PVCM/cama/fr/ma54 Gradient pour résoudre Ax = b -- Exercice.ipynb @@ -1,25 +1,4 @@ { - "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.10.12" - } - }, - "nbformat": 4, - "nbformat_minor": 4, "cells": [ { "cell_type": "markdown", @@ -41,27 +20,17 @@ }, { "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [], - "source": [ - "def solve_with_gradiant(A, b):\n", - " mu = 0.01\n" - ] - }, - { - "cell_type": "code", - "execution_count": 34, + "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[49, 13],\n", - " [13, 13]])" + "array([[0.15, 0.15],\n", + " [0.15, 0.55]])" ] }, - "execution_count": 34, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } @@ -71,21 +40,22 @@ "A = np.random.randint(10, size=(2,2))\n", "A = A + A.T\n", "A += np.diag(A.sum(axis=1))\n", + "A = A / np.sum(np.abs(A))\n", "A" ] }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([62, 26])" + "array([0.3, 0.7])" ] }, - "execution_count": 35, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -97,10 +67,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, "outputs": [], "source": [ + "def solve_with_gradiant(A, b):\n", + " mu = 0.01\n", + " xi = np.array(np.zeros(b.shape))\n", + " while True:\n", + " xn = xi - mu * (A @ xi - b)\n", + " if np.square(xn - xi).sum() < 1e-6 ** 2:\n", + " break\n", + " xi = xn\n", + " return xi" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0.9990519 , 1.00031603])" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ "solve_with_gradiant(A,b)" ] }, @@ -117,10 +115,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "array([0.9990519 , 1.00031603])" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def solve_with_gradiant_inertie(A, b):\n", + " mu = 0.01\n", + " i = 0.5\n", + " xi = np.array(np.zeros(b.shape))\n", + " while True:\n", + " xn = xi - mu * (A @ xi - b)\n", + " if np.square(xn - xi).sum() < 1e-6 ** 2:\n", + " break\n", + " xi = xn\n", + " return xi\n", + "solve_with_gradiant_inertie(A, b)" + ] }, { "cell_type": "markdown", @@ -173,10 +194,56 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "array([0.99999941, 0.99999941])" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def solve_with_gradiant_inertie_opti(A, b):\n", + " mu = 0.01\n", + " i = 0.5\n", + " xi = np.array(np.zeros(b.shape))\n", + " while True:\n", + " J = A @ xi - b\n", + " mu = np.dot(J, J) / np.dot(A @ J, J)\n", + " xn = xi - mu * (A @ xi - b)\n", + " if np.square(xn - xi).sum() < 1e-6 ** 2:\n", + " break\n", + " xi = xn\n", + " return xi\n", + "solve_with_gradiant_inertie_opti(A,b)" + ] } - ] -}
\ No newline at end of file + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.3" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} |
