Using adaptive step sizes with scipy.integrate.ode

前端 未结 5 1772
忘掉有多难
忘掉有多难 2020-12-08 08:01

The (brief) documentation for scipy.integrate.ode says that two methods (dopri5 and dop853) have stepsize control and dense output. L

5条回答
  •  醉话见心
    2020-12-08 08:14

    Here's another option that should also work with dopri5 and dop853. Basically, the solver will call the logistic() function as often as needed to calculate intermediate values so that's where we store the results:

    import numpy as np
    from scipy.integrate import ode
    import matplotlib.pyplot as plt
    
    sol = []
    def logistic(t, y, r):
        sol.append([t, y])
        return r * y * (1.0 - y)
    
    r = .01
    
    t0 = 0
    y0 = 1e-5
    t1 = 5000.0
    # Maximum number of steps that the integrator is allowed 
    # to do along the whole interval [t0, t1].
    N = 10000
    
    #backend = 'vode'
    backend = 'dopri5'
    #backend = 'dop853'
    solver = ode(logistic).set_integrator(backend, nsteps=N)
    solver.set_initial_value(y0, t0).set_f_params(r)
    
    # Single call to solver.integrate()
    solver.integrate(t1)
    sol = np.array(sol)
    
    plt.plot(sol[:,0], sol[:,1], 'b.-')
    plt.show()
    

提交回复
热议问题