---
title: Magnetism and superconductivity lab
heading: Mean-field magnetization, Curie–Weiss fits, Stoner–Wohlfarth switching, field penetration and the Josephson junction's I–V curve, in Python
eyebrow: B12 · Lesson 41 · Magnetism and superconductivity lab
xp: 50
---

::: hook
Lessons 37 to 40 described magnets and superconductors with a handful of models: the mean-field magnet, the Curie–Weiss law, a single-domain grain flipping in a field, the London penetration depth and the Josephson junction. Each is a short calculation that has no closed form in general, or one worth checking numerically: a self-consistent equation, a fit, an energy landscape whose minimum disappears, a differential equation for a phase. In this lab you'll write each one and compare it with the limiting formulas the lessons used.
:::

::: play
- The curve is the average voltage across an overdamped Josephson junction against its bias current, both normalized: $\frac{V}{I_cR}$ against $\frac{I}{I_c}$.
- Below the critical current the junction carries a supercurrent with no voltage. Above it the phase slips continuously and the average voltage follows $\sqrt{i^2 - 1}$, approaching the ohmic line at high current.
- The readouts give the voltage and the Josephson frequency for a real junction with the critical current and resistance set by the sliders. Step 5 integrates the phase to get this curve.
:::

```sim plot
view: graph
x: i = 0..3
y: v = 0..3
slider: c = 10..500 = 100 µA log
slider: r = 1..50 step 0.5 = 10 Ω
slider: b = 0..3 step 0.01 = 1.5
plot: sqrt(abs(i^2 - 1))*step(i - 1) | label: average voltage, V/(IcR) | samples: 400
plot: i | dashed | color: ch2 | label: ohmic, V = IR | samples: 50
marker: i = b | label: your bias
readout: voltage, mV = sqrt(abs(b^2 - 1))*step(b - 1)*c*1e-6*r*1000 | digits: 4
readout: Josephson frequency, GHz = 483.5978*sqrt(abs(b^2 - 1))*step(b - 1)*c*1e-6*r*1000 | digits: 4
```

::: predict
## A single-domain magnetic grain's easy axis points along x, and it's magnetized along +x. A field is applied pointing back toward −x. Which direction of field flips it with the least field?

- [ ] Exactly along −x, straight against the magnetization
- [x] At 45° to the easy axis
- [ ] Perpendicular to the easy axis
- [ ] It doesn't matter

::: reveal
At 45°. A field straight along the axis exerts no torque until the magnetization is already disturbed, so it must reach the full anisotropy field $H_K$. A field at an angle both pushes and twists, and at 45° the switching field is only $\frac12H_K$. The Stoner–Wohlfarth astroid, which step 3 traces numerically, captures this for every angle.
:::
:::

::: nameit
## Mean-field magnetization

In mean-field theory each moment feels the applied field plus an exchange field proportional to the magnetization. For spin ½ with no applied field, the reduced magnetization m satisfies $m = \tanh\frac{m}{t}$ with $t = T/T_c$. Below $T_c$ there's a nonzero solution, found by iterating; near $T_c$ it goes as $m \approx \sqrt{3(1 - t)}$. Above $T_c$ the susceptibility follows the Curie–Weiss law, $\chi = \frac{C}{T - \theta}$, and a straight-line fit of 1/χ against T gives C and θ.

## Stoner–Wohlfarth switching

A single-domain grain with uniaxial anisotropy has energy, in units of its anisotropy energy, $e(\theta) = \frac12\sin^2\theta - h\cos(\theta - \psi)$ for magnetization angle θ and field h (in units of $H_K$) at angle ψ. Switching happens when the minimum holding the old direction disappears. The critical field traces the **astroid**,

$$h_{sw}(\psi) = \left(\cos^{2/3}\psi + \sin^{2/3}\psi\right)^{-3/2},$$

1 along the axis and ½ at 45°.

## Field in a superconducting slab

