---
title: "Project: characterizing a capacitor discharge"
heading: From a noisy record to a time constant, an energy and a deadline
eyebrow: A5 · Lesson 55 · Project: characterizing a capacitor discharge
xp: 100
---

::: hook
A capacitor of unknown value discharges through a known 1 kΩ resistor, and a logger records its voltage every half millisecond, with 1% noise on each reading. From that record alone you'll find the time constant and the starting voltage, the energy the resistor absorbed, the moment the voltage fell below 1 V, and the current partway through. Every part of the course comes in: logarithms and a straight-line fit, integrals of data, an exponential's tail, and a derivative of noisy samples.
:::

::: play
- The plot shows $\ln v$ against time for a discharge from 9 V. A pure exponential turns into a straight line, of slope $-1/\tau$ and intercept $\ln V_0$.
- Change the time constant (in ms) and watch the slope change. The readout gives the slope.
- Noise in the readings scatters points about this line; a straight-line fit through all of them recovers the slope and intercept.
:::

```sim plot
view: graph
x: t = 0..50m s
y: lnv = -3..2.5
slider: tau = 5..20 = 10
plot: ln(9) - t/(tau*1e-3) | samples: 200 | label: ln v
readout: slope, per second = -1/(tau*1e-3) | digits: 4
```

::: predict
## The fit gives $\ln v = 2.197 - 100.0\,t$, with $t$ in seconds. What are the time constant and the starting voltage?

- [x] 10 ms and 9.0 V
- [ ] 100 ms and 2.2 V
- [ ] 10 ms and 2.2 V
- [ ] 0.01 ms and 9.0 V

::: reveal
10 ms and 9.0 V. The slope is $-1/\tau$, so $\tau = 1/100$ s = 10 ms. The intercept is $\ln V_0$, so $V_0 = e^{2.197} = 9.0$ V. The resistor is 1 kΩ, so the capacitor is $\tau/R = 10$ µF.
:::
:::

::: nameit
## The plan

1. **Fit.** Take $y_k = \ln v_k$ and fit a straight line $y = c + st$ by least squares:
$$s = \frac{\sum(t_k - \bar t)(y_k - \bar y)}{\sum(t_k - \bar t)^2}, \qquad c = \bar y - s\bar t$$
Then $\tau = -1/s$, $V_0 = e^c$ and $C = \tau/R$.
2. **Energy.** The resistor absorbs $\int v^2/R\,dt$. Integrate the record by Simpson's rule, add the tail past the end of the record from the fit, $\frac{V_0^2\tau}{2R}e^{-2T/\tau}$, and compare with the energy the capacitor started with, $\tfrac12CV_0^2$.
3. **Deadline.** The fit says $v$ falls to 1 V at $\tau\ln(V_0/1)$. The data say it at the first sample below 1 V, interpolated with the one before.
4. **Current.** At 10 ms, compare $-C\frac{v_{k+1} - v_{k-1}}{2\Delta t}$ from the data with $\frac{V_0}{R}e^{-t/\tau}$ from the fit.

## Why fit in logs

Fitting a line to $\ln v$ turns a curve fit into the straight-line formulas above. The price is that 1% noise on $v$ becomes about 0.01 of noise on $\ln v$ at every sample, so late samples, where $v$ is small, count as much as early ones. With multiplicative noise like this, that's fair; with a fixed noise floor it would give the tail too much weight.
:::

::: example
## A fit returns $s = -102.0$ per second and $c = 2.20$. What are $\tau$, $V_0$ and $C$ with $R$ = 1 kΩ, and when does the voltage reach 1 V?

::: solution
1. **Time constant.**
   $$\begin{aligned} \tau &= 1/102.0 \\ &= 9.80\ \text{ms} \end{aligned}$$
2. **Starting voltage.**
   $$\begin{aligned} V_0 &= e^{2.20} \\ &= 9.03\ \text{V} \end{aligned}$$
3. **Capacitance.**
   $$\begin{aligned} C &= \tau/R \\ &= 9.80\ \mu\text{F} \end{aligned}$$
4. **Time to reach 1 V.**
   $$\begin{aligned} t &= \tau\ln(9.03) \\ &= 9.80 \times 2.200 \\ &= 21.6\ \text{ms} \end{aligned}$$

**Answer:** $\tau = 9.80$ ms, $V_0 = 9.03$ V and $C = 9.80$ µF, and the voltage reaches 1 V at 21.6 ms.
:::
:::

::: example
## The record is 50 ms long, five time constants of 10 ms. How much energy does it leave out?

