1. A method for compensating the magnetic field of an MR system, the steps comprising: a) setting compensation parameters for a corrective circuit in the MR system to a set of sample values; b) performing a pulse sequence using the MR system and measuring the error produced in the magnetic field; c) repeating steps a) and b) to measure the error in the magnetic field at different sets of sample values; d) calculating a gradient error function by fitting the measured errors to a polynomial, the gradient error function expressing the error produced in the magnetic field as a function of the compensation parameters; e) calculating an optimal set of compensation parameter values by finding the minimum in the gradient error function; and f) using the optimal compensation parameter values in the corrective circuit of the MR system to acquire NMR data.