For high-resolution spectrographs with large phase-space acceptance the hardware correction of all the relevant aberrations requires so many multipole elements that software correction methods are often more adequate. In this case, the obligatory computation of the higher-order transfer map becomes feasible if the (three-dimensional) magnetic field within the spectrograph can be approximated with sufficient accuracy in an analytical form. For this purpose, we present an approach that allows the use of midplane measurements or alternatively measurements in several planes resulting in a global Maxwellian field that suppresses local measurement inaccuracies. It is based on a modified charge density method generating the magnetic field by a superposition of Gaussian charge distributions. The accuracy of the method is assessed through test cases for which analytical solutions of the field components are known. A maximum relative inaccuracy of the magnetic field in the midplane smaller than ±10 -4 is obtained in the relevant field area. In addition, we obtain a good agreement comparing the multipole content of the analytical field solution with the one of the approximated field.