technischeDoku/plot.py

71 lines
2.5 KiB
Python

"""
plot.py -- finale Version mit zentralem Plot-Stil.
Aenderung gegenueber Stufe 3:
+ plt.style.use("thn.mplstyle")
Der Plot-Stil (Schrift, Farben, Liniendicken) wird einmal in
thn.mplstyle definiert -- alle Plots im Paper sehen automatisch
gleich aus. Aenderst du den Stil, aktualisieren sich alle Plots.
"""
import numpy as np
import matplotlib.pyplot as plt
# Plot-Stil zentral aus Datei laden
plt.style.use("thn.mplstyle")
# 1) CSV einlesen ------------------------------------------------------
daten = np.loadtxt("messwerte.csv", delimiter=",", skiprows=1)
omega = daten[:, 0]
betrag = daten[:, 1]
phase = daten[:, 2]
# 2) Theoretische Knickfrequenz ----------------------------------------
omega_K_theor = 56
K_s_dB = betrag[0]
# 3) Gemessene Knickfrequenz: dort wo der Betrag um 3 dB faellt --------
ziel_dB = K_s_dB - 3
omega_K_mess = np.interp(ziel_dB, np.flip(betrag), np.flip(omega))
abweichung = abs(omega_K_mess - omega_K_theor) / omega_K_theor * 100
# 4) Bode-Diagramm zeichnen --------------------------------------------
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.semilogx(omega, betrag, "o-")
ax1.axhline(ziel_dB, linestyle=":", color="gray",
label=f"$K_s - 3$ dB = {ziel_dB:.2f} dB")
ax1.axvline(omega_K_theor, linestyle="--", color="gray",
label=f"$\\omega_K$ theor. = {omega_K_theor} rad/s")
ax1.set_ylabel("Betrag in dB")
ax1.legend(loc="lower left")
ax2.semilogx(omega, phase, "o-")
ax2.set_xlabel("Kreisfrequenz $\\omega$ in rad/s")
ax2.set_ylabel("Phase in Grad")
fig.tight_layout()
fig.savefig("plot.pdf")
# 5) Tabelle (S-Spalten -- siunitx richtet am Komma aus) ---------------
zeilen = [
r"\begin{tabular}{S[table-format=4.1] S[table-format=-2.2] S[table-format=-3.1]}",
r"\toprule",
r"{$\omega$ (rad/s)} & {Betrag (dB)} & {Phase ($^\circ$)} \\",
r"\midrule",
]
for i in range(len(omega)):
zeilen.append(f"{omega[i]:g} & {betrag[i]:.2f} & {phase[i]:.1f} \\\\")
zeilen.append(r"\bottomrule")
zeilen.append(r"\end{tabular}")
with open("tabelle.tex", "w", encoding="utf-8") as datei:
datei.write("\n".join(zeilen) + "\n")
# 6) Werte als LaTeX-Makros --------------------------------------------
with open("werte.tex", "w", encoding="utf-8") as datei:
datei.write(f"\\newcommand{{\\omegaKtheorie}}{{{omega_K_theor}}}\n")
datei.write(f"\\newcommand{{\\omegaKmess}}{{{omega_K_mess:.1f}}}\n")
datei.write(f"\\newcommand{{\\abweichung}}{{{abweichung:.1f}}}\n")
print("Fertig: plot.pdf, tabelle.tex, werte.tex (mit thn.mplstyle).")