Reyvill Code
Reyvill Code
import scipy.linalg as la
import numpy as np
b = np.array([-4, 4, 7])
print("Matrix L:\n", L)
print("Matrix U:\n", U)
print("Permutation Matrix P:\n", P)
# Solve LUx = Pb
Pb = np.dot(P, b)
y = la.solve_triangular(L, Pb, lower=True)
x = la.solve_triangular(U, y)
print("Solution of the system:", x)
Output:
Matrix L:
[[ 1. 0. 0. ]
[ 0.33333333 1. 0. ]
[-0.66666667 0.625 1. ]]
Matrix U:
[[ 3. 1. 4. ]
[ 0. 2.66666667 -3.33333333]
[ 0. 0. 6.75 ]]
Permutation Matrix P:
[[0. 1. 0.]
[1. 0. 0.]
[0. 0. 1.]]
Code:
import numpy as np
from scipy.linalg import lu, solve_triangular
# Define matrix A and vector b
A = np.array([[1, 3, -2],
[3, 1, 4],
[-2, 1, 2]], dtype=float)
b = np.array([-4, 4, 7], dtype=float)
# Solve for Pb
Pb = np.dot(P, b)
# Print results
print("Matrix L: (Lower Triangular)")
print(L)
print("\nSolution x:")
print(x)
Output:
[[ 1. 0. 0. ]
[ 0.33333333 1. 0. ]
[-0.66666667 0.625 1. ]]
[[ 3. 1. 4. ]
[ 0. 2.66666667 -3.33333333]
[ 0. 0. 6.75 ]]
[[0. 1. 0.]
[1. 0. 0.]
[0. 0. 1.]]
Solution x: