Using adaptive step sizes with scipy.integrate.ode

前端 未结 5 1767
忘掉有多难
忘掉有多难 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:03

    The integrate method accepts a boolean argument step that tells the method to return a single internal step. However, it appears that the 'dopri5' and 'dop853' solvers do not support it.

    The following code shows how you can get the internal steps taken by the solver when the 'vode' solver is used:

    import numpy as np
    from scipy.integrate import ode
    import matplotlib.pyplot as plt
    
    
    def logistic(t, y, r):
        return r * y * (1.0 - y)
    
    r = .01
    
    t0 = 0
    y0 = 1e-5
    t1 = 5000.0
    
    backend = 'vode'
    #backend = 'dopri5'
    #backend = 'dop853'
    solver = ode(logistic).set_integrator(backend)
    solver.set_initial_value(y0, t0).set_f_params(r)
    
    sol = []
    while solver.successful() and solver.t < t1:
        solver.integrate(t1, step=True)
        sol.append([solver.t, solver.y])
    
    sol = np.array(sol)
    
    plt.plot(sol[:,0], sol[:,1], 'b.-')
    plt.show()
    

    Result: Plot of the solution

提交回复
热议问题