Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

numpy - python code for multiple ode

I want to write a fourth order Adams Bashforth to solve the system. And the following is what I have :

the system is in the following link: system we have

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 )


    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 * ( 55.0 * f0 - 59.0 * k1 + 37.0 * k2 - 9.0 * k3 ) / 24.0

    return x

Am I right?

When I execute it, it gives me the following error message (P.S.: defining the system using the link: link)

>>>X = AdamsBashforth4(equation, init, t)

Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module> X = AdamsBashforth4(equation, init, t)
File "<pyshell#20>", line 15, in AdamsBashforth4 f0 = f( x[:-1], t[:-1] )
File "<pyshell#11>", line 2, in equation x, y = X ValueError: too many values to unpack

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...