Fitting Data
Suppose you charge a capacitor through a resistor and read its voltage with a voltmeter every half second. You end up with a list of numbers, but what you want is a single number: the time constant , which says how quickly the capacitor charges. A fit is how you get from one to the other. You choose a model (an equation with adjustable parameters), then find the parameter values that bring the curve closest to the data.
“Closest” is made precise with residuals: the residual of a data point is the measured value minus the model’s value at that same time,
Whatever the model, plot the residuals and check for two things:
- Random scatter about zero, with a size comparable to the error bars
- A systematic pattern (an arch, a trend, a run of points all on one side): the model is wrong, or the parameters are not at their best values.
The first model most of us reach for is a straight line, . Fitting a line has real advantages: the best-fit and come from a closed-form formula, and the uncertainties in and do too. For data that really are linear, such as distance against time at constant speed, nothing more is needed.
Try it on the data below. Each voltmeter reading has an uncertainty of (shown with error bars).
a = 2.26 ± 0.05 V, b = 0.335 ± 0.008 V/s.
The line follows the overall trend in rising voltage, and the software reports confident-looking values of and . However, many residuals are much larger than the reported uncertainties, and they do not appear randomly scattered about zero. This indicates that we have the wrong model. Charging slows down as the capacitor fills, so we need a different fitting function.
A functional fit
In the ideal theoretical case, a capacitor charging toward the battery voltage follows
which has two parameters with physical meaning: , the battery voltage the capacitor is heading toward, and , the time it takes to cover about of the remaining “distance”. If you switch the fit model above, and the residuals scatter about zero and lie mostly inside the band.
There is no formula that hands you the best and . Instead the computer (or you, in Desmos) adjusts them iteratively until the curve can’t be improved.
It is tempting to dodge this by straightening the data. Since , a graph of against is a straight line with slope . This works, but poorly. You need before you can plot anything, even though it is one of the things you are trying to fit. Late in the charge, is tiny, so the same voltmeter noise becomes a huge relative error (and can even make the logarithm undefined). Taking the log also distorts the error bars. Fitting the original curve avoids all of this.
Uncertainty by hand
Your best-fit is not a perfect number. Slightly different values of would have described the data almost as well. The uncertainty in should quantify the range of values that still describe the data acceptably, and you can find it directly: leave the best fit, increase until the residuals clearly develop a pattern, and note where that happened. Then do the same in the other direction. Half the distance between these two extreme values is a reasonable estimate of uncertainty.
Real data rarely start at the instant the switch closes. In the run below, recording began early, so the first few readings sit at zero until the capacitor starts charging at some time . The model gains a third parameter:
Note that the three fit parameters are linked. A larger makes the curve rise more slowly, which can be partly compensated by a larger . Delaying the start time also has a similar effect on the curve as a larger . When adjusting to estimate the uncertainty, you should also re-adjust the other free parameters before judging the residuals.
If you find this process tedious, you are not alone! Luckily, there are ways of automating this with a computer. A common method uses the so-called chi-square statistic, which is discussed below.
Slide τ until the residuals stop looking like random scatter, and mark that edge. Then slide the other way. Re-tune Vb and t0 each time.
A first look at
Manually adjusting fit parameters by hand is subjective, and as the number of free parameters increases beyond two, this process can get rather unwieldy. The chi-square statistic allows us to make this fitting process more objective. For each point, measure the residual in units of that point’s own uncertainty , square it so that positive and negative residuals do not cancel, and add them up:
This reframes the fitting process into an optimization problem: the best-fit parameters (contained in the model function) are the ones that make as small as possible. Furthermore, we can make some quantitative statements about uncertainty. A data point that “misses” by about one adds about to this sum, so we expect a good fit to points with parameters has (the minus p requires a bit of thought).
The uncertainty in each best-fit parameter has a clean rule: the uncertainty in a parameter is how far you can adjust it, re-optimizing the others, before rises by above its minimum.
Problem Solving
Reporting the time constant
Reporting the time constant
Problem
Fitting to a charging curve and re-adjusting at each step, you find the residuals still look random for from to . Report .
Use
The best estimate is the middle of the acceptable range and the uncertainty is its half-width.
Solve
So .
Check
The uncertainty is rounded to one significant figure and the value to the same decimal place, as before. An uncertainty of is about of , plausible for twenty points with error bars.
Does the fit agree with RC?
Does the fit agree with RC?
Problem
The circuit used and . Does the fitted agree with ?
Use
is a product, so relative uncertainties add. Then check whether the fitted range overlaps the predicted one.
Solve
so . The two ranges, and , overlap, and the discrepancy is uncertainties.
Verdict
Consistent — but notice which uncertainty is doing the limiting. The fit pins down about four times better than the component tolerances do, so this test can only be as sharp as the parts you used. Measuring and more carefully, rather than collecting more voltage readings, is what would sharpen the comparison.
Fitting Checkpoint1 of 4