Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def tridiagonal_algorithm(a, b, c, d):
- n = len(a)
- p = [0] * (n + 1)
- q = [0] * (n + 1)
- # Прямая прогонка
- for i in range(n):
- denominator = b[i] - a[i] * p[i]
- if abs(denominator) < 1e-6:
- raise ValueError("Деление на ноль или почти ноль в прогонке.")
- p[i + 1] = c[i] / denominator
- q[i + 1] = (a[i] * q[i] - d[i]) / denominator
- print("Прямая прогонка:")
- for i in range(1, n):
- print(f"(P{i}, Q{i}) = ({p[i]:.4f}, {q[i]:.4f})")
- print(f"(Q{n}) = ({q[n]:.4f})\n")
- # Обратная прогонка
- res = [0] * n
- res[-1] = q[n]
- for i in range(n - 2, -1, -1):
- res[i] = p[i + 1] * res[i + 1] + q[i + 1]
- return res
- n = 5
- v = 8.0
- A = np.zeros((n, n))
- for i in range(n):
- for j in range(max(0, i - 1), min(i + 2, n)):
- A[i][j] = (v + i) / 100
- A[i][i] = v + i
- V = np.array([v + i for i in range(n)])
- B = np.dot(A, V)
- print("Расширенная матрица A|B:")
- for i in range(n):
- print("\t".join(f"{A[i][j]:.4f}" for j in range(n)), "|", f"{B[i]:.4f}")
- print()
- a = [0] * n
- b = [0] * n
- c = [0] * n
- d = B.tolist()
- for i in range(n):
- if i > 0:
- a[i] = A[i][i - 1]
- b[i] = -A[i][i]
- if i < n - 1:
- c[i] = A[i][i + 1]
- X = tridiagonal_algorithm(a, b, c, d)
- print("Решение СЛАУ:")
- print("\t".join(f"{x:.4f}" for x in X))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement