1

我使用未校准的指南针在现场收集了简单的调查数据。意识到这个问题,在现场,将一个好的罗盘与未校准的罗盘进行了比较,并记录了 11 个方位的差异。该图显示差异非常接近 sin 函数。我希望将多项式(3 次)拟合到这个结果函数中,以使用未校准的罗盘来校正测量数据。我的曲线拟合程序产生的拟合曲线很差。谁能看到有什么问题?

import numpy as np
import scipy
import pylab
correctCompass=\
np.array([134.4,112.6,069.7,051.1,352.5,314.6,218.3,258.2,237.8,186.5,153.7])
errorCompass=\
np.array([131.6,108.9,065.6,047.0,349.8,314.0,284.6,262.7,243.4,189.8,153.2])
# sort compass values
for i in range(0,11):
   for j in range(i+1,11):
       if correctCompass[i] > correctCompass[j]:
          tmp=correctCompass[j]
          correctCompass[j]=correctCompass[i]
          correctCompass[i]=tmp
          tmp=errorCompass[j]
          errorCompass[j]=errorCompass[i]
          errorCompass[i]=tmp

diff = correctCompass - errorCompass + 15.0
height=diff.max() + 16.0
polycoeffs = scipy.polyfit(correctCompass, diff, 3)

# fit the data with a polynomial
yfit = scipy.polyval(polycoeffs,correctCompass)

pylab.plot(correctCompass, diff, 'k.')
pylab.plot(correctCompass, yfit, 'r-')

pylab.axis([0,360,-10.0,height])
pylab.show()
4

1 回答 1

2

polyfit工作正常,问题是diff降低拟合的一个负面因素,但当您将 yaxis 的最低值设置为 -10 时,这并没有显示在您的图中

diff = array([ 19.1,  19.1,  18.7,  17.8,  15.5,  11.7, -51.3,   9.4,  10.5, 15.6,  17.7])

如果您发表评论pylab.axis([0,360,-10.0,height]),您会看到“问题”

此外,您可以使用以下三行替换两个嵌套的 for 循环,从而改进代码并使代码更具可读性:

sort = np.argsort(correctCompass)
correctCompass = correctCompass[sort]
errorCompass = errorCompass[sort]
于 2013-02-03T21:49:24.087 回答