solved Tasks 2 + 3
This commit is contained in:
parent
3a52f4f220
commit
2c82034065
|
@ -5,9 +5,7 @@ def switchRows(matrix, row1, row2):
|
||||||
matrix[[row1, row2]] = matrix[[row2, row1]]
|
matrix[[row1, row2]] = matrix[[row2, row1]]
|
||||||
return matrix
|
return matrix
|
||||||
|
|
||||||
|
def Schenk_Brandenberger_S6_Aufg2(A, b):
|
||||||
|
|
||||||
def gaussAltorithmus(A, b):
|
|
||||||
|
|
||||||
def calculateRow(A, b, row, column):
|
def calculateRow(A, b, row, column):
|
||||||
b[row] = [b[row][0] - (A[row][column] / A[column][column]) * b[column][0]]
|
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
|
return A, b
|
||||||
|
|
||||||
# Erstelle obere Dreiecksmatrix
|
# Erstelle obere Dreiecksmatrix
|
||||||
|
countRowSwitch = 0
|
||||||
columnsToEdit = []
|
columnsToEdit = []
|
||||||
for row in range(1, len(A)):
|
for row in range(1, len(A)):
|
||||||
columnsToEdit.append(row - 1)
|
columnsToEdit.append(row - 1)
|
||||||
for column in columnsToEdit:
|
for column in columnsToEdit:
|
||||||
print("Zeile", row, "Spalte", column)
|
|
||||||
if(A[row-1][column] == 0 and (row - 1 == column)):
|
if(A[row-1][column] == 0 and (row - 1 == column)):
|
||||||
rowToSwitch = row
|
rowToSwitch = row
|
||||||
if(row == 1):
|
if(row == 1):
|
||||||
|
@ -30,10 +28,11 @@ def gaussAltorithmus(A, b):
|
||||||
return "Matrix ist nicht regulär!"
|
return "Matrix ist nicht regulär!"
|
||||||
A = switchRows(A, row - 1, rowToSwitch)
|
A = switchRows(A, row - 1, rowToSwitch)
|
||||||
b = switchRows(b, row - 1, rowToSwitch)
|
b = switchRows(b, row - 1, rowToSwitch)
|
||||||
|
countRowSwitch += 1
|
||||||
else:
|
else:
|
||||||
A, b = calculateRow(A, b, row, column)
|
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
|
# Rückwärtseinsetzen
|
||||||
columnsToEdit = []
|
columnsToEdit = []
|
||||||
|
@ -41,35 +40,40 @@ def gaussAltorithmus(A, b):
|
||||||
columnsToEdit.append(row + 1)
|
columnsToEdit.append(row + 1)
|
||||||
for column in columnsToEdit:
|
for column in columnsToEdit:
|
||||||
A, b = calculateRow(A, b, row, column)
|
A, b = calculateRow(A, b, row, column)
|
||||||
print(A)
|
|
||||||
print(b)
|
|
||||||
row -= 1
|
row -= 1
|
||||||
print("\nA\n", A, "\nb\n", b)
|
print("\nA:\n", A, "\nb:\n", b)
|
||||||
|
|
||||||
|
det = 1
|
||||||
result = []
|
result = []
|
||||||
for i in range(len(A)):
|
for i in range(len(A)):
|
||||||
result.append(b[i][0] / A[i][i])
|
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__':
|
if __name__ == '__main__':
|
||||||
# Beispiel aus Vorlesungsfolen:
|
# Beispiel 4.2 aus Vorlesungsfolen:
|
||||||
A = np.array([[1.0, 5.0, 6.0],
|
# A = np.array([[1.0, 5.0, 6.0],
|
||||||
[7.0, 9.0, 6.0],
|
# [7.0, 9.0, 6.0],
|
||||||
[2.0, 3.0, 4.0]])
|
# [2.0, 3.0, 4.0]])
|
||||||
b = np.array([[29.0],
|
# b = np.array([[29.0],
|
||||||
[43.0],
|
# [43.0],
|
||||||
[20.0]])
|
# [20.0]])
|
||||||
|
|
||||||
# A = np.array([[1.0, 1.5, 2.0],
|
# Beispiel aus Aufgabe 1a
|
||||||
# [2.0, 3.0, 5.0],
|
A = np.array([[1.0, 1.5, 2.0],
|
||||||
# [0.0, 1.0, 2.0]])
|
[2.0, 3.0, 5.0],
|
||||||
#
|
[0.0, 1.0, 2.0]])
|
||||||
# b = np.array([[16.0],
|
|
||||||
# [35.0],
|
|
||||||
# [12.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)):
|
for i in range(len(result)):
|
||||||
print("x" + str(i) + ": " + str(result[i]))
|
print("x" + str(i) + ": " + str(result[i]))
|
||||||
|
print("Determinante: " + str(det))
|
|
@ -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))
|
Loading…
Reference in New Issue