---
title: "Biopotential lab: membranes, cables, electrodes and leads"
heading: Resting potentials from the Goldman equation under changing potassium, a finite-difference cable solved against its exact cosh profile, electrode impedances turning common-mode voltage into differential error, and all six limb leads with the axis from two
eyebrow: S15 · Lesson 19 · Biopotential lab
xp: 60
---

::: hook
Biopotentials start as ion gradients across membranes, spread along cells as through a leaky cable, reach the skin as millivolt patterns, and enter an amplifier through electrodes that are anything but ideal. Lessons 10 to 18 took each step in turn. In this lab you'll compute each one numerically: the resting potential as concentrations change, the voltage along a finite nerve fiber, the interference an electrode mismatch lets in, and the full set of limb leads from the two an ECG actually measures.
:::

::: play
- The solid curve is the resting potential from the Goldman equation ($P_K : P_{Na} : P_{Cl}$ = 1 : 0.04 : 0.45) against extracellular potassium; the dashed curve is the potassium Nernst potential.
- At normal potassium (4 mM) the membrane sits 20 mV above $E_K$, pulled up by sodium. As potassium rises, both rise, but the resting potential less steeply.
- Raising the sodium permeability pulls the resting potential further from $E_K$.
:::

```sim plot
view: graph
x: K = 1..40 log
y: V = -110..-20
slider: p = 0.01..0.2 = 0.04
plot: 26.73*ln((K + p*145 + 0.45*4)/(140 + p*12 + 0.45*120)) | label: Goldman resting potential, mV
plot: 26.73*ln(K/140) | dashed | label: potassium Nernst potential, mV
```

::: predict
## Extracellular potassium doubles from 4 to 8 mM. The potassium Nernst potential rises 18.5 mV. The resting potential rises by

- [ ] More than 18.5 mV
- [ ] Exactly 18.5 mV
- [x] About 8 mV
- [ ] Not at all

::: reveal
The Goldman equation weights each ion by its permeability, and sodium and chloride keep pulling the potential where they want it, so the resting potential moves 7.9 mV (from −75.4 to −67.4 mV), less than half of $E_K$'s shift. Task 1 computes it.
:::
:::

::: nameit
## What the lab computes

- **Goldman**: $V = \frac{kT}q\ln\frac{P_K[K]_o + P_{Na}[Na]_o + P_{Cl}[Cl]_i}{P_K[K]_i + P_{Na}[Na]_i + P_{Cl}[Cl]_o}$, at 37 °C unless told otherwise.
- **The cable**: a steady voltage $V_0$ held at one end of a fiber of length L sealed at the other obeys $\lambda^2V'' = V$, with exact solution $V_0\cosh((L - x)/\lambda)/\cosh(L/\lambda)$. On a grid of spacing h, $\lambda^2(V_{i-1} - 2V_i + V_{i+1})/h^2 = V_i$ is a tridiagonal system, solved in one forward and one backward sweep; the sealed end uses a mirror node, $V_{n+1} = V_{n-1}$.
- **Electrodes**: each is $R_s + R_d/(1 + j\omega R_dC_d)$. A common-mode voltage $v_{cm}$ reaching an amplifier with input impedance $Z_{in}$ through electrodes $Z_1$ and $Z_2$ becomes a differential error $v_{cm}[Z_{in}/(Z_{in} + Z_1) - Z_{in}/(Z_{in} + Z_2)]$, about $v_{cm}|Z_1 - Z_2|/Z_{in}$.
- **Leads**: III = II − I, aVR = −(I + II)/2, aVL = I − II/2, aVF = II − I/2, and the frontal axis from I and aVF: $\tan\theta = (2/\sqrt3)\,\text{aVF}/\text{I}$.
:::

::: check
kind: python

## Write `ghk(K, Na, Cl, P=(1, 0.04, 0.45), T=310.15)`, where each ion is an (inside, outside) pair in mM, returning millivolts.

```python starter
import math

def ghk(K, Na, Cl, P=(1, 0.04, 0.45), T=310.15):
    return 26.73 * math.log(K[1] / K[0])
```

```python solution
import math

def ghk(K, Na, Cl, P=(1, 0.04, 0.45), T=310.15):
    VT = 1.380649e-23 * T / 1.602176634e-19 * 1000
    return VT * math.log((P[0] * K[1] + P[1] * Na[1] + P[2] * Cl[0]) / (P[0] * K[0] + P[1] * Na[0] + P[2] * Cl[1]))
```

```python tests
v4 = ghk((140, 4), (12, 145), (4, 120))
v8 = ghk((140, 8), (12, 145), (4, 120))
assert abs(v4 + 75.351) < 0.01 and abs(v8 + 67.433) < 0.01, "−75.4 mV at 4 mM, −67.4 mV at 8 mM."
assert abs(ghk((140, 4), (12, 145), (4, 120), P=(1, 0, 0)) + 95.023) < 0.01, "With only potassium permeable, it's the Nernst potential."
```

::: right
Right. The membrane is mostly a potassium electrode, but not entirely, which is why it never quite reaches $E_K$.
:::
:::

::: check
kind: python

## Write `cable(V0, lam, L, n)`: the steady voltage at the n + 1 grid points along a fiber of length L sealed at its far end, with $V_0$ held at x = 0, by finite differences.

