HM1_Aufgabenserie9/Schenk_Brandenberger_S9_Auf...

40 lines
1.2 KiB
Python

import numpy as np
from math import nan
def Schenk_Brandenberger_S9_Aufg2(A, A_approx, b, b_approx):
cond_A = np.linalg.cond(A,np.inf)
norm_A_minus_A_approx = np.linalg.norm((A-A_approx),np.inf)
norm_A = np.linalg.norm(A,np.inf)
norm_b_minus_b_approx = np.linalg.norm((b-b_approx),np.inf)
norm_b = np.linalg.norm(b,np.inf)
x = np.linalg.solve(A,b)
x_approx = np.linalg.solve(A_approx,b_approx)
dxmax = (cond_A/(1-(cond_A*(norm_A_minus_A_approx/norm_A))))*((norm_A_minus_A_approx/norm_A)+(norm_b_minus_b_approx/norm_b))
dxobs = np.linalg.norm((x-x_approx),np.inf)/np.linalg.norm(x,np.inf)
if(cond_A*(norm_A_minus_A_approx/norm_A)) >= 1:
dxmax = nan
return [x, x_approx, dxmax, dxobs]
if __name__ == '__main__':
# Werte aus Aufgabe 1
A = np.array([[20, 30, 10],
[10, 17, 6],
[2, 3, 2]])
A_approx = A - 0.1
b = np.array([[5720],
[3300],
[836]])
b_approx = b + 100
print(b_approx)
[x, x_approx, dxmax, dxobs] = Schenk_Brandenberger_S9_Aufg2(A, A_approx, b, b_approx)
print("Value x: ")
print(x)
print("X approx with error: ")
print(x_approx)
print("dx max: ", dxmax)
print("dx obs: ", dxobs)