Being able to fit a model to experimental data is an extremely important laboratory skill. Most physics students are familiar with linear curve fitting, often with a spreadsheet or data-collection software such as _Data Studio_ (PASCO Scientific) or _Logger Pro_ (Vernier Software).
This set of exercises takes students beyond these introductory tools into the realm of arbitrary-function curve fitting, with error bars and estimates of parameter uncertainties.

Subject Areas

Mathematical/Numerical Methods and Experimental Labs

Level

Beyond the First Year

Available Implementation

Python

Learning Objectives

Students who complete this module will gain experience in
* Reading data files into the computer.
* Fitting data to arbitrary functions.
* Using uncertainty in data points as part of the calculation of the curve fit.
* Reporting parameter values _with uncertainty_.

Login or register as a verified educator for free to access instructor-only material.

Login or register as a verified educator for free to access instructor-only material.

These exercises are not tied to a specific programming language. Example implementations are provided under the Code tab, but the Exercises can be implemented in whatever platform you wish to use (e.g., Excel, Python, MATLAB, etc.).

## Exercise 1: Linear fit
The data set "calibration.txt" shows the _reported_ position of a rotational sensor (in units of $\frac{1}{1024}$ of a rotation) after $N$ revolutions. If the rotational sensor is properly calibrated, this should be a horizontal line at 0, but it's not.
* By how much per revolution is the sensor miscalibrated? Plot a graph of the data, with a linear curve fit, to answer. Be sure to include errorbars on the graph, and report the uncertainty in your fit parameters.
## Exercise 2: Power fit
Students in a first-semester physics course collected this data showing the angular velocity of a rotating "point mass" $m$ and the force $F_c$ required to keep the mass rotating in a circle of radius $R$. If you plot this data, it _looks_ linear (except for that one point at (0,0)) but for theoretical reasons we believe that the equation for centripetal force should be
$$ F_c = mR\omega^2 $$
* Does this data fit the model for $F_c$? Plot a graph to support your answer.
* Statistically, the errorbars should intersect the curve fit for about 63% of the data points: Are your errorbars reasonable?
* The rotating mass had $m=200$ grams, and the radius of rotation was $R=18$ cm. Is this consistent with parameters from your curve fit?
## Exercise 3: Exponential fit
Barium 137 is radioactive. Activity of a sample of Barium 137 was measured as a function of time, and the results are shown in file 'decay.txt'. We would expect that for radioactive decay,
$$ N = N_o e^{-t/\tau} $$
where $\tau= \frac{t_{1/2}}{\ln(2)}$.
* Find the half-life $t_{1/2}$ for Barium 137. Support your answer with a graph and a curve fit, of course!
## Exercise 4: Challenge!
The data in file damped_oscillation.txt is shows the position of a magnetic rotor in a fixed magnetic field. One model for the behavior of this rotor would be "exponentially-damped oscillation",
$$ \theta = \theta_o e^{-\beta t} \cos(\omega t + \phi) + \varphi $$
Find the resonant frequency $\omega$ and the damping constant $\beta$ for this apparatus. You may assume the uncertainty in position is negligible.
_Hint: Your initial guesses need to be in the right ballpark!_

Code

Login or register as a verified educator for free to access instructor-only material.

Solutions

Login or register as a verified educator for free to access instructor-only material.

Decay experiment issues

Author: David Jackson
Posted: September 24, 2018 at 10:32AM

There are two issues with the decay experiment. The first is simply a typo: the data is listed as being in seconds in the data file when it should be in minutes.

The second issue is more problematic. If one uses a model of the form $N = N_0e^{-t/\tau}$ (as discussed in the instructor materials), the half-life estimate comes out to be $t_{1/2} = (\ln 2) \tau = 2.85 \pm 0.03$ min, which is quite a bit off from the accepted value of $2.552 \pm 0.001$ min.

However, if one includes a background fit parameter in the model so that $N = B + N_0e^{-t/\tau}$, then the fit leads to a half-life value of $t_{1/2} = 2.55 \pm 0.18$, which is consistent with the accepted value.

Problem with error estimates

Author: David Jackson
Posted: September 13, 2018 at 10:43AM

This is a very nice exercise set. However, it seems that you need to include the parameter absolute_sigma=True when calling curve_fit whenever the errors are actual errors (i.e. they have the same units as the measured values). This seems important, as it can change the results significantly.

For example, increasing the size of the errors in the supplied data files has no effect on the estimated errors on the fit parameters, no matter how large you make the errors. Obviously, this makes no sense. However, setting absolute_sigma=True makes the routine work as expected.

More information is available in the scipy.optimize.curve_fit documentation at: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html