A slab of thickness d in a parallel field $B_0$ expels it except within λ of each surface: $B(x) = B_0\frac{\cosh(x/\lambda)}{\cosh(d/2\lambda)}$, with x measured from the middle. Its average is $\frac{2\lambda}{d}\tanh\frac{d}{2\lambda}$ of $B_0$, which tends to $\frac{2\lambda}{d}$ for thick slabs and to 1 for thin ones.

## The overdamped junction

A Josephson junction shunted by a resistance R, with negligible capacitance, obeys $\frac{\hbar}{2eR}\frac{d\varphi}{dt} = I - I_c\sin\varphi$. Above $I_c$ the phase slips continuously, and averaging $V = \frac{\hbar}{2e}\frac{d\varphi}{dt}$ over a slip gives $\bar V = R\sqrt{I^2 - I_c^2}$, with the supercurrent oscillating at the Josephson frequency $f = \frac{2e\bar V}{h}$ = 483.6 GHz per mV.
:::

::: example
## For a spin-½ mean-field magnet, what's the magnetization at half its Curie temperature and at 0.9 of it? How good is the near-$T_c$ formula?

::: solution
1. **Iterate the mean-field equation.** Iterating $m \leftarrow \tanh(m/t)$ from m = 1 converges to 0.958 at t = 0.5 and to 0.525 at t = 0.9.
2. **The formula near the Curie temperature.** $\sqrt{3(1 - t)}$ gives 0.548 at 0.9, 4% high, and is useless at 0.5 (it gives 1.22).
3. **Closer still.** At t = 0.99 the two agree to 0.4%: 0.1725 against 0.1732.
4. **The shape of the curve.** The magnetization stays close to saturation well below $T_c$ and then collapses over the last 20% of the way.

**Answer:** the magnetization is 0.958 at half the Curie temperature and 0.525 at 0.9 of it; the near-$T_c$ formula is 4% high at 0.9 and useless at 0.5.
:::
:::

::: example
## A recording grain has anisotropy field $H_K$ = 1 MA/m. What field flips it along its easy axis, and at 45°?

::: solution
1. **Along the axis.** Here $h_{sw}$ = 1, so $\mu_0H$ = 1.26 T.
2. **At 45°.**
   $$\begin{aligned} h_{sw} &= (2 \times 0.5^{1/3})^{-3/2} \\ &= \frac12 \end{aligned}$$
   so 0.63 T.
3. **At 30° and 60°.** It's 0.524.
4. **The writability limit.** Write heads can't produce much more than about 2 T, so ever-smaller grains, which need higher $H_K$ for stability, run into a writability limit.
5. **Ways around it.** Tilting the head's field toward 45° gains a factor of two, and heat-assisted recording lowers $H_K$ briefly by heating each bit.

**Answer:** the grain flips at 1.26 T along its easy axis and at 0.63 T at 45°.
:::
:::

::: example
## A Josephson junction has $I_c$ = 100 µA and is shunted by 10 Ω. Biased at 150 µA, what average voltage does it show, and at what frequency does it oscillate?

::: solution
1. **Average voltage.**
   $$\begin{aligned} \bar V &= R\sqrt{I^2 - I_c^2} \\ &= 10\ \Omega \times \sqrt{150^2 - 100^2}\ \mu\text{A} \\ &= 1.12\ \text{mV} \end{aligned}$$
2. **Oscillation frequency.**
   $$\begin{aligned} f &= 483.6\ \text{GHz/mV} \times 1.118\ \text{mV} \\ &= 541\ \text{GHz} \end{aligned}$$
3. **What the voltage looks like.** It isn't steady: it's a train of phase-slip pulses whose average is 1.12 mV.
4. **Uses.** Junctions like this generate and detect terahertz radiation, and arrays of them, driven by microwaves, set the volt.

**Answer:** the junction shows an average of 1.12 mV and oscillates at 541 GHz.
:::
:::

::: check
kind: python

## Step 1. Write `mean_field(t)` returning the reduced magnetization of a spin-½ mean-field magnet at $t = T/T_c$, by iterating $m \leftarrow \tanh(m/t)$ from m = 1 until it converges (return 0 for t ≥ 1).

