以下は、これまでの複数のソルバーのコードです。この問題のシステムはこちら、私たちのシステム ですが、Python で実行すると、次のエラーが表示されます。
トレースバック (最後の最後の呼び出し): ファイル "G:\math3511\assignment\assignment5\qu2"、59 行目、X = AdamsBashforth4(equation, init, t) ファイル "G:\math3511\assignment\assignment5\qu2"、 32 行目、AdamsBashforth4 の k2 = h * f( x[i] + 0.5 * k1, t[i] + 0.5 * h ) TypeError: 'float' 型の非 int でシーケンスを乗算できません
コード:
import numpy
from numpy import array, exp, linspace
def AdamsBashforth4( f, x0, t ):
"""
Fourth-order Adams-Bashforth method::
u[n+1] = u[n] + dt/24.*(55.*f(u[n], t[n]) - 59*f(u[n-1], t[n-1]) +
37*f(u[n-2], t[n-2]) - 9*f(u[n-3], t[n-3]))
for constant time step dt.
RK2 is used as default solver for first steps.
"""
n = len( t )
x = numpy.array( [ x0 ] * n )
# Start up with 4th order Runge-Kutta (single-step method). The extra
# code involving f0, f1, f2, and f3 helps us get ready for the multi-step
# method to follow in order to minimize the number of function evaluations
# needed.
f1 = f2 = f3 = 0
for i in xrange( min( 3, n - 1 ) ):
h = t[i+1] - t[i]
f0 = f( x[i], t[i] )
k1 = h * f0
k2 = h * f( x[i] + 0.5 * k1, t[i] + 0.5 * h )
k3 = h * f( x[i] + 0.5 * k2, t[i] + 0.5 * h )
k4 = h * f( x[i] + k3, t[i+1] )
x[i+1] = x[i] + ( k1 + 2.0 * ( k2 + k3 ) + k4 ) / 6.0
f1, f2, f3 = ( f0, f1, f2 )
for i in xrange( n - 1 ):
h = t[i+1] - t[i]
f0 = f( x[i], t[i] )
k1 = h * f0
k2 = h * f( x[i] + 0.5 * k1, t[i] + 0.5 * h )
k3 = h * f( x[i] + 0.5 * k2, t[i] + 0.5 * h )
k4 = h * f( x[i] + k3, t[i+1] )
x[i+1] = x[i] + h * ( 9.0 * fw + 19.0 * f0 - 5.0 * f1 + f2 ) / 24.0
f1, f2, f3 = ( f0, f1, f2 )
return x
def equation(X, t):
x, y = X
return [ x+y-exp(t), x+y+2*exp(t) ]
init = [ -1.0, -1.0 ]
t = linspace(0, 4, 50)
X = AdamsBashforth4(equation, init, t)