Serie 02
This commit is contained in:
88
Kuengjoe_s02/Kuengjoe_S02_Aufg2.py
Normal file
88
Kuengjoe_s02/Kuengjoe_S02_Aufg2.py
Normal file
@@ -0,0 +1,88 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# a)
|
||||
|
||||
def f1(x):
|
||||
return (((((((x - 14)*x + 84)*x - 280)*x + 560)*x - 672)*x + 448)*x - 128)
|
||||
|
||||
def f2(x):
|
||||
return (x-2)**7
|
||||
|
||||
xa = np.linspace(1.99, 2.01, 501, dtype=np.float64)
|
||||
|
||||
ya1 = f1(xa)
|
||||
ya2 = f2(xa)
|
||||
|
||||
plt.figure()
|
||||
plt.plot(xa, ya1, label='f1(x) expanded', linewidth=2)
|
||||
plt.plot(xa, ya2, label='f2(x) (x-2)^7', linestyle='--', linewidth=2)
|
||||
plt.title("Vergleich zweier äquivalenter Funktionen")
|
||||
plt.xlabel("x")
|
||||
plt.ylabel("f(x)")
|
||||
plt.legend()
|
||||
plt.grid(True, alpha=0.3)
|
||||
plt.tight_layout()
|
||||
|
||||
|
||||
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
rel_err= np.abs((ya1 - ya2)/ np.where( ya2!= 0, ya2, 1))
|
||||
|
||||
plt.figure()
|
||||
plt.plot(xa, rel_err)
|
||||
plt.title("Relativer Fehler zwischen f1 und f2")
|
||||
plt.xlabel("x")
|
||||
plt.ylabel("Relativer Fehler")
|
||||
plt.yscale("log")
|
||||
plt.grid(True, which="both", alpha=0.3)
|
||||
plt.tight_layout()
|
||||
|
||||
# b)
|
||||
xmin, xmax, h = -1e-14, 1e-14, 1e-17
|
||||
n = int(round((xmax - xmin) / h)) + 1
|
||||
xb = np.linspace(xmin, xmax, n, dtype=np.float64)
|
||||
|
||||
def g_naive(x):
|
||||
return x / (np.sin(1.0+x) - np.sin(1.0))
|
||||
|
||||
g_b = g_naive(xb)
|
||||
i0 = np.argmin(np.abs(xb))
|
||||
g_b[i0] = np.nan
|
||||
|
||||
|
||||
plt.figure()
|
||||
plt.plot(xb, g_b)
|
||||
plt.title('g(x) = x / (sin(1+x) - sin(1))')
|
||||
plt.xlabel('x')
|
||||
plt.ylabel('g(x)')
|
||||
plt.grid(True, alpha=0.3)
|
||||
plt.tight_layout()
|
||||
|
||||
|
||||
# c)
|
||||
|
||||
def g_stab(x):
|
||||
return x / (2.0 * np.cos(1.0 +0.5*x) * np.sin(0.5*x))
|
||||
|
||||
g_c = g_stab(xb)
|
||||
g_c[i0] = 1.0 / np.cos(1.0)
|
||||
|
||||
plt.figure()
|
||||
plt.plot(xb, g_c)
|
||||
plt.axhline(1.0 /np.cos(1.0), linestyle='--')
|
||||
plt.title('Stabilisierte Berechnung von g(x)')
|
||||
plt.xlabel('x')
|
||||
plt.ylabel('g(x)')
|
||||
plt.grid(True, alpha=0.3)
|
||||
plt.tight_layout()
|
||||
|
||||
print('theoretischer Grenzwert: g(0) =', 1.0 / np.cos(1.0))
|
||||
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
45
Kuengjoe_s02/Kuengjoe_S02_Aufg3.py
Normal file
45
Kuengjoe_s02/Kuengjoe_S02_Aufg3.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import math
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def s2n_naiv(s: float) -> float:
|
||||
t = max(0.0, 1.0 - (s*s)/4.0)
|
||||
return math.sqrt(2.0 - 2.0*math.sqrt(t))
|
||||
|
||||
def s2n_stabil(s: float) -> float:
|
||||
t = max(0.0, 1.0 - (s*s)/4.0)
|
||||
return (s*s) / (2.0*(1.0 + math.sqrt(t)))
|
||||
n0 = 6
|
||||
s_naiv = 1.0
|
||||
s_stab = 1.0
|
||||
K = 40
|
||||
|
||||
m_vals = [n0]
|
||||
p_naiv = [n0 * s_naiv]
|
||||
p_stab = [n0 * s_stab]
|
||||
|
||||
m = n0
|
||||
for _ in range(K):
|
||||
m *= 2
|
||||
s_naiv = s2n_naiv(s_naiv)
|
||||
s_stab = s2n_stabil(s_stab)
|
||||
m_vals.append(m)
|
||||
p_naiv.append(m * s_naiv)
|
||||
p_stab.append(m * s_stab)
|
||||
|
||||
zwei_pi = 2.0 * math.pi
|
||||
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"Stabil: m*s_m = {p_stab[-1]:.15f} Fehler = {abs(p_stab[-1]-zwei_pi):.3e}")
|
||||
|
||||
plt.figure()
|
||||
plt.plot(m_vals, p_naiv, label="naive Formel")
|
||||
plt.plot(m_vals, p_stab, label="stabilisierte Formel")
|
||||
plt.axhline(zwei_pi, linestyle="--", linewidth=1, label="2π")
|
||||
plt.xscale("log")
|
||||
plt.xlabel("Anzahl Ecken m (log-Skala)")
|
||||
plt.ylabel("Umfangapproximation m·s_m")
|
||||
plt.title("Archimedes-Algorithmus: Umfang des Einheitskreises")
|
||||
plt.legend()
|
||||
plt.grid(True, alpha=0.3)
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
37
Kuengjoe_s02/Kuengjoe_S02_Aufg4.py
Normal file
37
Kuengjoe_s02/Kuengjoe_S02_Aufg4.py
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
|
||||
import math
|
||||
|
||||
def maschinengenauigkeit():
|
||||
eps = 1.0
|
||||
while 1.0 + eps != 1.0:
|
||||
eps /= 2.0
|
||||
return eps
|
||||
|
||||
def q_min_aus_eps(eps):
|
||||
q = 1.0/eps
|
||||
while 1.0 + q != q:
|
||||
q *= 2.0
|
||||
return q
|
||||
|
||||
eps = maschinengenauigkeit()
|
||||
eps_krit = 2.0*eps
|
||||
q_min = q_min_aus_eps(eps)
|
||||
|
||||
bits = round(-math.log2(eps)) # ≈ 53
|
||||
decs = round(-math.log10(eps))
|
||||
|
||||
eps = maschinengenauigkeit()
|
||||
eps_krit = 2.0 * eps # kleinste Zahl mit 1+eps_krit != 1
|
||||
q_min = q_min_aus_eps(eps)
|
||||
|
||||
bits = round(-math.log2(eps)) # ≈ 53
|
||||
decs = round(-math.log10(eps)) # ≈ 16
|
||||
|
||||
print(f"Maschinengenauigkeit eps = {eps:.20e}")
|
||||
print(f"Kontrolle: 1+eps == 1 -> {1.0 + eps == 1.0}")
|
||||
print(f"Kontrolle: 1+2*eps != 1 -> {1.0 + eps_krit != 1.0}")
|
||||
print(f"Signifikanz: ~{bits} Bits (~{decs} Dezimalstellen)")
|
||||
print(f"q_min = {q_min:.0f}")
|
||||
print(f"Kontrolle: 1+q_min == q_min -> {1.0 + q_min == q_min}")
|
||||
print("Zusammenhang: q_min = 1/eps (bei IEEE-754 double exakt).")
|
||||
Reference in New Issue
Block a user