1. ホーム
  2. python

[解決済み] IndexError: インデックス 1 は、サイズ 1/ForwardEuler の軸 0 に対して境界外です。

2022-02-01 14:23:13

質問

一階微分方程式系でx(t)を数値的に解いています。その方程式は

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

この問題を解くために、私は次のように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()

以下のような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

私はおそらく間違っているのか理解できません。私はすでに解決された問題の後に調べたが、それは一緒に私を助けることはありません。 私はオリエンテーションとして、次のコードを使用しています。 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 )

私の目標は、関数をプロットすることです。

どのように解決するのですか?

問題は、トレースバックにあるように、次の行にあります。 x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] ) . そのコンテキストで置き換えてみましょう。

  • x は [x0 * n] に等しい配列なので、その長さは 1 です。
  • は0からn-2(nはここでは関係ない)までの繰り返しで、iはインデックスです。最初のうちは何も問題ないのですが(ここではどうやら始まりはないようです... :( ))、やがて i + 1 >= len(x) <=> i >= 0 の場合は、要素 x[i+1] は存在しない。ここでは、forループの先頭からこの要素が存在しない。

これを解決するためには 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] )) .