【发布时间】:2017-11-01 17:45:48
【问题描述】:
我想绘制 6 条三次多项式曲线。我有以下代码::
import numpy as np
import matplotlib.pyplot as plt
a0 = np.array([ 0.04438, -0.01808, -0.01836, 0.01170, -0.01062, -0.01062])
a1 = np.array([-2.26095, -0.13595, -0.03577, -0.00400, -0.03577, -0.00400])
a2 = np.array([-0.13387, 0.01941, 0.02612, 0.00066, 0.02612, 0.00066])
a3 = np.array([ 0.00066, -0.00183, -0.00558, -0.00558, 0.00890, 0.00890])
x_max = 2.80
x_min = 0.30
diff = 0.01
y = int(((x_max - x_min)/diff))
m_diff = np.zeros((y,6))
xmin = int(x_min * 100)
xmax = int(x_max * 100)
di = int(diff * 100)
for xx in range(xmin, xmax, di):
x = xx*diff
m_diff[xx] = a0 + (a1*x) + (a2*x*x) + (a3*x*x*x)
为什么会出现“IndexError: index 250 is out of bounds for axis 0 with size 250”错误?这应该是什么?最终我只想做:
plt(xx, m_diff[:,0])
plt(xx, m_diff[:,1])
plt(xx, m_diff[:,2])
plt(xx, m_diff[:,3])
plt(xx, m_diff[:,4])
plt(xx, m_diff[:,5])
谢谢!!
【问题讨论】:
-
m_diff具有形状 (250,6)。您的 for 循环从 30 变为 280。当您的 for 循环到达循环 250 时,您的m_diff不是那么大,这就是您收到错误的原因。
标签: python arrays loops numpy matplotlib