The first thing I would try, if you haven't already, is to do a full DFU Restore of the device. Backup your device with iTunes first (of course). A DFU Restore will download a fresh version of iOS and install it on your device, deleting everything else before that. Than try the compass features before installing a bunch of apps, just in case the problem came from some kind of App or software conflict.
If that doesn't work, then you probably have a logic board issue. Has your phone been dropped or exposed to water recently? Has it been through any repairs or repair attempts? Any logic board issues will have to be looked at by a repair shop that does micro-soldering repairs.