A method includes generating a magnetic field in a predefined volume. A reference model is defined, which models the magnetic field at multiple points in the volume using spherical harmonics. The magnetic field is measured by a field detector, which is coupled to an intra-body probe inserted into an organ of a living body located in the volume. A cost function is defined by comparing the measured magnetic field with the reference magnetic field model within the volume. The cost function is minimized by a computation over dipole terms in a derivative over the cost function so as to find a position and orientation that matches the measured magnetic field. The found position and orientation is outputted as the position and orientation of the probe in the organ.