solve ode using multistep solver
Question: The following is the code for multiple solvers so far. The system for this
problem is here, [our
system](http://stackoverflow.com/questions/16747624/setup-odes-in-
python?noredirect=1#comment24123067_16747624) However, when I execute it in
Python, it shows me the following error:

Traceback (most recent call last): File
"G:\math3511\assignment\assignment5\qu2", line 59, in X =
AdamsBashforth4(equation, init, t) File
"G:\math3511\assignment\assignment5\qu2", line 32, in AdamsBashforth4 k2 = h *
f( x[i] + 0.5 * k1, t[i] + 0.5 * h ) TypeError: can't multiply sequence by
non-int of type 'float'

the code:

    
    
    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)
    


Answer: It seems that f is returning a sequence (list, tuple, or like) and there's no
operation to multiply all items in a sequence by a value.

Dirk is right, looking at your algorithm, your equation should return (even if
it small) a numpy array, as you can scalar-multiply a numpy array. So keeping
your code but embedding your return in equation with an array. Like this:

    
    
    np.array([1,2]) # a numpy array containing [1,2]
    

