user7345804
user7345804

Reputation:

What is the error in this sample code via scipy docs that attempts to solve an ode?

I am trying to learn about solving ODEs in python and scipy seemed like a good starting point. I've become comfortable with using odeint and am now trying to learn how to use ode. I tried running the sample code in the scipy docs, but it is returning an error. I've copied the code below alongside the error.

CODE

from scipy.integrate import ode

y0, t0 = [1.0j, 2.0], 0

def f(t, y, arg1):
    return [1j*arg1*y[0] + y[1], -arg1*y[1]**2]

def jac(t, y, arg1):
    return [[1j*arg1, 1], [0, -arg1*2*y[1]]]

r = ode(f, jac).set_integrator('zvode', method='bdf', with_jacobian=True)
r.set_initial_value(y0, t0).set_f_params(2.0).set_jac_params(2.0)
t1 = 10
dt = 1

while r.successful() and r.t < t1:
    r.integrate(r.t+dt)
    print("%g %g" % (r.t, r.y))

ERROR MESSAGE

    print("%g %g" % (r.t, r.y))
TypeError: only length-1 arrays can be converted to Python scalars

Upvotes: 0

Views: 29

Answers (1)

Lutz Lehmann
Lutz Lehmann

Reputation: 26040

r.y is a length 2 array. And that of complex numbers. Both is incompatible with the formatting instruction for one float.

Implicit conversion to string however works,

    print "%g %s" % (r.t, r.y) 

Upvotes: 2

Related Questions