From 2c82034065dd64cb152aac9d88173e7a9aaae869 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 10 Nov 2022 21:35:17 +0100 Subject: [PATCH] solved Tasks 2 + 3 --- Schenk_Brandenberger_S6_Aufg2.py | 54 +++++++++++++++++--------------- Schenk_Brandenberger_S6_Aufg3.py | 21 +++++++++++++ 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/Schenk_Brandenberger_S6_Aufg2.py b/Schenk_Brandenberger_S6_Aufg2.py index 7b03434..6985618 100644 --- a/Schenk_Brandenberger_S6_Aufg2.py +++ b/Schenk_Brandenberger_S6_Aufg2.py @@ -5,9 +5,7 @@ def switchRows(matrix, row1, row2): matrix[[row1, row2]] = matrix[[row2, row1]] return matrix - - -def gaussAltorithmus(A, b): +def Schenk_Brandenberger_S6_Aufg2(A, b): def calculateRow(A, b, row, column): b[row] = [b[row][0] - (A[row][column] / A[column][column]) * b[column][0]] @@ -15,11 +13,11 @@ def gaussAltorithmus(A, b): return A, b # Erstelle obere Dreiecksmatrix + countRowSwitch = 0 columnsToEdit = [] for row in range(1, len(A)): columnsToEdit.append(row - 1) for column in columnsToEdit: - print("Zeile", row, "Spalte", column) if(A[row-1][column] == 0 and (row - 1 == column)): rowToSwitch = row if(row == 1): @@ -30,10 +28,11 @@ def gaussAltorithmus(A, b): return "Matrix ist nicht regulär!" A = switchRows(A, row - 1, rowToSwitch) b = switchRows(b, row - 1, rowToSwitch) + countRowSwitch += 1 else: A, b = calculateRow(A, b, row, column) - print("\nA\n", A, "\nb\n", b) + print("\nObere Dreiecksmatrix A:\n", A, "\nb:\n", b) # Rückwärtseinsetzen columnsToEdit = [] @@ -41,35 +40,40 @@ def gaussAltorithmus(A, b): columnsToEdit.append(row + 1) for column in columnsToEdit: A, b = calculateRow(A, b, row, column) - print(A) - print(b) row -= 1 - print("\nA\n", A, "\nb\n", b) + print("\nA:\n", A, "\nb:\n", b) + det = 1 result = [] for i in range(len(A)): result.append(b[i][0] / A[i][i]) - return result + det *= A[i][i] + if(countRowSwitch % 2 == 1): + det *= (-1) + return result, det if __name__ == '__main__': - # Beispiel aus Vorlesungsfolen: - A = np.array([[1.0, 5.0, 6.0], - [7.0, 9.0, 6.0], - [2.0, 3.0, 4.0]]) - b = np.array([[29.0], - [43.0], - [20.0]]) + # Beispiel 4.2 aus Vorlesungsfolen: + # A = np.array([[1.0, 5.0, 6.0], + # [7.0, 9.0, 6.0], + # [2.0, 3.0, 4.0]]) + # b = np.array([[29.0], + # [43.0], + # [20.0]]) - # A = np.array([[1.0, 1.5, 2.0], - # [2.0, 3.0, 5.0], - # [0.0, 1.0, 2.0]]) - # - # b = np.array([[16.0], - # [35.0], - # [12.0]]) + # Beispiel aus Aufgabe 1a + A = np.array([[1.0, 1.5, 2.0], + [2.0, 3.0, 5.0], + [0.0, 1.0, 2.0]]) - result = gaussAltorithmus(A, b) + b = np.array([[16.0], + [35.0], + [12.0]]) + + result, det = Schenk_Brandenberger_S6_Aufg2(A, b) + print("Ergebnis:") for i in range(len(result)): - print("x" + str(i) + ": " + str(result[i])) \ No newline at end of file + print("x" + str(i) + ": " + str(result[i])) + print("Determinante: " + str(det)) \ No newline at end of file diff --git a/Schenk_Brandenberger_S6_Aufg3.py b/Schenk_Brandenberger_S6_Aufg3.py index e69de29..7bf13f4 100644 --- a/Schenk_Brandenberger_S6_Aufg3.py +++ b/Schenk_Brandenberger_S6_Aufg3.py @@ -0,0 +1,21 @@ +import numpy as np +from Schenk_Brandenberger_S6_Aufg2 import * + + +if __name__ == '__main__': + # Beispiel 4.1 + 4.3 aus den Vorlesungsfolien + A = np.array([[-1.0, 1.0, 1.0], + [1.0, -3.0, -2.0], + [5.0, 1.0, 4.0]]) + b = np.array([[0.0], + [5.0], + [3.0]]) + c = np.array([[13.0], + [-32.0], + [22.0]]) + + result, det = Schenk_Brandenberger_S6_Aufg2(A, c) + print("Ergebnis:") + for i in range(len(result)): + print("x" + str(i) + ": " + str(result[i])) + print("Determinante: " + str(det)) \ No newline at end of file