-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from pep-dortmund/data_to_fitting_exercises
add polyfit exercise and data to uncertainties curve_fit wrapper
- Loading branch information
Showing
18 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
File renamed without changes.
5 changes: 2 additions & 3 deletions
5
...s-toolbox/4-scipy/3-curve_fit/aufgabe.txt → ...s-toolbox/4-scipy/4-curve_fit/aufgabe.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters