Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 索引越界错误:动画/模拟_Python 2.7_Numpy_Matplotlib_Indexoutofboundsexception - Fatal编程技术网

Python 2.7 索引越界错误:动画/模拟

Python 2.7 索引越界错误:动画/模拟,python-2.7,numpy,matplotlib,indexoutofboundsexception,Python 2.7,Numpy,Matplotlib,Indexoutofboundsexception,我不明白为什么我得到这个索引越界错误。我已经对这些索引的值进行了测试打印,并且打印正确。有人能解释一下我的阵列在哪里被重组吗 class Particle: def __init__(self,fourvector = [1.0,1.0,1.0,-1.0], origin=(0,0)): self.mass = 2.5 # Mass in kg self.fourvector = np.asarray(fou

我不明白为什么我得到这个索引越界错误。我已经对这些索引的值进行了测试打印,并且打印正确。有人能解释一下我的阵列在哪里被重组吗

class Particle:

    def __init__(self,fourvector = [1.0,1.0,1.0,-1.0],
                 origin=(0,0)):
        self.mass = 2.5       # Mass in kg
        self.fourvector = np.asarray(fourvector,dtype='float')
        self.Vx_init = self.fourvector[2]
        self.x_init = self.fourvector[0]
        self.y_init = self.fourvector[1]
        self.Vy_init = self.fourvector[3]
        self.time_passed = 0
        self.origin = origin
        print fourvector[0]
        print fourvector[2]

    def position(self):
        x0 = self.origin[0]
        x1 = self.fourvector[0]
        Vx = self.fourvector[2]
        y0 = self.origin[1]
        y1 = self.fourvector[1]
        Vy = self.fourvector[3]
        x = x0 + x1 * Vx
        y = x0 + y1 * Vy

        return (x,y)

    def derivs(self,fourvector):
        '''derivative computation setup'''
        x_pos = fourvector[0]
        y_pos = fourvector[1]
        dydx = np.zeros_like(fourvector)
        dydx[0] = fourvector[2] #x-comp of velocity
        dydx[1] = (-x_pos)/((x_pos)**2 + (y_pos)**2)**1.5
        dydx[2] = fourvector[3] #y-comp of velocity
        dydx[3] = (-y_pos)/((x_pos)**2 + (y_pos)**2)**1.5
        return dydx

    def time_step(self,dt):
        '''Time progression and state fourvector update'''
        self.fourvector = integrate.odeint(self.derivs,0,dt)
        self.time_passed += dt


body = Particle([1.0,1.0,1.0,2.0])    #Object of Particle created.
dt = 1./30

fig = plt.figure()
ax = fig.add_subplot(111,aspect='equal',autoscale_on=False,xlim=(-3,3),ylim=(-3,3))
ax.grid()

line, = ax.plot([],[],'o-',lw=2)
time_text = ax.text(0.02,0.95,'',transform=ax.transAxes)

def init():
    line.set_data([],[])
    time_text.set_text('')
    return line, time_text

def animate(i):
    global body, dt
    body.time_step(dt)
    line.set_data(*body.position())
    time_text.set_text('time = %.1f' %body.time_passed)
    return line, time_text

from time import time
t0 = time()
animate(0)
t1 = time()
interval = 1000*dt - (t1 - t0)

ani = animation.FuncAnimation(fig, animate, frames = 300,
                              interval = interval, blit=True, init_func=init)
plt.show()
错误回溯:

bash-3.2$ python MoreCrap.py 
1.0
1.0
Traceback (most recent call last):
  File "MoreCrap.py", line 80, in <module>
    animate(0)
  File "MoreCrap.py", line 74, in animate
    line.set_data(*body.position())
  File "MoreCrap.py", line 26, in position
    Vx = self.fourvector[2]
IndexError: index out of bounds
bash-3.2$python MoreCrap.py
1
1
回溯(最近一次呼叫最后一次):
文件“MoreCrap.py”,第80行,在
设置动画(0)
文件“MoreCrap.py”,第74行,在动画中
line.set_数据(*body.position())
文件“MoreCrap.py”,第26行,就位
Vx=自身四向量[2]
索引器:索引超出范围

您对
integrate.odeint
的调用是错误的。如果它真的符合你的想法,请查阅手册

在任何情况下,
fourvector
在调用它之前有值
[1.1.1.2.]
,在调用它之后有值
[[0.]]
,因此它不包含任何索引为
[2]
的值


odeint的主页是。

感谢您指出
integrate.odeint
问题。但我仍在试图将我的头绕在从
[12]
[[0]]
fourvector上。投票结束吗?为什么?我看不到我的索引在哪里被重新排列,对我来说,这似乎是一个简单的编程问题。。。