We present a method for implementing the constraints that are implied by Maxwell's equations in fits to measurements of the magnetic field in the muon storage ring of the g−2 experiment. The method that we use makes use of toroidal-harmonic solutions of Laplace's equation. We point out that the fitting problem can be approximated well as a linear-algebra problem. We have devised an efficient algorithm for the linear-algebra problem that makes it possible to find a solution for 105 data points and 104 harmonics in less than an hour on a present-day desktop computer. We illustrate our method by applying it to some preliminary measurements of the magnetic field in the g−2 storage ring.