关于Python中实时数据的数值积分(梯形)问题-
背景:我在实时测量一个平均速度为100米/分钟的移动物体,每100毫秒采样一次,持续60秒,因此在一分钟结束时,我将有600个样本。我的测量方法不精确,每次测量都有10米的误差。一分钟间隔后的典型图表为:
问题:我大致知道物体在一分钟内移动100米,但是当在这段时间内积分(现在只使用梯形)时,我得到的答案是比预期的高60倍,我做错了什么?我怀疑是'width'或deltaT=100ms是不正确的(?)在
我的python代码如下-这是惯用的,也就是说,不是python代码,原因是为了模拟实时测量:
# Trapezoidal rule integral
testData = [] # store all vel. measurements
width = 100e-3 # sample every 100ms
total = 0
currVel = 0
prevVel = 0
t = 0
while( t < 60 ):
# take a live sample and save it
currVel = np.random.normal(100,10,1)
testData.append( currVel )
# only complete the integral after the second sample
if( t >= 100e-3 ):
total += width*(currVel+prevVel)/2
# update previous flow and increment
prevVel = currVel
t += 100e-3
#total /= 60 # fudge factor, why this factor out?
print "Total distance covered over 1 minute", total
积分可以看作是面积计算。你基本上有:
你得到的答案是6000,因为程序没有单位的表示。(答案是每分钟6000米秒。)如果你用这种方式来写计算,错误可能会更明显:
^{pr2}$现在秒数将正确地抵消。有关Python unit libraries的讨论,请参阅此线程。在
相关问题 更多 >
编程相关推荐