11 KiB
11 KiB
In [ ]:
import numpy as np
from scipy.optimize import curve_fitIn [ ]:
import matplotlib.pyplot as plt
%matplotlib inlineIn [ ]:
x = np.random.uniform(0., 100., 100)
y = 3. * x + 2. + np.random.normal(0., 10., 100)
plt.plot(x, y, '.')In [ ]:
def line(x, a, b):
return a * x + bIn [ ]:
popt, pcov = curve_fit(line, x, y)In [ ]:
poptIn [ ]:
e = np.repeat(10., 100)
plt.errorbar(x, y, yerr=e, fmt="none")In [ ]:
popt, pcov = curve_fit(line, x, y, sigma=e, absolute_sigma=True)In [ ]:
poptIn [ ]:
print("a =", popt[0], "+/-", pcov[0,0]**0.5)
print("b =", popt[1], "+/-", pcov[1,1]**0.5)In [ ]:
plt.errorbar(x, y, yerr=e, fmt="none")
xfine = np.linspace(0., 100., 100) # define values to plot the function for
plt.plot(xfine, line(xfine, popt[0], popt[1]), 'r-')In [ ]:
plt.errorbar(x, y, yerr=e, fmt="none")
plt.plot(xfine, line(xfine, *popt), 'r-')In [ ]:
x = np.random.uniform(0., 10., 100)
y = np.polyval([1, 2, -3], x) + np.random.normal(0., 10., 100)
e = np.random.uniform(5, 10, 100)In [ ]:
# your solution hereIn [ ]:
# The following code reads in the file and removes bad values
import numpy as np
date, temperature = np.loadtxt('data/munich_temperatures_average_with_bad_data.txt', unpack=True)
keep = np.abs(temperature) < 90
date = date[keep]
temperature = temperature[keep]
plt.plot(date,temperature)In [ ]:
# your solution here