我使用未经校准的指南针在现场收集了简单的调查数据。实现这个问题,在现场,轴承比较了一个好的指南针和未校准的指南针和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()
答案 0 :(得分:2)
polyfit
工作正常,问题是diff
中的一个负面因素降低了适合度,但是当您将yaxis的最低值设置为-10时,未在图中显示p>
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]