在Python中集成值列表 [英] Integrating a list of values in Python
问题描述
我有一个数据列表,它代表加速度.为简单起见,假设此列表为:
I have a list of data, which represents acceleration. For simplicity, let's say this list is:
X = [1, 2, 3, 4, 5]
我想对该列表中的每个值进行两次积分(两次积分),以便我可以检索有关位置的信息.
I want to integrate each value in this list twice (double integration) so I can retrieve information about location.
快速提醒:位置,速度和加速度是相关的.您可以通过两次导数来找到加速度.或者,如果从加速度开始,则可以两次积分.
Quick reminder: position, velocity, and acceleration are related. You can find acceleration by taking the derivative twice. Or, you can take the integral twice if you start with acceleration.
我无法真正更改正在使用的数据.我将浮点值存储在来自加速度计的列表中.我还了解,通常,集成需要定义一个函数,通常需要一个可以在一定间隔内集成的变量.
I cannot really change the data I am working with. I have floating point values stored in a list coming from my accelerometer. I also understand that usually, integration requires a function to be defined, typically with a variable that can be integrated over an interval.
我尝试使用该库:
import scipy.integrate as integrate
并尝试使用.quad
方法,但出现错误.
and have attempted using the .quad
method, but I am yielding errors.
我很乐意提供任何可以帮助您的提示,想法或资源链接.
I would love any tips, ideas, or links to resources that could help.
推荐答案
使用.cumtrapz
方法. cumtrapz(X)
通过梯形法以单位间距计算X的近似累积积分.
Use .cumtrapz
method. cumtrapz(X)
computes the approximate cumulative integral of X via the trapezoidal method with unit spacing.
import scipy.integrate as it
X = [1, 2, 3, 4, 5]
velocity = it.cumtrapz(X,initial=0)
location = it.cumtrapz(velocity,initial=0)
print 'velocity: ', velocity
print 'location: ', location
输出:
velocity: [ 0. 1.5 4. 7.5 12. ]
location: [ 0. 0.75 3.5 9.25 19. ]
请注意,如果您有与加速度矢量相对应的时间矢量t
,则需要使用cumtrapz(X,t)
. 此处是方法
Note if your have the time vector t
corresponding to acceleration vector, you need to use cumtrapz(X,t)
. Here is the reference for method cumtrapz
.
这篇关于在Python中集成值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!