From b762344a1fa427cff6d43087e5128796e43300d1 Mon Sep 17 00:00:00 2001 From: chrbeckm Date: Sun, 20 Aug 2023 18:44:38 +0200 Subject: [PATCH 1/2] add polyfit exercise and data to uncertainties curve_fit wrapper --- .../4-scipy/3-polyfit/aufgabe.txt | 4 ++ .../4-scipy/3-polyfit/loesung.py | 40 +++++++++++++++++++ .../{3-curve_fit => 4-curve_fit}/.gitignore | 0 .../{3-curve_fit => 4-curve_fit}/aufgabe.txt | 2 +- .../{3-curve_fit => 4-curve_fit}/loesung.py | 0 .../{3-curve_fit => 4-curve_fit}/vorlage.py | 2 +- .../aufgabe.txt | 0 .../{4-peakdetect => 5-peakdetect}/data.txt | 0 .../{4-peakdetect => 5-peakdetect}/loesung.py | 2 +- .../{4-peakdetect => 5-peakdetect}/vorlage.py | 2 +- .../4-scipy/{5-beugung => 6-beugung}/I.txt | 0 .../4-scipy/{5-beugung => 6-beugung}/L.txt | 0 .../{5-beugung => 6-beugung}/aufgabe.txt | 0 .../{5-beugung => 6-beugung}/lambda.txt | 0 .../{5-beugung => 6-beugung}/loesung.py | 0 .../{5-beugung => 6-beugung}/vorlage.py | 0 .../5-uncertainties/3-curve_fit/aufgabe.txt | 4 ++ .../5-uncertainties/3-curve_fit/loesung.py | 32 +++++++++++++++ 18 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 exercises-toolbox/4-scipy/3-polyfit/aufgabe.txt create mode 100644 exercises-toolbox/4-scipy/3-polyfit/loesung.py rename exercises-toolbox/4-scipy/{3-curve_fit => 4-curve_fit}/.gitignore (100%) rename exercises-toolbox/4-scipy/{3-curve_fit => 4-curve_fit}/aufgabe.txt (67%) rename exercises-toolbox/4-scipy/{3-curve_fit => 4-curve_fit}/loesung.py (100%) rename exercises-toolbox/4-scipy/{3-curve_fit => 4-curve_fit}/vorlage.py (96%) rename exercises-toolbox/4-scipy/{4-peakdetect => 5-peakdetect}/aufgabe.txt (100%) rename exercises-toolbox/4-scipy/{4-peakdetect => 5-peakdetect}/data.txt (100%) rename exercises-toolbox/4-scipy/{4-peakdetect => 5-peakdetect}/loesung.py (98%) rename exercises-toolbox/4-scipy/{4-peakdetect => 5-peakdetect}/vorlage.py (96%) rename exercises-toolbox/4-scipy/{5-beugung => 6-beugung}/I.txt (100%) rename exercises-toolbox/4-scipy/{5-beugung => 6-beugung}/L.txt (100%) rename exercises-toolbox/4-scipy/{5-beugung => 6-beugung}/aufgabe.txt (100%) rename exercises-toolbox/4-scipy/{5-beugung => 6-beugung}/lambda.txt (100%) rename exercises-toolbox/4-scipy/{5-beugung => 6-beugung}/loesung.py (100%) rename exercises-toolbox/4-scipy/{5-beugung => 6-beugung}/vorlage.py (100%) 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 67% rename from exercises-toolbox/4-scipy/3-curve_fit/aufgabe.txt rename to exercises-toolbox/4-scipy/4-curve_fit/aufgabe.txt index 9ae28961..c9a92760 100644 --- a/exercises-toolbox/4-scipy/3-curve_fit/aufgabe.txt +++ b/exercises-toolbox/4-scipy/4-curve_fit/aufgabe.txt @@ -1,4 +1,4 @@ -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 09ebed48..93583216 100644 --- a/exercises-toolbox/5-uncertainties/3-curve_fit/aufgabe.txt +++ b/exercises-toolbox/5-uncertainties/3-curve_fit/aufgabe.txt @@ -16,3 +16,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") From 041b3e8ee676b5967a5625f8a8f1a1c967e83994 Mon Sep 17 00:00:00 2001 From: Joshua Luckey Date: Mon, 25 Sep 2023 22:28:58 +0200 Subject: [PATCH 2/2] add deleted lines --- exercises-toolbox/4-scipy/4-curve_fit/aufgabe.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/exercises-toolbox/4-scipy/4-curve_fit/aufgabe.txt b/exercises-toolbox/4-scipy/4-curve_fit/aufgabe.txt index c9a92760..9a23a558 100644 --- a/exercises-toolbox/4-scipy/4-curve_fit/aufgabe.txt +++ b/exercises-toolbox/4-scipy/4-curve_fit/aufgabe.txt @@ -1,3 +1,6 @@ +# curve_fit + +Aufgabe: 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.