2

我想计算车辆的速度,在 x 轴上绘制以秒为单位的时间图,在 y 轴上绘制以 km/h 为单位的速度图。为此,我需要获取之前计算的 y 值。

示例:y[x] = y[x-1] * a

a = 0,11768
x = np.arange(0, 100, 1)    # 0 to 100 seconds
y = a * y[x-1] ??
plt.plot(x, y)
plt.show()

用 numpy 可以做到这一点,还是我应该循环遍历所有索引?

4

4 回答 4

1
于 2013-10-25T10:25:09.270 回答
1

Your calculation for y is wrong. Instead of multiplying the previous speed with the acceleration, you have to add the acceleration to that speed. An alternative way would be to multiply the acceleration with the time and add that to some initial speed. This way, you can use a simple list comprehension for y.

a = 0.11768  # acceleration (note the dot instead of comma!)
y0 = 0       # initial speed at time x = 0
X = numpy.arange(0, 100, 1)
Y = numpy.array([y0 + a * x for x in X])

When using Numpy, there's an even simpler way -- thanks to @JoeKington for pointing this out:

Y = y0 + a * X  # multiplies each value of X with a and adds y0
于 2013-10-25T10:25:31.723 回答
0

我不知道在 numpy 中是否可行,但我知道如何使用 pandas 轻松实现它:

import pandas as pd
import numpy as np
a=0.11768

df = pd.DataFrame(np.arange(0, 100, 1),columns=['X'])
df['Y'] = a*df['X'].shift(1)
于 2013-10-25T10:19:14.280 回答
0
a = 0.11768
x = np.arange(0, 100, 1)
y = [1]
for i in range(1, len(x)-1):
  y.append(a * y[i-1])
于 2013-10-25T10:22:18.930 回答