Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 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 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...