```python starter
import math

def mean_field(t):
    return math.sqrt(max(0.0, 3 * (1 - t)))

print(mean_field(0.5), mean_field(0.9))
```

```python solution
import math

def mean_field(t):
    if t >= 1:
        return 0.0
    m = 1.0
    for _ in range(200000):
        new = math.tanh(m / t)
        if abs(new - m) < 1e-14:
            break
        m = new
    return m

print(mean_field(0.5), mean_field(0.9))
```

```python tests
import math
assert abs(mean_field(0.5) - 0.95750) < 1e-4, f"At t = 0.5, m = 0.9575; got {mean_field(0.5)}"
assert abs(mean_field(0.9) - 0.52543) < 1e-4, "At t = 0.9, m = 0.5254."
assert abs(mean_field(0.99) / math.sqrt(0.03) - 1) < 0.01, "Near Tc it approaches √(3(1 − t))."
assert mean_field(1.2) == 0.0, "Above Tc there's no spontaneous magnetization."
```

::: right
Right. Iteration converges because tanh's slope at the solution is below 1; near $T_c$ it slows, as the slope approaches 1.
:::
:::

::: check
kind: python

## Step 2. Write `curie_weiss_fit(T, chi)` returning `(C, theta)` from a least-squares straight-line fit of 1/χ against T, using `np.polyfit`.

```python starter
import numpy as np

def curie_weiss_fit(T, chi):
    return 1.0, 0.0

T = np.linspace(400, 800, 9)
print(curie_weiss_fit(T, 2.0 / (T - 350)))
```

```python solution
import numpy as np

def curie_weiss_fit(T, chi):
    slope, intercept = np.polyfit(np.asarray(T), 1 / np.asarray(chi), 1)
    C = 1 / slope
    return float(C), float(-intercept * C)

T = np.linspace(400, 800, 9)
print(curie_weiss_fit(T, 2.0 / (T - 350)))
```

```python tests
import numpy as np
T = np.linspace(400, 800, 9)
C, th = curie_weiss_fit(T, 2.0 / (T - 350))
assert abs(C - 2) < 1e-9 and abs(th - 350) < 1e-6, f"Exact data must return C = 2, θ = 350; got {C}, {th}"
rng = np.random.default_rng(4)
noisy = 0.5 / (T + 20) * (1 + 0.002 * rng.standard_normal(T.size))
C2, th2 = curie_weiss_fit(T, noisy)
assert abs(C2 - 0.5) < 0.01 and abs(th2 + 20) < 5, "An antiferromagnet gives a negative θ."
```

::: right
Right. The sign of θ tells ferromagnetic (positive) from antiferromagnetic (negative) interactions.
:::
:::

::: check
kind: python

## Step 3. Write `switching_field(psi)` returning the Stoner–Wohlfarth switching field (units of $H_K$) for a field at angle `psi` (radians, 0 to π/2) from the easy axis, opposing a grain magnetized along +x. Count the local minima of $e(\theta) = \frac12\sin^2\theta - h\cos(\theta - \pi - \psi)$ on a fine θ grid, and find by bisection the smallest h at which the one near θ = 0 is gone.

```python starter
def switching_field(psi):
    return 1.0

print(switching_field(0.7853981633974483))
```

```python solution
import numpy as np

def has_old_minimum(h, psi):
    th = np.linspace(-np.pi / 2, np.pi / 2, 20001)
    e = 0.5 * np.sin(th) ** 2 - h * np.cos(th - np.pi - psi)
    interior = (e[1:-1] < e[:-2]) & (e[1:-1] < e[2:])
    return bool(np.any(interior))

def switching_field(psi):
    lo, hi = 0.0, 1.5
    for _ in range(50):
        mid = 0.5 * (lo + hi)
        if has_old_minimum(mid, psi):
            lo = mid
        else:
            hi = mid
    return 0.5 * (lo + hi)

print(switching_field(0.7853981633974483))
```

