Many ship accidents have arisen from an error in course indication. Bearing in mind that the actual errors in gyrocompass and satellite compass are really minor, they may be considered valid to be input into an autopilot provided that any failure in such devices is controlled by means of a secondary heading source such as a magnetic compass. However, magnetic compass deviation may be significant and its heading should be corrected before being input to the autopilot. The errors caused by the geographic variability of the deviation should also be taken into account. Moreover, the current way to reduce the deviation requires that the ship is un-berthed to execute a complete swing. The aim of this article is to obtain a ship magnetic model by means of an algorithm based on least squares to correct magnetic compass heading input in the autopilot and to permit definitive magnetic compass compensation without swinging the ship through 360°.