Algorithme de la remontée: Backward Substitution for Linear Systems

Telechargé par Fatima OUKADDOUR
programation de la methode de rementé pour resoudre une système triangulaire supérieur
Algorithme de la remontée
# Configuration initiale
import numpy as np
import matplotlib.pyplot as plt
def remontee(U, b):
# Conversion explicite en numpy array
U = np.array(U, dtype=float)
b = np.array(b, dtype=float)
n = len(b)
x = np.zeros(n)
x[n-1] = b[n-1] / U[n-1, n-1]
for k in range(n-2, -1, -1):
s = 0
for j in range(k+1, n):
s = s + U[k, j] * x[j]
x[k] = (b[k] - s) / U[k, k]
return x
# Exemple
# Données du problème
U = np.array([
[2, -1, 3],
[0, 4, 1],
[0, 0, 5]
], dtype=float)
b = np.array([5, 6, 10], dtype=float)
x = remontee(U, b)
print("La solution du système est x=",x) #
Méthode de la descente
def descente(L, b):
# Conversion explicite en numpy array
L = np.array(L, dtype=float)
b = np.array(b, dtype=float)
n = len(b)
y = np.zeros(n) # Array numpy
y[0] = b[0] / L[0, 0] # Utilise L[0,0] au lieu de L[0][0]
for k in range(1, n):
somme = 0
for j in range(0, k):
somme += L[k, j] * y[j] # L[k,j] au lieu de L[k][j]
y[k] = (b[k] - somme) / L[k, k]
return y # Retourne un array numpy
# Matrice triangulaire inférieure
L = [
[2, 0, 0],
[1, 3, 0],
[4, -2, 5]
]
b = [4, 5, 8]
x = descente(L, b)
print("La solution du système est x=",x) #
Méthode de Gauss
def gauss_elimination(A, b):
# Conversion explicite en numpy array
A = np.array(A, dtype=float)
b = np.array(b, dtype=float)
n = len(b)
# Copie pour ne pas modifier les originaux
U = [row[:] for row in A]
B = b[:]
for k in range(n-1):
for i in range(k+1, n):
mik = U[i][k] / U[k][k]
for j in range(k, n):
U[i][j] = U[i][j] - mik * U[k][j]
B[i] = B[i] - mik * B[k]
return U, B
# Exemple
# Système donné
A = np.array([
[2, 1, -1],
[-3, -1, 2],
[-2, 1, 2]
], dtype=float)
b = np.array([8, -11, -3], dtype=float)
# Élimination de Gauss
U, B = gauss_elimination(A, b)
print("\nU",U)
print("\nB",B)
# Remontée
x = remontee(U, B)
print("\nLa solution du système est x=",x)
La méthode LU
def lu_decomposition(A):
"""
Décomposition LU sans pivotage.
Paramètres:
A : np.ndarray (n x n) - matrice régulière
Retourne:
L : np.ndarray (n x n) - triangulaire inférieure avec des 1 sur la diagonale
U : np.ndarray (n x n) - triangulaire supérieure
"""
n = len(A)
# Copie de A dans U
U = np.array(A, dtype=float)
# Initialisation de L comme matrice identité
L = np.eye(n, dtype=float)
for k in range(n-1):
for i in range(k+1, n):
# Calcul du multiplicateur
L[i, k] = U[i, k] / U[k, k]
# Mise à jour de la ligne i de U
for j in range(k, n):
U[i, j] -= L[i, k] * U[k, j]
return L, U
# Système donné
A = np.array([
[2, 3, 1],
[4, 7, 3],
[6, 18, 5]
], dtype=float)
b = np.array([1, 1, -1], dtype=float)
# Décomposition LU
L, U = lu_decomposition(A)
print("Matrice A :")
print(A)
print("\nMatrice L :")
print(L)
print("\nMatrice U :")
print(U)
print("\nVérification L * U :")
print(np.dot(L, U))
# Résolution
y = descente(L, b)
print("\nY=",y)
x = remontee(U, y)
from fractions import Fraction
print("\nSolution : X=",x)
# Affichage en fractions
print("\nSolution : X =", [Fraction(val).limit_denominator() for val in x])
Methode Factorisation LU avec pivotage partiel.
import numpy as np
def lu_pivot(A):
"""
Factorisation LU avec pivotage partiel.
Paramètres:
A : np.ndarray (n x n) - matrice régulière
Retourne:
L : np.ndarray (n x n) - triangulaire inférieure avec des 1 sur la diagonale
U : np.ndarray (n x n) - triangulaire supérieure
P : np.ndarray (n x n) - matrice de permutation telle que PA = LU
"""
n = len(A)
U = A.astype(float).copy()
L = np.eye(n, dtype=float)
P = np.eye(n, dtype=float)
for k in range(n-1):
# Recherche du pivot
p = k
for i in range(k+1, n):
if abs(U[i, k]) > abs(U[p, k]):
p = i
# Permutation si nécessaire
if p != k:
# Échanger les lignes de U
U[[k, p]] = U[[p, k]]
# Échanger les lignes de L (partie déjà calculée)
L[[k, p], :k] = L[[p, k], :k]
# Échanger les lignes de P
P[[k, p]] = P[[p, k]]
# Élimination
for i in range(k+1, n):
L[i, k] = U[i, k] / U[k, k]
for j in range(k, n):
U[i, j] -= L[i, k] * U[k, j]
return L, U, P
# Test avec un exemple
A = np.array([
[2, 3, 1],
[4, 7, 3],
[6, 18, 5]
], dtype=float)
L, U, P = lu_pivot(A)
print("Matrice A :")
print(A)
print("\nMatrice L :")
print(L)
print("\nMatrice U :")
print(U)
print("\nMatrice P :")
print(P)
# Étape 1 : Permutation du second membre
b_tilde = P @ b
print(f"b_tilde (Pb) = {b_tilde}")
# Étape 2 : Descente (résolution de Ly = b_tilde)
y = descente(L, b_tilde)
print(f"y (solution de Ly = Pb) = {y}")
# Étape 3 : Remontée (résolution de Ux = y)
x = remontee(U, y)
print(f"x (solution de Ux = y) = {x}")
# Affichage en fractions
print("\nSolution : X =", [Fraction(val).limit_denominator() for val in x])
print("\nPA :")
print("\nVérification PA = LU :")
print(np.allclose(P @ A, L @ U))
La factorisation de cholesky
def cholesky(A):
"""
Factorisation de Cholesky : A = L * L^T
A : matrice symétrique définie positive
Retourne : matrice triangulaire inférieure L
"""
n = len(A)
L = np.zeros((n, n), dtype=float)
for k in range(n):
# Calcul du pivot l_kk
somme = 0
for s in range(k):
somme += L[k, s] ** 2
L[k, k] = np.sqrt(A[k, k] - somme)
# Calcul de la colonne k de L
for i in range(k+1, n):
somme = 0
for s in range(k):
somme += L[i, s] * L[k, s]
L[i, k] = (A[i, k] - somme) / L[k, k]
return L
# Matrice A et second membre b
A = np.array([
[4, 2, 2],
[2, 5, 1],
[2, 1, 3]
], dtype=float)
b = np.array([1, 2, 3], dtype=float)
# Factorisation de Cholesky
L = cholesky(A)
print("Matrice A :")
print(A)
print("\nMatrice L (Cholesky) :")
print(L)
print("\nVérification L * L^T :")
print(L @ L.T)
# Résolution du système Ax = b
# 1. Descente : Ly = b
y = descente(L, b)
print("\ny =", y)
# 2. Remontée : L^T x = y
x = remontee(L.T, y)
print("\nSolution x =", x)
# Affichage en fractions
print("\nSolution : X =", [Fraction(val).limit_denominator() for val in x])
# Vérification
print("\nVérification A*x :", A @ x)
print("b :", b)
1 / 5 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans l'interface ou les textes ? Ou savez-vous comment améliorer l'interface utilisateur de StudyLib ? N'hésitez pas à envoyer vos suggestions. C'est très important pour nous!