From 320aedd827b0070f0699fe8bc0c20a790a267b69 Mon Sep 17 00:00:00 2001 From: marcellus Date: Wed, 11 Jun 2025 12:44:10 +0200 Subject: update --- ... pour r\303\251soudre Ax = b -- Exercice.ipynb" | 161 +++++++++++++++------ 1 file changed, 114 insertions(+), 47 deletions(-) (limited to 'PVCM') diff --git "a/PVCM/cama/fr/ma54 Gradient pour r\303\251soudre Ax = b -- Exercice.ipynb" "b/PVCM/cama/fr/ma54 Gradient pour r\303\251soudre Ax = b -- Exercice.ipynb" index 2a7cf27..46ec5a8 100644 --- "a/PVCM/cama/fr/ma54 Gradient pour r\303\251soudre Ax = b -- Exercice.ipynb" +++ "b/PVCM/cama/fr/ma54 Gradient pour r\303\251soudre 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,9 +67,37 @@ }, { "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 +} -- cgit v1.2.3