How to do it...

  1. As with integration, SciPy has some extremely accurate general-purpose solvers for systems of ordinary differential equations of the first order:
  1. For real-valued functions, we have basically two flavors: ode (with options passed with the set_integrator method) and odeint (simpler interface). The syntax of ode is as follows:
ode(f,jac=None)
  1. The first parameter, f, is the function to be integrated, and the second parameter, jac, refers to the matrix of partial derivatives with respect to the dependent variables (the Jacobian). This creates an ode object with different methods to indicate the algorithm to solve the system (set_integrator), the initial conditions (set_initial_value), and different parameters to be sent to the function or its Jacobian.
  2. The options for the integration algorithm are vode for the real-valued variable coefficient ODE solver, with fixed-leading-coefficient implementation (it provides Adam's method for non-stiff problems and BDF for stiff); zvode for the complex-valued variable coefficient ODE solver, with similar options to the preceding option; dopri5 for a Runge-Kutta method of order (4)5; dop853 for a Runge-Kutta method of order 8(5, 3).
  3. The next code snippet presents an example of using scipy.integrate.ode to solve the initial value problem using the following formula:
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))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset