In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as opt
For this problem you are given a raw dataset in the file decay_osc.npz
. This file contains three arrays:
tdata
: an array of time valuesydata
: an array of y valuesdy
: the absolute uncertainties (standard deviations) in yYour job is to fit the following model to this data:
$$ y(t) = A e^{-\lambda t} \cos{\omega t + \delta} $$First, import the data using NumPy and make an appropriately styled error bar plot of the raw data.
In [9]:
data=np.load("decay_osc.npz")
T=data["tdata"]
Y=data["ydata"]
dy=data["dy"]
f=plt.figure(figsize=(15,10))
plt.errorbar(T,Y,yerr=dy,fmt='o');
In [10]:
assert True # leave this to grade the data import and raw data plot
Now, using curve_fit
to fit this model and determine the estimates and uncertainties for the parameters:
curve_fit
to get a good fit.absolute_sigma=True
.
In [14]:
par_est=[0,0,0,0]
popt, popc = opt.curve_fit(lambda t,A,lam,ome,delt:A*np.exp(-lam*t)*np.cos(ome*t+delt), T, Y, par_est, sigma=dy, absolute_sigma=True)
print(list(zip(popt, np.diag(popc))))
f=plt.figure(figsize=(15,10))
plt.errorbar(T,Y,yerr=dy,fmt='o')
Tx=np.linspace(0,20, 1000)
plt.plot(Tx,popt[0]*np.exp(-popt[1]*Tx)*np.cos(-popt[2]*Tx+popt[3]));
In [ ]:
assert True # leave this cell for grading the fit; should include a plot and printout of the parameters+errors