::: solution
1. **Energy in the tail.** The tail past $T$ = 50 ms holds
   $$\begin{aligned} E &= \frac{V_0^2\tau}{2R}e^{-2T/\tau} \\ &= \frac{81 \times 0.01}{2000}e^{-10} \\ &= 1.8 \times 10^{-8}\ \text{J} \end{aligned}$$
2. **Compare with the total.** Against a total of 0.405 mJ, that's 0.005%.
3. **Why so little.** Five time constants capture all but $e^{-10}$ of the energy, since the energy decays twice as fast as the voltage.

**Answer:** the record leaves out $1.8 \times 10^{-8}$ J, 0.005% of the 0.405 mJ total.
:::
:::

::: check
given: s = -50 | -100 | -200; c = 1.6 | 2.2 | 2.5
answer: -1000/s
unit: ms
tolerance: 0.5%

## A straight-line fit to $\ln v$ against $t$ (in seconds) has slope {s} and intercept {c}. What is the time constant?

::: right
Right. $\tau = -1/s$ = {answer} ms.
:::

::: misses
- [1000/s] Negative: a decay has a negative slope, and $\tau$ is positive.
- [-1/s] That's in seconds. Convert to milliseconds.
- [any] Not yet. The slope of $\ln v$ is $-1/\tau$.
:::
:::

::: check
given: s = -50 | -100 | -200; c = 1.6 | 2.2 | 2.5
answer: -c/s*1000
unit: ms
tolerance: 0.5%

## With the same fit (slope {s}, intercept {c}), when does the voltage fall to 1 V?

::: right
Right. $\tau\ln(V_0/1) = \frac{c}{-s}$ = {answer} ms.
:::

::: misses
- [any] Not yet. $\ln v = c + st$ reaches $\ln 1 = 0$ at $t = -c/s$.
:::
:::

::: check
kind: python

## The starter makes the logged record `t`, `v`. Fit a line to $\ln v$ by least squares (write the formulas yourself) and set `tau`, `V0` and `C` (with `R` = 1000 Ω).

```python starter
import math, random
random.seed(5)
R = 1000
t = [k * 0.5e-3 for k in range(101)]
v = [9 * math.exp(-x / 10e-3) * (1 + random.gauss(0, 0.01)) for x in t]

tau = V0 = C = 0
print(tau, V0, C)
```

```python solution
import math, random
random.seed(5)
R = 1000
t = [k * 0.5e-3 for k in range(101)]
v = [9 * math.exp(-x / 10e-3) * (1 + random.gauss(0, 0.01)) for x in t]

y = [math.log(a) for a in v]
tb, yb = sum(t) / len(t), sum(y) / len(y)
s = sum((a - tb) * (b - yb) for a, b in zip(t, y)) / sum((a - tb) ** 2 for a in t)
c = yb - s * tb
tau = -1 / s
V0 = math.exp(c)
C = tau / R
print(tau, V0, C)
```

```python tests
assert abs(tau - 10e-3) < 2e-4, f"The time constant should come out near 10 ms; got {tau * 1e3:.3f} ms."
assert abs(V0 - 9) < 0.1, f"The starting voltage should come out near 9 V; got {V0:.3f} V."
assert abs(C - tau / 1000) < 1e-15, "C should be tau / R."
import math
y = [math.log(a) for a in v]
n = len(t); tb = sum(t) / n; yb = sum(y) / n
s_want = sum((a - tb) * (b - yb) for a, b in zip(t, y)) / sum((a - tb) ** 2 for a in t)
assert abs(-1 / tau - s_want) < 1e-6 * abs(s_want), "tau should come from the least-squares slope of ln v."
```

::: right
Right. The fit recovers about 10.004 ms from noisy data, because 101 points average the noise down.
:::
:::

::: check
kind: python

## Using the record and your fit (copied into the starter), set `E_data` to $\int v^2/R\,dt$ over the record by Simpson's rule, `E_tail` to the fit's tail past the end, $\frac{V_0^2\tau}{2R}e^{-2T/\tau}$, and `E_cap` to $\tfrac12CV_0^2$.

```python starter
import math, random
random.seed(5)
R = 1000
t = [k * 0.5e-3 for k in range(101)]
v = [9 * math.exp(-x / 10e-3) * (1 + random.gauss(0, 0.01)) for x in t]
y = [math.log(a) for a in v]
tb, yb = sum(t) / len(t), sum(y) / len(y)
s = sum((a - tb) * (b - yb) for a, b in zip(t, y)) / sum((a - tb) ** 2 for a in t)
tau, V0 = -1 / s, math.exp(yb - s * tb)
C = tau / R

E_data = E_tail = E_cap = 0
print(E_data, E_tail, E_cap)
```

