Rigid-body rotation. With principal moments of inertia I = diag(Ix, Iy, Iz) and body-frame spin w, Euler's equation is I dw/dt = tau - w x (I w). The attitude is a unit quaternion obeying dq/dt = 1/2 q (0, w). Without torque the angular momentum vector H = R(q) I w stays fixed in space and the rotational energy T = 1/2 w.I w is constant; the spin axis wanders inside the body (polhode). Both are checked below.
The Earth's field. The field is modelled as a tilted-orbit centred dipole: B = Bs (R_E/r)^3 [3 (m_hat . r_hat) r_hat - m_hat] with Bs = 31.2 microtesla (typical equatorial surface value) and the dipole pointing to geographic south. This gives |B| = Bs (R_E/r)^3 sqrt(1 + 3 sin^2 lambda), i.e. twice as strong over the poles. The real field is a spherical-harmonic model (IGRF) whose dipole is tilted about 10 degrees from the spin axis and offset; those details change the numbers but not the physics.
B-dot control. A magnetometer measures the field in body axes, B_b. Because the satellite spins, dB_b/dt = R^T dB_i/dt - w x B_b. The coils are commanded to m = -k dB_b/dt (each axis clipped to the maximum dipole) which produces a torque tau = m x B_b. For a spin perpendicular to a steady field this is tau = -k |B|^2 w: pure viscous friction with decay rate k |B|^2 / I - the exponential decay check. If the spin is parallel to B nothing changes in the body frame, so no torque appears: B-dot cannot remove spin about the field direction, which is why an equatorial orbit (the field barely rotates) detumbles very slowly. The magnetometer is treated as ideal, with no noise, no filtering and no sampling delay.
The intermediate-axis flip. A body spinning about its largest or smallest axis is stable; about the intermediate axis it is not. Linearising Euler's equations around spin w about the middle axis (moment Ib, others Ia < Ib < Ic) gives perturbations that grow like exp(sigma t) with sigma = w sqrt((Ic - Ib)(Ib - Ia)/(Ia Ic)), then the body periodically flips over (the "tennis-racket" or Dzhanibekov effect). The preset starts with a tiny perturbation so you can watch the flip happen while the angular momentum H stays fixed.
Estimate in the sweep. Averaging the field over the orbit gives a rough decay time tau = I_mean / (k (2/3) <|B|^2>); the time to fall from the starting spin to 0.5 deg/s is about tau ln(w0 / 0.5 deg/s). It ignores coil saturation and the direction of the field, so treat it as a lower bound (in the runs here the simulated time was about 1.3 to 1.5 times longer for small gains); with a large gain the coils saturate and detumbling gets slower, as the table shows.
What is simplified. No gravity-gradient, drag, solar-pressure torque or residual magnetism; orbit is circular and the Earth does not rotate; wing hinges are rigid; the 3U bus is a uniform box (3.4 kg) and the deployed wings two uniform plates (0.3 kg each), so inertia values are plausible but not those of a real mission. Gains and coil limits are rounded typical values for a 3U magnetorquer set, not a specific product.
Checks on this page. (1) Exponential decay in a steady field against exp(-k B^2 t / I); (2) angular momentum and energy conservation for torque-free tumbling over 2000 s; (3) polhode period of an axisymmetric body against 2 pi / ((Iz - It) w_z / It); (4) growth rate of the intermediate-axis instability against the formula above; (5) dipole-field magnitude at the equator and over the pole.