Python 索引错误:索引 1 超出轴 0 的范围,大小为 1/ForwardEuler

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30059227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 07:54:55  来源:igfitidea点击:

IndexError: index 1 is out of bounds for axis 0 with size 1/ForwardEuler

pythonfunctionpython-2.7

提问by Mlle Blanche

I am numerically solving for x(t) for a system of first order differential equations. The system is:

我正在数值求解一阶微分方程组的 x(t)。该系统是:

dy/dt=(C)\*[(-K\*x)+M*A]

dy/dt=(C)\*[(-K\*x)+M*A]

I have implemented the Forward Euler method to solve this problem as follows: Here is my code:

我已经实现了 Forward Euler 方法来解决这个问题,如下所示:这是我的代码:

import matplotlib
import numpy as np
from numpy import *
from numpy import linspace
from matplotlib import pyplot as plt


C=3
K=5
M=2
A=5
#------------------------------------------------------------------------------
def euler (f,x0,t):
    n=len (t)
    x=np.array ([x0*n])
    for i in xrange (n-1):
        x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )
    return x



#---------------------------------------------------------------------------------          
if __name__=="__main__":
    from pylab import *

    def f(x,t): 
        return (C)*[(-K*x)+M*A]

    a,b=(0.0,10.0)
    n=200
    x0=-1.0
    t=linspace (a,b,n)

    #numerical solutions
    x_euler=euler(f,x0,t)

    #compute true solution values in equal spaced and unequally spaced cases
    x=-C*K
    #figure
    plt.plot (t,x_euler, "b")
    xlabel ()
    ylabel ()
    legend ("Euler")

    show()
`
M=2
A=5
#----------------------------------------------------------------------------
def euler (f,x0,t):
    n=len (t)
    x=np.array ([x0*n])
    for i in xrange (n-1):
        x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )
    return x



#---------------------------------------------------------------------------          
if __name__=="__main__":
    from pylab import *

    def f(x,t): 
        return (C)*[(-K*x)+M*A]

    a,b=(0.0,10.0)
    n=200
    x0=-1.0
    t=linspace (a,b,n)

    #numerical solutions
    x_euler=euler(f,x0,t)

    #compute true solution values in equal spaced and unequally spaced cases
    x=-C*K
    #figure
    plt.plot (t,x_euler, "b")
    xlabel ()
    ylabel ()
    legend ("Euler")

    show()

I get following Traceback:

我得到以下回溯:

Traceback (most recent call last):
  File "C:/Python27/testeuler.py", line 50, in <module>
    x_euler=euler(f,x0,t)
  File "C:/Python27/testeuler.py", line 28, in euler
    x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )
IndexError: index 1 is out of bounds for axis 0 with size 1

I don′t understand what is probably wrong.I already looked up after solved questions, but it doesn′t help me along.Can you find my error? I am using following code as an orientation: def euler( f, x0, t ):

我不明白可能有什么问题。我在解决问题后已经查过了,但这对我没有帮助。你能找到我的错误吗?我使用以下代码作为方向:def euler( f, x0, t ):

    n = len( t )
    x = numpy.array( [x0] * n )
    for i in xrange( n - 1 ):
        x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )

    return x
if __name__ == "__main__":
    from pylab import *

    def f( x, t ):
        return x * numpy.sin( t )

    a, b = ( 0.0, 10.0 )
    x0 = -1.0

    n = 51
    t = numpy.linspace( a, b, n )

    x_euler = euler( f, x0, t )

My goal is to plot the function.

我的目标是绘制函数。

采纳答案by Spirine

The problem, as the Traceback says, comes from the line x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] ). Let's replace it in its context:

正如 Traceback 所说,问题来自于行 x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )。让我们在它的上下文中替换它:

  • x is an array equal to [x0 * n], so its length is 1
  • you're iterating from 0 to n-2 (n doesn't matter here), and i is the index. In the beginning, everything is ok (here there's no beginning apparently... :( ), but as soon as i + 1 >= len(x)<=> i >= 0, the element x[i+1]doesn't exist. Here, this element doesn't exist since the beginning of the for loop.
  • x 是一个等于 [x0 * n] 的数组,所以它的长度是 1
  • 您正在从 0 迭代到 n-2(n 在这里无关紧要),而 i 是索引。一开始,一切都很好(这里显然没有开始...... :(),但是只要i + 1 >= len(x)<=> i >= 0,元素x[i+1]就不存在。在这里,这个元素从 for 循环开始就不存在了.

To solve this, you must replace x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )by x.append(x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )).

要解决此问题,您必须替换x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )x.append(x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] ))

回答by leeladam

The problem is with your line

问题出在你的线路上

x=np.array ([x0*n])

Here you define x as a single-item array of -200.0. You could do this:

在这里,您将 x 定义为 -200.0 的单项数组。你可以这样做:

x=np.array ([x0,]*n)

or this:

或这个:

x=np.zeros((n,)) + x0

Note: your imports are quite confused. You import numpy modules three times in the header, and then later import pylab (that already contains all numpy modules). If you want to go easy, with one single

注意:您的导入非常混乱。您在标头中导入 numpy 模块三次,然后导入 pylab(已包含所有 numpy 模块)。如果你想轻松一点,一个人

from pylab import *

line in the top you could use all the modules you need.

您可以使用顶部的所有模块。