```python solution
import math, random
random.seed(5)
R = 1000
t = [k * 0.5e-3 for k in range(101)]
v = [9 * math.exp(-x / 10e-3) * (1 + random.gauss(0, 0.01)) for x in t]
y = [math.log(a) for a in v]
tb, yb = sum(t) / len(t), sum(y) / len(y)
s = sum((a - tb) * (b - yb) for a, b in zip(t, y)) / sum((a - tb) ** 2 for a in t)
tau, V0 = -1 / s, math.exp(yb - s * tb)
C = tau / R

h = 0.5e-3
p = [a * a / R for a in v]
E_data = h / 3 * (p[0] + p[-1] + 4 * sum(p[1:-1:2]) + 2 * sum(p[2:-1:2]))
E_tail = V0**2 * tau / (2 * R) * math.exp(-2 * t[-1] / tau)
E_cap = 0.5 * C * V0**2
print(E_data, E_tail, E_cap)
```

```python tests
assert abs((E_data + E_tail) / E_cap - 1) < 0.02, f"The resistor should absorb about the energy the capacitor held; got {E_data + E_tail:.4e} against {E_cap:.4e} J."
assert E_tail < 1e-4 * E_cap, "Five time constants leave almost nothing in the tail."
assert abs(E_cap - 0.5 * C * V0**2) < 1e-15, "E_cap should be (1/2) C V0^2."
```

::: right
Right. The record's energy matches the capacitor's to about 1%, the size the noise allows.
:::
:::

::: check
kind: python

## With the same record and fit, set `t_fit` to the time the fit says the voltage reaches 1 V, and `t_data` to the first crossing below 1 V in the data, interpolated linearly between that sample and the one before.

```python starter
import math, random
random.seed(5)
R = 1000
t = [k * 0.5e-3 for k in range(101)]
v = [9 * math.exp(-x / 10e-3) * (1 + random.gauss(0, 0.01)) for x in t]
y = [math.log(a) for a in v]
tb, yb = sum(t) / len(t), sum(y) / len(y)
s = sum((a - tb) * (b - yb) for a, b in zip(t, y)) / sum((a - tb) ** 2 for a in t)
tau, V0 = -1 / s, math.exp(yb - s * tb)

t_fit = t_data = 0
print(t_fit, t_data)
```

```python solution
import math, random
random.seed(5)
R = 1000
t = [k * 0.5e-3 for k in range(101)]
v = [9 * math.exp(-x / 10e-3) * (1 + random.gauss(0, 0.01)) for x in t]
y = [math.log(a) for a in v]
tb, yb = sum(t) / len(t), sum(y) / len(y)
s = sum((a - tb) * (b - yb) for a, b in zip(t, y)) / sum((a - tb) ** 2 for a in t)
tau, V0 = -1 / s, math.exp(yb - s * tb)

t_fit = tau * math.log(V0 / 1)
k = next(i for i, a in enumerate(v) if a < 1)
t_data = t[k - 1] + (v[k - 1] - 1) / (v[k - 1] - v[k]) * (t[k] - t[k - 1])
print(t_fit, t_data)
```

```python tests
import math
true = 10e-3 * math.log(9)
assert abs(t_fit - true) < 1e-4, f"The fit's crossing should be near {true * 1e3:.3f} ms; got {t_fit * 1e3:.3f} ms."
assert abs(t_data - true) < 2e-4, f"The data's crossing should be near {true * 1e3:.3f} ms; got {t_data * 1e3:.3f} ms."
k = next(i for i, a in enumerate(v) if a < 1)
assert t[k - 1] <= t_data <= t[k], "The interpolated crossing must lie between the two samples around it."
```

::: right
Right. The two agree to a few hundredths of a millisecond; the fit is steadier because it uses every sample, not just two.
:::
:::

::: check
kind: python

## At $t$ = 10 ms (sample 20), set `i_data` to the current from a central difference of the data, $-C\frac{v_{21} - v_{19}}{2\Delta t}$, and `i_fit` to $\frac{V_0}{R}e^{-t/\tau}$ from the fit. Then set `true` to the noise-free value, $\frac{9}{1000}e^{-1}$, and `better` to `"fit"` or `"data"`, whichever is closer.

```python starter
import math, random
random.seed(5)
R = 1000
t = [k * 0.5e-3 for k in range(101)]
v = [9 * math.exp(-x / 10e-3) * (1 + random.gauss(0, 0.01)) for x in t]
y = [math.log(a) for a in v]
tb, yb = sum(t) / len(t), sum(y) / len(y)
s = sum((a - tb) * (b - yb) for a, b in zip(t, y)) / sum((a - tb) ** 2 for a in t)
tau, V0 = -1 / s, math.exp(yb - s * tb)
C = tau / R

i_data = i_fit = true = 0
better = ""
print(i_data, i_fit, true, better)
```

