python code for multiple ode

泄露秘密 提交于 2019-12-18 09:14:58

问题


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

来源:https://stackoverflow.com/questions/16748433/python-code-for-multiple-ode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!