```python tests
import math
astroid = lambda p: (math.cos(p) ** (2 / 3) + math.sin(p) ** (2 / 3)) ** -1.5
assert abs(switching_field(math.pi / 4) - 0.5) < 3e-3, f"At 45° the switching field is ½; got {switching_field(math.pi / 4)}"
assert abs(switching_field(math.pi / 6) - astroid(math.pi / 6)) < 3e-3, "At 30° it follows the astroid, 0.524."
assert abs(switching_field(0.05) - astroid(0.05)) < 5e-3, "Nearly along the axis it approaches 1."
```

::: right
Right. The astroid emerges from nothing more than watching a local minimum vanish.
:::
:::

::: check
kind: python

## Step 4. Write `slab_average(d, lam)` returning the average of $B/B_0$ across a superconducting slab of thickness d in a parallel field, by numerically averaging $\frac{\cosh(x/\lambda)}{\cosh(d/2\lambda)}$ from −d/2 to d/2.

```python starter
def slab_average(d, lam):
    return 1.0

print(slab_average(2.0, 1.0))
```

```python solution
import numpy as np

def slab_average(d, lam):
    x = np.linspace(-d / 2, d / 2, 20001)
    B = np.cosh(x / lam) / np.cosh(d / (2 * lam))
    trap = np.trapezoid if hasattr(np, "trapezoid") else np.trapz
    return float(trap(B, x) / d)

print(slab_average(2.0, 1.0))
```

```python tests
import math
assert abs(slab_average(2.0, 1.0) - math.tanh(1)) < 1e-6, f"For d = 2λ the average is tanh 1 = 0.7616; got {slab_average(2.0, 1.0)}"
assert abs(slab_average(100.0, 1.0) - 0.02) < 1e-4, "A thick slab admits about 2λ/d."
assert abs(slab_average(0.01, 1.0) - 1) < 1e-4, "A very thin slab barely screens at all."
```

::: right
Right. Thin superconducting films let field through, which is why their critical fields in parallel are so high.
:::
:::

::: check
kind: python

## Step 5. Write `rcsj_voltage(i)` returning the average of $\frac{d\varphi}{d\tau}$ for $\frac{d\varphi}{d\tau} = i - \sin\varphi$, by RK4 from φ = 0 over a long time (step 0.01, time 2000), after discarding the first quarter. This is $\bar V/(I_cR)$.

```python starter
def rcsj_voltage(i):
    return i

print(rcsj_voltage(1.5))
```

```python solution
import math

def rcsj_voltage(i):
    f = lambda p: i - math.sin(p)
    dt, n = 0.01, 200000
    p = 0.0
    start = None
    for k in range(n):
        if k == n // 4:
            start = p
        k1 = f(p)
        k2 = f(p + 0.5 * dt * k1)
        k3 = f(p + 0.5 * dt * k2)
        k4 = f(p + dt * k3)
        p += dt / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
    return (p - start) / (dt * (n - n // 4))

print(rcsj_voltage(1.5))
```

```python tests
import math
assert abs(rcsj_voltage(1.5) - math.sqrt(1.25)) < 0.01, f"At 1.5 Ic the average is √1.25 = 1.118; got {rcsj_voltage(1.5)}"
assert abs(rcsj_voltage(0.8)) < 1e-3, "Below Ic the phase locks and there's no average voltage."
assert abs(rcsj_voltage(3.0) - math.sqrt(8)) < 0.01, "At 3 Ic it's √8 = 2.83."
```

::: right
Right. The phase spends most of its time crawling past φ = π/2, where $i - \sin\varphi$ is smallest, which is why the average falls below the ohmic value.
:::
:::

::: check
kind: parts
given: t = 0.9 | 0.95 | 0.99; C = 1 | 3; T = 600 | 900

## A spin-½ mean-field ferromagnet is at t = {t} of its Curie temperature $T_c$ = {T} K, with Curie constant {C} K.

::: part
answer: sqrt(3*(1 - t))
tolerance: 0.5%

### What does the near-$T_c$ formula give for its reduced magnetization?

