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 τ=RC\tau = RC, 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,

ri=Vi−Vmodel(ti).r_i = V_i - V_\text{model}(t_i).

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, V=a+btV = a + bt. Fitting a line has real advantages: the best-fit aa and bb come from a closed-form formula, and the uncertainties in aa and bb 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 ±0.1 V\pm 0.1\ \text{V} (shown with error bars).

0123450246810time t (s)capacitor voltage V (V)+1.50−1.5residuals (V)

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 aa and bb. 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

V(t)=Vb(1−e−t/τ),V(t) = V_b\left(1 - e^{-t/\tau}\right),

which has two parameters with physical meaning: VbV_b, the battery voltage the capacitor is heading toward, and τ\tau, the time it takes to cover about 63%63\% of the remaining “distance”. If you switch the fit model above, and the residuals scatter about zero and lie mostly inside the ±0.1 V\pm 0.1\ \text{V} band.

There is no formula that hands you the best VbV_b and τ\tau. 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 Vb−V=Vbe−t/τV_b - V = V_b e^{-t/\tau}, a graph of ln⁡(Vb−V)\ln(V_b - V) against tt is a straight line with slope −1/τ-1/\tau. This works, but poorly. You need VbV_b before you can plot anything, even though it is one of the things you are trying to fit. Late in the charge, Vb−VV_b - V 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 τ\tau is not a perfect number. Slightly different values of τ\tau would have described the data almost as well. The uncertainty in τ\tau should quantify the range of values that still describe the data acceptably, and you can find it directly: leave the best fit, increase τ\tau 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 t0t_0. The model gains a third parameter:

V(t)={0t≤t0Vb(1−e−(t−t0)/τ)t>t0V(t) = \begin{cases} 0 & t \le t_0 \\ V_b\left(1 - e^{-(t - t_0)/\tau}\right) & t > t_0 \end{cases}

Note that the three fit parameters are linked. A larger τ\tau makes the curve rise more slowly, which can be partly compensated by a larger VbV_b. Delaying the start time also has a similar effect on the curve as a larger τ\tau. When adjusting τ\tau 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.

0123450246810time t (s)capacitor voltage V (V)+0.60−0.6residuals (V)

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 χ2\chi^2

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 σi\sigma_i, square it so that positive and negative residuals do not cancel, and add them up:

χ2=∑i(Vi−Vmodel(ti)σi)2.\chi^2 = \sum_i \left(\frac{V_i - V_\text{model}(t_i)}{\sigma_i}\right)^2.

This reframes the fitting process into an optimization problem: the best-fit parameters (contained in the model function) are the ones that make χ2\chi^2 as small as possible. Furthermore, we can make some quantitative statements about uncertainty. A data point that “misses” by about one σ\sigma adds about 11 to this sum, so we expect a good fit to NN points with pp parameters has (χ2)min≈N−p(\chi^2)_\text{min} \approx N - p (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 χ2\chi^2 rises by 11 above its minimum.

Problem Solving

Reporting the time constant

Problem

Fitting Vb(1−e−t/τ)V_b\left(1-e^{-t/\tau}\right) to a charging curve and re-adjusting VbV_b at each step, you find the residuals still look random for τ\tau from 2.10 s2.10\ \text{s} to 2.24 s2.24\ \text{s}. Report τ\tau.

Use

The best estimate is the middle of the acceptable range and the uncertainty is its half-width.

Solve

τ=2.10+2.242=2.17 s,δτ=2.24−2.102=0.07 s.\tau = \frac{2.10 + 2.24}{2} = 2.17\ \text{s}, \qquad \delta\tau = \frac{2.24 - 2.10}{2} = 0.07\ \text{s}.

So τ=2.17±0.07 s\tau = 2.17 \pm 0.07\ \text{s}.

Check

The uncertainty is rounded to one significant figure and the value to the same decimal place, as before. An uncertainty of 0.07 s0.07\ \text{s} is about 3%3\% of τ\tau, plausible for twenty points with ±0.1 V\pm 0.1\ \text{V} error bars.

Does the fit agree with RC?

Problem

The circuit used R=100±5 kΩR = 100 \pm 5\ \text{k}\Omega and C=22±2 μFC = 22 \pm 2\ \mu\text{F}. Does the fitted τ=2.17±0.07 s\tau = 2.17 \pm 0.07\ \text{s} agree with τ=RC\tau = RC?

Use

RCRC is a product, so relative uncertainties add. Then check whether the fitted range overlaps the predicted one.

Solve

RC=(100 kΩ)(22 μF)=2.2 s,δ(RC)RC=5100+222=5.0%+9.1%=14%,RC = (100\ \text{k}\Omega)(22\ \mu\text{F}) = 2.2\ \text{s}, \qquad \frac{\delta(RC)}{RC} = \frac{5}{100} + \frac{2}{22} = 5.0\% + 9.1\% = 14\%,

so RC=2.2±0.3 sRC = 2.2 \pm 0.3\ \text{s}. The two ranges, [2.10, 2.24][2.10,\ 2.24] and [1.9, 2.5][1.9,\ 2.5], overlap, and the discrepancy is ∣2.17−2.2∣0.3≈0.1\tfrac{|2.17 - 2.2|}{0.3} \approx 0.1 uncertainties.

Verdict

Consistent — but notice which uncertainty is doing the limiting. The fit pins τ\tau down about four times better than the component tolerances do, so this test can only be as sharp as the parts you used. Measuring RR and CC more carefully, rather than collecting more voltage readings, is what would sharpen the comparison.

Fitting Checkpoint1 of 4

You fit a straight line to charging-capacitor data. The slope and intercept come with small uncertainties, but the residuals follow a smooth arch. What does that tell you?