```python solution
import math, random
random.seed(5)
R = 1000
t = [k * 0.5e-3 for k in range(101)]
v = [9 * math.exp(-x / 10e-3) * (1 + random.gauss(0, 0.01)) for x in t]
y = [math.log(a) for a in v]
tb, yb = sum(t) / len(t), sum(y) / len(y)
s = sum((a - tb) * (b - yb) for a, b in zip(t, y)) / sum((a - tb) ** 2 for a in t)
tau, V0 = -1 / s, math.exp(yb - s * tb)
C = tau / R

i_data = -C * (v[21] - v[19]) / (2 * 0.5e-3)
i_fit = V0 / R * math.exp(-t[20] / tau)
true = 9 / 1000 * math.exp(-1)
better = "fit" if abs(i_fit - true) < abs(i_data - true) else "data"
print(i_data, i_fit, true, better)
```

```python tests
import math
assert abs(true - 0.0033109) < 1e-6, "The noise-free current at 10 ms is 3.311 mA."
assert abs(i_fit - true) / true < 0.01, f"The fit's current should be within 1% of 3.311 mA; got {i_fit * 1e3:.4f} mA."
assert abs(i_data - true) / true > 0.05, "Differencing noisy samples should be noticeably off."
assert better == "fit", "The fit uses all the samples; the difference uses two noisy ones."
```

::: right
Right. The difference is off by about 29% here, while the fit is within 0.1%: differentiate a model, not raw noisy data.
:::
:::

::: check
kind: python

## How noisy were the readings? Using the fit from the starter, set `res` to the residuals $\ln v_k - (c + st_k)$, and `noise` to their standard deviation (the $n - 2$ formula, since the fit used two parameters). For 1% noise on $v$, it should come out near 0.01.

```python starter
import math, random
random.seed(5)
t = [k * 0.5e-3 for k in range(101)]
v = [9 * math.exp(-x / 10e-3) * (1 + random.gauss(0, 0.01)) for x in t]
y = [math.log(a) for a in v]
tb, yb = sum(t) / len(t), sum(y) / len(y)
s = sum((a - tb) * (b - yb) for a, b in zip(t, y)) / sum((a - tb) ** 2 for a in t)
c = yb - s * tb

res = []
noise = 0
print(noise)
```

```python solution
import math, random
random.seed(5)
t = [k * 0.5e-3 for k in range(101)]
v = [9 * math.exp(-x / 10e-3) * (1 + random.gauss(0, 0.01)) for x in t]
y = [math.log(a) for a in v]
tb, yb = sum(t) / len(t), sum(y) / len(y)
s = sum((a - tb) * (b - yb) for a, b in zip(t, y)) / sum((a - tb) ** 2 for a in t)
c = yb - s * tb

res = [yk - (c + s * tk) for tk, yk in zip(t, y)]
noise = math.sqrt(sum(r * r for r in res) / (len(res) - 2))
print(noise)
```

```python tests
import math
assert len(res) == 101 and abs(sum(res)) < 1e-9, "Least-squares residuals sum to zero."
assert 0.007 < noise < 0.013, f"The residuals should show about 1% noise; got {noise:.4f}."
```

::: right
Right. The residuals recover the noise level, which is how you'd know the fit's uncertainty without being told.
:::
:::

::: explain
## The project found the current at 10 ms two ways, and the fit won by a wide margin. When would you trust the central difference instead?

::: model
The fit wins here because the data really are an exponential plus small random noise, so a two-parameter model fitted to all 101 samples averages the noise away, while a central difference uses just two samples and divides their noise by a small time step, magnifying it. The difference would be the better choice when the model is wrong or unknown: if the discharge had a second, slower component, or the resistor heated and changed value, the exponential fit would describe the wrong curve, however precisely, and its derivative would be wrong everywhere. A difference makes no assumption about the shape. With noisy data the honest compromise is to smooth or fit locally (a few neighboring samples) and differentiate that, trading a little bias for much less noise.
:::
:::

::: practice

::: check
given: s = -1000..-10; c = 0.5..3
answer: -1000/s
unit: ms
tolerance: 0.5%

## Fit slope {s} per second: what is $\tau$ in ms?

::: misses
- [any] $\tau = -1/s$.
:::
:::

::: check
given: s = -1000..-10; c = 0.5..3
answer: -c/s*1000
unit: ms
tolerance: 0.5%

## Fit $\ln v = \given{c} + (\given{s})t$: when does $v$ reach 1 V, in ms?

::: misses
- [any] $t = -c/s$.
:::
:::
:::
