This commit is contained in:
2025-10-13 16:04:59 +02:00
parent b834b69d43
commit dbda6f3aa4
4 changed files with 103 additions and 1 deletions

10
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
.venv

View File

@@ -0,0 +1,45 @@
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return 5.0 * (2.0 * x**2)**(-1.0/3.0)
def g(x):
return 10.0**5 * (2.0 * np.e)**(-x/100.0)
def h_exp(x):
return (625.0/64.0)**x
# (i)
x1 = np.logspace(-3, 2, 4000) # 0.001 ... 100
y1 = f(x1)
plt.figure()
plt.loglog(x1, y1)
plt.title("Aufg. 3b (i) f(x) as straight line in log-log")
plt.xlabel("x")
plt.ylabel("f(x)")
plt.grid(True, which="both")
# (ii)
x2 = np.linspace(1e-6, 100.0, 4000)
y2 = g(x2)
plt.figure()
plt.semilogy(x2, y2)
plt.title("Aufg. 3b (ii) g(x) as straight line in semilog-y")
plt.xlabel("x")
plt.ylabel("g(x)")
plt.grid(True, which="both")
# (iii)
x3 = np.linspace(1e-6, 100.0, 4000)
y3 = h_exp(x3)
plt.figure()
plt.semilogy(x3, y3)
plt.title("Aufg. 3b (iii) h(x) as straight line in semilog-y")
plt.xlabel("x")
plt.ylabel("h(x)")
plt.grid(True, which="both")
plt.show()

View File

@@ -0,0 +1,42 @@
import numpy as np
import matplotlib.pyplot as plt
def g(x):
# Naive Form des Polynoms 100 x² 200 x + 99
return 100*x**2 - 200*x + 99
def g_fact(x):
# Faktorisierte Form 100 (x 1.1)(x 0.9) vermeidet Auslöschung
return 100*(x-1.1)*(x-0.9)
def h(x, factored=False):
# Liefert h(x) = sqrt(g(x)); bei factored=True wird die stabile Variante
# g_fact(x) verwendet
return np.sqrt(g_fact(x) if factored else g(x))
def kappa_h(x):
# Konditionszahl κ_h(x) = |x * h'(x) / h(x)|
# mit h'(x) = 100 (x 1) / h(x)
return np.abs(x * 100*(x-1) / (h(x)**2))
# a) Vergleich der Auswertungen
x_test = 1.1 + np.array([1e-8, 1e-7, 1e-6, 1e-5])
print("x h_naiv h_fakt relFehler")
for x in x_test:
# instabile Auswertung
h_naiv = h(x)
# stabile Auswertungm
h_fakt = h(x, factored=True)
relerr = abs(h_naiv - h_fakt)/abs(h_fakt)
print(f"{x:.10f} {h_naiv:.12e} {h_fakt:.12e} {relerr:.2e}")
# b) Plot der Konditionszahl κ_h(x) auf [1.1, 1.3]
dx = 1e-7
x_vals = np.arange(1.1 + dx, 1.3 + dx, dx)
plt.semilogy(x_vals, kappa_h(x_vals))
plt.xlabel("x")
plt.ylabel(r"$\kappa_h(x)$")
plt.title("Konditionszahl von $h(x)$ auf [1.1, 1.3]")
plt.grid(True, which="both", ls="--", alpha=0.6)
plt.tight_layout()
plt.show()

View File

@@ -7,7 +7,8 @@ def s2n_naiv(s: float) -> float:
def s2n_stabil(s: float) -> float: def s2n_stabil(s: float) -> float:
t = max(0.0, 1.0 - (s*s)/4.0) t = max(0.0, 1.0 - (s*s)/4.0)
return (s*s) / (2.0*(1.0 + math.sqrt(t))) return s / math.sqrt(2.0*(1.0 + math.sqrt(t)))
n0 = 6 n0 = 6
s_naiv = 1.0 s_naiv = 1.0
s_stab = 1.0 s_stab = 1.0
@@ -30,6 +31,10 @@ zwei_pi = 2.0 * math.pi
print(f"Letzter m-Wert: m = {m_vals[-1]:.0f}") print(f"Letzter m-Wert: m = {m_vals[-1]:.0f}")
print(f"Naiv: m*s_m = {p_naiv[-1]:.15f} Fehler = {abs(p_naiv[-1]-zwei_pi):.3e}") print(f"Naiv: m*s_m = {p_naiv[-1]:.15f} Fehler = {abs(p_naiv[-1]-zwei_pi):.3e}")
print(f"Stabil: m*s_m = {p_stab[-1]:.15f} Fehler = {abs(p_stab[-1]-zwei_pi):.3e}") print(f"Stabil: m*s_m = {p_stab[-1]:.15f} Fehler = {abs(p_stab[-1]-zwei_pi):.3e}")
err = abs(p_stab[-1] - 2*math.pi)/(2*math.pi)
print(f"relativer Fehler stabil = {err:.3e}")
plt.figure() plt.figure()
plt.plot(m_vals, p_naiv, label="naive Formel") plt.plot(m_vals, p_naiv, label="naive Formel")