  Self-compensated comagnetometers, employing overlapping samples of
spin-polarized alkali and noble gases (for example K-$^3$He) are promising
sensors for exotic beyond-the-standard-model fields and high-precision
metrology such as rotation sensing. When the comagnetometer operates in the
so-called self-compensated regime, the effective field, originating from
contact interactions between the alkali valence electrons and the noble-gas
nuclei, is compensated with an applied magnetic field. When the comagnetometer
begins operation in a given magnetic field, spin-exchange optical pumping
establishes equilibrium between the alkali electron-spin polarization and the
nuclear-spin polarization. Subsequently, when the magnetic field is tuned to
the compensation point, the spin polarization is brought out of the equilibrium
conditions. This causes a practical issue for long measurement times. We report
on a novel method for closed-loop control of the compensation field. This
method allows optimization of the operating parameters, especially magnetic
field gradients, in spite of the inherently slow (hours to days) dynamics of
the system. With the optimization, higher stable nuclear polarization, longer
relaxation times and stronger electron-nuclear coupling are achieved which is
useful for nuclear-spin-based quantum memory, spin amplifiers and gyroscopes.
The optimized sensor demonstrates a sensitivity comparable to the best previous
comagnetometer but with four times lower noble gas density. This paves the way
for applications in both fundamental and applied science.
