A method includes generating a magnetic field in apredefined volume. A reference model is defined, whichmodels the magnetic field at multiple points in thevolume using spherical harmonics. The magnetic field ismeasured by a field detector, which is coupled to anintra-body probe inserted into an organ of a living bodylocated in the volume. A cost function is defined bycomparing the measured magnetic field with the referencemagnetic field model within the volume. The cost functionis minimized by a computation over dipole terms in aderivative over the cost function so as to find aposition and orientation that matches the measuredmagnetic field. The found position and orientation isoutputted as the position and orientation of the probe inthe organ.