::: misses
- [sqrt(1 - t)] The coefficient is 3: $m \approx \sqrt{3(1 - t)}$.
- [any] $\sqrt{3(1 - t)}$.
:::
:::

::: part
answer: C/(T*(1 + (1 - t)) - T)
tolerance: 0.5%

### Above $T_c$, at a temperature as far above as t is below, what does Curie–Weiss give for χ (with θ = $T_c$)?

::: misses
- [any] $\chi = \frac{C}{T - \theta}$ at $T = T_c(2 - t)$.
:::
:::
:::

::: check
given: p = 10 | 30 | 45 | 70
answer: (cos(p*pi/180)^(2/3) + sin(p*pi/180)^(2/3))^(-1.5)
tolerance: 0.5%

## A Stoner–Wohlfarth grain sees a reversing field at {p}° to its easy axis. What switching field, in units of $H_K$, does the astroid give?

::: right
Right. $h_{sw} = (\cos^{2/3}\psi + \sin^{2/3}\psi)^{-3/2}$, lowest (½) at 45°.
:::

::: misses
- [1/cos(p*pi/180)] Only the field's component along the axis doesn't capture the twisting torque.
- [any] The astroid formula.
:::
:::

::: check
given: d = 0.1 | 0.2 | 0.4; l = 0.1 | 0.2
answer: 2*l/d*tanh(d/(2*l))
tolerance: 0.5%

## A superconducting slab {d} µm thick has penetration depth {l} µm. What fraction of a parallel applied field does it hold on average?

::: right
Right. $\frac{2\lambda}{d}\tanh\frac{d}{2\lambda}$.
:::

::: misses
- [2*l/d] That's the thick-slab limit; keep the tanh.
- [any] $\frac{2\lambda}{d}\tanh\frac{d}{2\lambda}$.
:::
:::

::: check
given: c = 50 | 100 | 300; r = 5 | 20; b = 1.2 | 2
answer: r*sqrt((b*c)^2 - c^2)*1e-6*1000
unit: mV
tolerance: 0.5%

## An overdamped junction with $I_c$ = {c} µA and R = {r} Ω is biased at {b} $I_c$. What average voltage does it show?

::: right
Right. $\bar V = R\sqrt{I^2 - I_c^2}$.
:::

::: misses
- [r*b*c*1e-6*1000] That's the ohmic value; phase slips spend time near the critical current.
- [any] $R\sqrt{I^2 - I_c^2}$.
:::
:::

::: explain
## Why is the average voltage across an overdamped Josephson junction less than IR, even well above its critical current?

::: model
Above the critical current, the junction's phase can't stay put, so it advances continuously, and the voltage is proportional to how fast it advances. But the supercurrent, $I_c\sin\varphi$, still flows and varies as the phase turns. During the part of each cycle when the supercurrent carries a large share of the bias, little current is left for the resistor, so the phase advances slowly; when the supercurrent reverses, the phase races. The phase lingers where it's slow, so on average the resistor carries less than the full bias current, and the average voltage is $R\sqrt{I^2 - I_c^2}$. Far above $I_c$ the supercurrent's swing averages out and the junction becomes ohmic.
:::
:::

::: practice

::: check
given: p = 10 | 20 | 30 | 45 | 60 | 75
answer: (cos(p*pi/180)^(2/3) + sin(p*pi/180)^(2/3))^(-1.5)
tolerance: 0.5%

## Astroid switching field at {p}°?

::: misses
- [any] $(\cos^{2/3}\psi + \sin^{2/3}\psi)^{-3/2}$.
:::
:::

::: check
given: c = 20 | 200; r = 2 | 30; b = 1.5 | 2.5
answer: r*sqrt((b*c)^2 - c^2)*1e-6*1000
unit: mV
tolerance: 0.5%

## RCSJ voltage: $I_c$ = {c} µA, R = {r} Ω, bias {b} $I_c$?

::: misses
- [any] $R\sqrt{I^2 - I_c^2}$.
:::
:::
:::