```python starter
import math

def cable(V0, lam, L, n):
    return [V0 * math.exp(-k * L / n / lam) for k in range(n + 1)]
```

```python solution
def cable(V0, lam, L, n):
    h = L / n
    a = 1 / h ** 2
    sub = [a] * n
    diag = [-2 * a - 1 / lam ** 2] * n
    sup = [a] * n
    rhs = [0.0] * n
    rhs[0] -= a * V0
    sub[-1] = 2 * a
    c, d = [0.0] * n, [0.0] * n
    c[0], d[0] = sup[0] / diag[0], rhs[0] / diag[0]
    for i in range(1, n):
        m = diag[i] - sub[i] * c[i - 1]
        c[i] = sup[i] / m if i < n - 1 else 0.0
        d[i] = (rhs[i] - sub[i] * d[i - 1]) / m
    x = [0.0] * n
    x[-1] = d[-1]
    for i in range(n - 2, -1, -1):
        x[i] = d[i] - c[i] * x[i + 1]
    return [V0] + x
```

```python tests
import math
V = cable(20.0, 1.0, 2.0, 400)
exact = lambda x: 20 * math.cosh((2 - x) / 1) / math.cosh(2)
assert max(abs(V[i] - exact(i * 2 / 400)) for i in range(401)) < 1e-4, "Matches the cosh solution to 0.1 µV."
assert abs(V[-1] - 5.316) < 0.001, "The sealed end holds 5.32 mV, more than an infinite fiber's 2.71."
```

::: right
Right. A sealed end reflects the current back, so a short fiber holds more voltage at its far end than the exponential of an infinite one.
:::
:::

::: check
kind: python

## Write `electrode_z(Rs, Rd, Cd, f)` returning a complex impedance, and `cm_error(vcm, Z1, Z2, Zin)`, the magnitude of the differential error.

```python starter
def electrode_z(Rs, Rd, Cd, f):
    return Rs + Rd

def cm_error(vcm, Z1, Z2, Zin):
    return 0.0
```

```python solution
import math

def electrode_z(Rs, Rd, Cd, f):
    return Rs + Rd / (1 + 1j * 2 * math.pi * f * Rd * Cd)

def cm_error(vcm, Z1, Z2, Zin):
    return abs(vcm * (Zin / (Zin + Z1) - Zin / (Zin + Z2)))
```

```python tests
Z1 = electrode_z(1e3, 100e3, 0.1e-6, 60)
Z2 = electrode_z(1e3, 60e3, 0.1e-6, 60)
assert abs(abs(Z1) - 25913.6) < 1 and abs(abs(Z2) - 24682.0) < 1, "At 60 Hz the capacitance shunts most of R_d: 25.9 and 24.7 kΩ."
e = cm_error(10e-3, Z1, Z2, 10e6)
assert abs(e * 1e6 - 4.139) < 0.01, "10 mV of common mode becomes 4.1 µV of differential error."
assert abs(e - 10e-3 * abs(Z1 - Z2) / 10e6) / e < 0.01, "The approximation vcm|Z1 − Z2|/Zin is within 1%."
```

::: right
Right. The mismatch that matters is in impedance at 60 Hz, which the double-layer capacitance makes far smaller than the DC resistances suggest.
:::
:::

::: check
kind: python

## Write `limb_leads(I, II)` returning a dict of all six limb leads, and `axis(I, II)` in degrees.

```python starter
def limb_leads(I, II):
    return {"I": I, "II": II}

def axis(I, II):
    return 0.0
```

```python solution
import math

def limb_leads(I, II):
    return {"I": I, "II": II, "III": II - I, "aVR": -(I + II) / 2, "aVL": I - II / 2, "aVF": II - I / 2}

def axis(I, II):
    return math.degrees(math.atan2((II - I / 2) * 2 / math.sqrt(3), I))
```

```python tests
L = limb_leads(0.5, 1.0)
assert L["III"] == 0.5 and L["aVR"] == -0.75 and L["aVL"] == 0.0 and L["aVF"] == 0.75, "III, aVR, aVL and aVF from I and II."
assert abs(axis(0.5, 1.0) - 60) < 1e-9, "Lead aVL is zero, so the axis is perpendicular to it: 60°."
assert abs(L["I"] + L["III"] - L["II"]) < 1e-12 and abs(L["aVR"] + L["aVL"] + L["aVF"]) < 1e-12, "Einthoven's law, and the augmented leads sum to zero."
```

::: right
Right. Only two limb leads carry independent information; the other four are computed, which is how ECG machines produce them.
:::
:::

::: explain
## Why does a short nerve fiber sealed at its far end hold more of an applied voltage there than an infinitely long fiber would?

::: model
In an infinite fiber, current injected at one point leaks out through the membrane as it spreads, and the voltage falls exponentially with the length constant, because there is always more membrane ahead to leak through. A sealed end stops the axial current: the charge that would have flowed onward has nowhere to go and must leave through the remaining membrane near the end. That raises the voltage near the end, so the profile flattens into a hyperbolic cosine, and at the end itself the voltage is the applied value divided by cosh(L/λ) rather than multiplied by e^{−L/λ}, which is larger by up to a factor of two.
:::
:::
