From 36cd2d662e4af89159ea74336f268218eb0f152c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20K=C3=BCng?= Date: Sat, 29 Nov 2025 11:13:50 +0100 Subject: [PATCH] Serie 10 --- Kuengjoe_S10/Kuengjoe_S10_Aufg1_python.py | 35 +++++++++++++++++++++++ Kuengjoe_S10/Kuengjoe_S10_Aufg2_python.py | 25 ++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 Kuengjoe_S10/Kuengjoe_S10_Aufg1_python.py create mode 100644 Kuengjoe_S10/Kuengjoe_S10_Aufg2_python.py diff --git a/Kuengjoe_S10/Kuengjoe_S10_Aufg1_python.py b/Kuengjoe_S10/Kuengjoe_S10_Aufg1_python.py new file mode 100644 index 0000000..a14c4f9 --- /dev/null +++ b/Kuengjoe_S10/Kuengjoe_S10_Aufg1_python.py @@ -0,0 +1,35 @@ +import numpy as np + +A = np.array([[8, 5, 2], + [5, 9, 1], + [4, 2, 7]], dtype=float) + +b = np.array([19, 5, 34], dtype=float) +x_start = np.array([1, -1, 3], dtype=float) +x_exact = np.array([2, -1, 4], dtype=float) + +D = np.diag(np.diag(A)) +L_plus_U = A - D +inv_D = np.linalg.inv(D) + + +B_jacobi = -np.dot(inv_D, L_plus_U) +alpha = np.linalg.norm(B_jacobi, np.inf) +#german text +print(f"Alpha (Faktor von Kontraktion): {alpha}") + + +x_curr = x_start.copy() +print(f"x(0) = {x_curr}") + +x_history = [x_curr.copy()] + +for k in range(1, 4): + x_new = np.dot(inv_D, (b - np.dot(L_plus_U, x_curr))) + + print(f"x({k}) = {x_new}") + x_history.append(x_new) + x_curr = x_new + +print("-" * 30) +print(f"Näherungslösung x(3): {np.around(x_curr, 4)}") \ No newline at end of file diff --git a/Kuengjoe_S10/Kuengjoe_S10_Aufg2_python.py b/Kuengjoe_S10/Kuengjoe_S10_Aufg2_python.py new file mode 100644 index 0000000..3bf3a57 --- /dev/null +++ b/Kuengjoe_S10/Kuengjoe_S10_Aufg2_python.py @@ -0,0 +1,25 @@ +import numpy as np + +A = np.array([[8, 5, 2], [5, 9, 1], [4, 2, 7]], dtype=float) +b = np.array([19, 5, 34], dtype=float) +x_k = np.array([1, -1, 3], dtype=float) +U = np.triu(A, 1) +DL = np.tril(A) + +DL_inv = np.linalg.inv(DL) + +print(f"Startvektor x(0): {np.around(x_k, 4)}") +x_history = [x_k.copy()] + +for k in range(1, 4): + x_new = np.dot(DL_inv, (b - np.dot(U, x_k))) + x_history.append(x_new.copy()) + x_k = x_new + print(f"x({k}): {np.around(x_k, 4)}") + +x_1 = np.array([2.7500, -1.4167, 3.5714]) +x_2 = np.array([2.1190, -1.0873, 3.9780]) +x_3 = np.array([1.9861, -0.9945, 4.0049]) + +print("-" * 35) +print(f"Näherung x(3) (4 Dezimalstellen): {x_3}") \ No newline at end of file