Fitting Models Exercise 2

Imports


In [3]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as opt

Fitting a decaying oscillation

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 values
  • ydata: an array of y values
  • dy: the absolute uncertainties (standard deviations) in y

Your 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 [4]:
def modl(t,A,o,l,d):
    return A*np.exp(-1*t)*np.cos(o*t)+d

In [5]:
thetabest,thetacov=opt.curve_fit(modl,tdata,ydata,np.array((6,1,1,0)),dy,absolute_sigma=True)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-d89d35998462> in <module>()
----> 1 thetabest,thetacov=opt.curve_fit(modl,tdata,ydata,np.array((6,1,1,0)),dy,absolute_sigma=True)

NameError: name 'tdata' is not defined

In [ ]:
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:

  • Print the parameters estimates and uncertainties.
  • Plot the raw and best fit model.
  • You will likely have to pass an initial guess to curve_fit to get a good fit.
  • Treat the uncertainties in $y$ as absolute errors by passing absolute_sigma=True.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()

In [ ]:
assert True # leave this cell for grading the fit; should include a plot and printout of the parameters+errors