diff --git a/exercises-toolbox/4-scipy/3-polyfit/aufgabe.txt b/exercises-toolbox/4-scipy/3-polyfit/aufgabe.txt new file mode 100644 index 00000000..23ae1171 --- /dev/null +++ b/exercises-toolbox/4-scipy/3-polyfit/aufgabe.txt @@ -0,0 +1,4 @@ +Führe einmal `python loesung.py` aus, um die Daten zu erzeugen. + +Fitte die Daten aus daten.txt (x, y, y_err) an ein Polynom zweiten Grades. +Nutze hierfür np.polyfit. diff --git a/exercises-toolbox/4-scipy/3-polyfit/loesung.py b/exercises-toolbox/4-scipy/3-polyfit/loesung.py new file mode 100644 index 00000000..b94721a7 --- /dev/null +++ b/exercises-toolbox/4-scipy/3-polyfit/loesung.py @@ -0,0 +1,40 @@ +import numpy as np +import matplotlib.pyplot as plt + +# Generate data +rng = np.random.default_rng(210) +N = 50 +data_x = np.linspace(-10, 10, N) +error_y = rng.lognormal(-1, 0.2, size=N) +data_y = rng.normal(data_x**2, error_y) +np.savetxt("daten.txt", np.column_stack([data_x, data_y, error_y]), header="x y y_err") + +# The solution starts here +x, y, e_y = np.genfromtxt("daten.txt", unpack=True) + + +def f(x, a, b, c): + return a * (x + b)**2 + c + + +parameters, covariance_matrix = np.polyfit(x, y, deg=2, cov=True) +uncertainties = np.sqrt(np.diag(covariance_matrix)) + +for name, value, unc in zip('abc', parameters, uncertainties): + print(f'{name} = {value:.3f} ± {unc:.3f}') + +fig = plt.figure(layout="constrained") +ax = fig.add_subplot() + +ax.errorbar(x, y, yerr=e_y, fmt="k.", label="data") + +t = np.linspace(-10, 10, 500) +ax.plot(t, f(t, *parameters), label="Fit") +ax.plot(t, t**2, "--", label="Original") + +ax.set_xlim(t[0], t[-1]) +ax.set_xlabel(r"$t$") +ax.set_ylabel(r"$f(t)$") +ax.legend() + +plt.savefig("loesung.pdf") diff --git a/exercises-toolbox/4-scipy/3-curve_fit/.gitignore b/exercises-toolbox/4-scipy/4-curve_fit/.gitignore similarity index 100% rename from exercises-toolbox/4-scipy/3-curve_fit/.gitignore rename to exercises-toolbox/4-scipy/4-curve_fit/.gitignore diff --git a/exercises-toolbox/4-scipy/3-curve_fit/aufgabe.txt b/exercises-toolbox/4-scipy/4-curve_fit/aufgabe.txt similarity index 65% rename from exercises-toolbox/4-scipy/3-curve_fit/aufgabe.txt rename to exercises-toolbox/4-scipy/4-curve_fit/aufgabe.txt index 6eadfcb5..9a23a558 100644 --- a/exercises-toolbox/4-scipy/3-curve_fit/aufgabe.txt +++ b/exercises-toolbox/4-scipy/4-curve_fit/aufgabe.txt @@ -1,8 +1,7 @@ -# curve_fit +# curve_fit Aufgabe: - -Führe einmal `python loesung.py` aus, um die daten zu erzeugen. +Führe einmal `python loesung.py` aus, um die Daten zu erzeugen. Fitte die Daten aus daten.txt (x, y, y_err) mit der Funktion f(x) = a * sin(b * x + c) + d. Benutze dazu scipy.optimize.curve_fit. diff --git a/exercises-toolbox/4-scipy/3-curve_fit/loesung.py b/exercises-toolbox/4-scipy/4-curve_fit/loesung.py similarity index 100% rename from exercises-toolbox/4-scipy/3-curve_fit/loesung.py rename to exercises-toolbox/4-scipy/4-curve_fit/loesung.py diff --git a/exercises-toolbox/4-scipy/3-curve_fit/vorlage.py b/exercises-toolbox/4-scipy/4-curve_fit/vorlage.py similarity index 96% rename from exercises-toolbox/4-scipy/3-curve_fit/vorlage.py rename to exercises-toolbox/4-scipy/4-curve_fit/vorlage.py index c15d7139..5d41c4f4 100644 --- a/exercises-toolbox/4-scipy/3-curve_fit/vorlage.py +++ b/exercises-toolbox/4-scipy/4-curve_fit/vorlage.py @@ -19,5 +19,5 @@ def f(x, a, b, c, d): ax.set_xlim(t[0], t[-1]) ax.set_xlabel(r"$t$") ax.set_ylabel(r"$f(t)$") -ax.legend(loc="best") +ax.legend() fig.savefig("loesung.pdf") diff --git a/exercises-toolbox/4-scipy/4-peakdetect/aufgabe.txt b/exercises-toolbox/4-scipy/5-peakdetect/aufgabe.txt similarity index 100% rename from exercises-toolbox/4-scipy/4-peakdetect/aufgabe.txt rename to exercises-toolbox/4-scipy/5-peakdetect/aufgabe.txt diff --git a/exercises-toolbox/4-scipy/4-peakdetect/data.txt b/exercises-toolbox/4-scipy/5-peakdetect/data.txt similarity index 100% rename from exercises-toolbox/4-scipy/4-peakdetect/data.txt rename to exercises-toolbox/4-scipy/5-peakdetect/data.txt diff --git a/exercises-toolbox/4-scipy/4-peakdetect/loesung.py b/exercises-toolbox/4-scipy/5-peakdetect/loesung.py similarity index 98% rename from exercises-toolbox/4-scipy/4-peakdetect/loesung.py rename to exercises-toolbox/4-scipy/5-peakdetect/loesung.py index 00f6bff0..d217d864 100644 --- a/exercises-toolbox/4-scipy/4-peakdetect/loesung.py +++ b/exercises-toolbox/4-scipy/5-peakdetect/loesung.py @@ -35,5 +35,5 @@ def e(x, a, b, c): ax.set_xlabel(r"$t \ / \ \mathrm{ms}$") ax.set_ylabel(r"$U \ / \ \mathrm{V}$") ax.set_xlim(0, 0.3) -ax.legend(loc="best") +ax.legend() fig.savefig("loesung.pdf") diff --git a/exercises-toolbox/4-scipy/4-peakdetect/vorlage.py b/exercises-toolbox/4-scipy/5-peakdetect/vorlage.py similarity index 96% rename from exercises-toolbox/4-scipy/4-peakdetect/vorlage.py rename to exercises-toolbox/4-scipy/5-peakdetect/vorlage.py index 708af950..cc30d343 100644 --- a/exercises-toolbox/4-scipy/4-peakdetect/vorlage.py +++ b/exercises-toolbox/4-scipy/5-peakdetect/vorlage.py @@ -21,5 +21,5 @@ ax.set_xlabel(r"$t \ / \ \mathrm{ms}$") ax.set_ylabel(r"$U \ / \ \mathrm{V}$") ax.set_xlim(0, 0.3) -ax.legend(loc="best") +ax.legend() fig.savefig("loesung.pdf") diff --git a/exercises-toolbox/4-scipy/5-beugung/I.txt b/exercises-toolbox/4-scipy/6-beugung/I.txt similarity index 100% rename from exercises-toolbox/4-scipy/5-beugung/I.txt rename to exercises-toolbox/4-scipy/6-beugung/I.txt diff --git a/exercises-toolbox/4-scipy/5-beugung/L.txt b/exercises-toolbox/4-scipy/6-beugung/L.txt similarity index 100% rename from exercises-toolbox/4-scipy/5-beugung/L.txt rename to exercises-toolbox/4-scipy/6-beugung/L.txt diff --git a/exercises-toolbox/4-scipy/5-beugung/aufgabe.txt b/exercises-toolbox/4-scipy/6-beugung/aufgabe.txt similarity index 100% rename from exercises-toolbox/4-scipy/5-beugung/aufgabe.txt rename to exercises-toolbox/4-scipy/6-beugung/aufgabe.txt diff --git a/exercises-toolbox/4-scipy/5-beugung/lambda.txt b/exercises-toolbox/4-scipy/6-beugung/lambda.txt similarity index 100% rename from exercises-toolbox/4-scipy/5-beugung/lambda.txt rename to exercises-toolbox/4-scipy/6-beugung/lambda.txt diff --git a/exercises-toolbox/4-scipy/5-beugung/loesung.py b/exercises-toolbox/4-scipy/6-beugung/loesung.py similarity index 100% rename from exercises-toolbox/4-scipy/5-beugung/loesung.py rename to exercises-toolbox/4-scipy/6-beugung/loesung.py diff --git a/exercises-toolbox/4-scipy/5-beugung/vorlage.py b/exercises-toolbox/4-scipy/6-beugung/vorlage.py similarity index 100% rename from exercises-toolbox/4-scipy/5-beugung/vorlage.py rename to exercises-toolbox/4-scipy/6-beugung/vorlage.py diff --git a/exercises-toolbox/5-uncertainties/3-curve_fit/aufgabe.txt b/exercises-toolbox/5-uncertainties/3-curve_fit/aufgabe.txt index de8da97c..79f4a42e 100644 --- a/exercises-toolbox/5-uncertainties/3-curve_fit/aufgabe.txt +++ b/exercises-toolbox/5-uncertainties/3-curve_fit/aufgabe.txt @@ -21,3 +21,7 @@ def ucurve_fit(f, x, y, **kwargs): … … = scipy.optimize.curve_fit(…, **kwargs) … + +Überprüfe deine Wrapper-Implementation an den Daten in daten.txt und fitte diese an +A * cos(B * x) + C. +Plotte den Fit zusammen mit den originalen Daten. diff --git a/exercises-toolbox/5-uncertainties/3-curve_fit/loesung.py b/exercises-toolbox/5-uncertainties/3-curve_fit/loesung.py index 7514a02b..ce4701f4 100644 --- a/exercises-toolbox/5-uncertainties/3-curve_fit/loesung.py +++ b/exercises-toolbox/5-uncertainties/3-curve_fit/loesung.py @@ -1,4 +1,5 @@ import numpy as np +import matplotlib.pyplot as plt import scipy.optimize import uncertainties as unc import uncertainties.unumpy as unp @@ -15,3 +16,34 @@ def ucurve_fit(f, x, y, **kwargs): ) return unc.correlated_values(popt, pcov) + + +def f(x, a, b, c): + return a * np.cos(x * b) + c + +# Generate data +length = 100 +x = np.linspace(0, 3 * np.pi, length) +rng = np.random.default_rng() +y1 = rng.normal(0.0, 0.2, length) +y2 = np.abs(rng.normal(0.0, 0.2, length)) + +y = f(x, 1, 1, -3) + y1 + +np.savetxt("daten.txt", np.column_stack([x, y, y2]), header="x\ty\ty_err") + +# Solution +x, y_0, y_err = np.genfromtxt("daten.txt", unpack=True) +y = unp.uarray(y_0, y_err) +params = ucurve_fit(f, x, y) +print("a * cos(x * b) + c") +for char, p in zip("abc", params): + print(f"{char} = {p}") + +fig = plt.figure(layout="constrained") +ax = fig.add_subplot() +ax.errorbar(x, unp.nominal_values(y), yerr=y_err, fmt=".", label="Daten") +ax.plot(x, f(x, *unp.nominal_values(params)), label="Fit") +ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi], [0, "π", "2π", "3π"]) +ax.legend() +plt.savefig("loesung.pdf")