Solved Task 2 + 3

This commit is contained in:
schrom01 2022-12-01 21:48:21 +01:00
parent b83a802328
commit 94ea0d85bf
3 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,22 @@
import numpy as np
if __name__ == '__main__':
A = np.array([[1,0,2],
[0,1,0],
[1e-4,0,1e-4]])
b_exact = np.array([[1],[1],[0]])
b_approx = np.array([[1],[1],[1.67e-7]])
print("Inverse von A:\n", np.linalg.inv(A))
x_exact = np.linalg.solve(A,b_exact)
print(x_exact)
x_approx = np.linalg.solve(A,b_approx)
print(x_approx)
print(x_exact-x_approx)
print("Task 1 d:")
print(60003*(3e-7/3))
print((60003/(1-0.0060003))*((3e-7/3)+(1.67e-7/1)))
print(((0.01*(1-0.0060003))/60003)-(3e-7/3))

View File

@ -0,0 +1,40 @@
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)

View File

@ -0,0 +1,33 @@
import numpy as np
from Schenk_Brandenberger_S9_Aufg2 import *
import matplotlib.pyplot as plt
all_dxmax = np.array([])
all_dxobs = np.array([])
dxmax_dxobs_ratio = np.array([])
index = np.arange(0,1000,1)
for i in range(1000):
A = np.random.rand(100,100)
b = np.random.rand(100,1)
A_approx = A + np.random.rand(100,100)/1e5
b_approx = b + np.random.rand(100,1)/1e5
[x, x_approx, dxmax, dxobs] = Schenk_Brandenberger_S9_Aufg2(A, A_approx, b, b_approx)
all_dxmax = np.append(all_dxmax, [dxmax], axis=0)
all_dxobs = np.append(all_dxobs, [dxobs], axis=0)
dxmax_dxobs_ratio = np.append(dxmax_dxobs_ratio, [dxmax/dxobs], axis=0)
plt.figure(1)
plt.semilogy(all_dxmax)
plt.semilogy(all_dxobs)
plt.semilogy(dxmax_dxobs_ratio)
plt.legend(["dxmax", "dxob", "dxmax/dxobs"])
plt.grid()
plt.xlabel("x")
plt.ylabel("f(x)")
plt.title("Aufgabe 3 Serie 9")
plt.show()
#Die obere Schranke dxmax liegt immer dxobs, deshalb ist sie sicher realistisch und korrekt
#Jedoch liegt die obere Schranke immer etwa den Faktor 1e3 über dxobs