diff --git a/src/klausurvorbereitung/regression_polyfit.py b/src/klausurvorbereitung/regression_polyfit.py new file mode 100644 index 0000000..90cf9a3 --- /dev/null +++ b/src/klausurvorbereitung/regression_polyfit.py @@ -0,0 +1,43 @@ +#%% +# Polyfit tutorial mit Plot +# Python initialisieren +import matplotlib.pyplot as plt +import numpy as np + +# Parameter +x_0 = 1 +x_E = 7.0 +y_a = -2 +y_b = 3 +dg = 1 +pr = 3 +lw = 3 +fig = 1 +tc_x = np.r_[x_0: x_E + 0.5:0.5] +tc_y = np.r_[y_a:y_b + 0.5:0.5] + +# Daten +x_data = np.r_[x_0:x_E+1] +y_data = np.array([2.5, 2.2, 1.5, 1.0, 0.6, -0.3, -1.4]) + +# Berechnungen +p = np.polyfit(x_data, y_data, dg) +g_data = np.polyval(p, x_data) + +# Ausgabe +print(60 * "*") +print("__file__") +print(60 * "*") +print(f"Steigung: m = {p[0]:#.{pr}g}") +print(f"y-Achsenabschnitt: q = {p[1]:#.{pr}g}") + +# Plot +fh = plt.figure(fig) +plt.plot(x_data, g_data, linewidth=lw) +plt.plot(x_data, y_data, "o", linewidth=lw) +plt.xlabel(r"$x$") +plt.ylabel(r"$y$") +plt.xticks(tc_x) +plt.yticks(tc_y) +plt.grid(visible=True) +plt.axis("image")