phys_581.assignment_2#

Assignment 2

Classes#

OdeResult

Bunch object for storing results of solve_ivp* methods.

Functions#

solve_ivp_abm(fun, t_span, y0, Nt[, ys, dys, dcp, ...])

Solve the specified IVP using a 5th order predictor-corrector method.

solve_ivp_euler(fun, t_span, y0, Nt)

Solve the specified IVP using Euler's method.

solve_ivp_rk4(fun, t_span, y0, Nt)

Solve the specified IVP using 4th order Runge-Kutta.

step_rk45(fun, t, y, f, h)

Take one step using the RK45 algorithm.

Module Contents#

class OdeResult#

Bases: scipy.optimize.OptimizeResult

Bunch object for storing results of solve_ivp* methods.

solve_ivp_abm(fun, t_span, y0, Nt, ys=None, dys=None, dcp=None, save_memory=False, start_factor=2)#

Solve the specified IVP using a 5th order predictor-corrector method.

This is the algorithm presented at the end of Section 23.10 of Hamming’s book. It is an average of the Milne and Adams-Bashforth cases.

Parameters:
  • Nt (int) – Number of steps. The time-step will be np.diff(t_span)/Nt.

  • ys ([y0, y1, y2, y3] or None) – First four steps to get the process started. If not provided, then these will be computed using solve_ivp_rk4().

  • dys ([dy0, dy1, dy2, dy3] or None) – Derivatives at the corresponding previous steps. Will be computed if not provided.

  • dcp (array, None) – Previous corrector-predictor difference (with a factor 161/170).

  • save_memory (bool) – If True, then only keep the last four steps.

Returns:

  • res (OdeResult) – Bunch object.

  • The remaining arguments should match those of scipy.integrate.solve_ivp().

  • Don’t worry about optimizations like allowing fun to be vectorized etc.

Notes

This method requires four initial values to get started.

solve_ivp_euler(fun, t_span, y0, Nt)#

Solve the specified IVP using Euler’s method.

Parameters:

Nt (int) – Number of steps. The time-step will be (t_span[1] - t_span[0])/Nt.

Returns:

  • res (OdeResult) – Bunch object.

  • The remaining arguments should match those of scipy.integrate.solve_ivp().

  • Don’t worry about optimizations like allowing fun to be vectorized etc.

solve_ivp_rk4(fun, t_span, y0, Nt)#

Solve the specified IVP using 4th order Runge-Kutta.

Parameters:

Nt (int) – Number of steps. The time-step will be (t_span[1] - t_span[0])/Nt.

Returns:

  • res (OdeResult) – Bunch object.

  • The remaining arguments should match those of scipy.integrate.solve_ivp().

  • Don’t worry about optimizations like allowing fun to be vectorized etc.

step_rk45(fun, t, y, f, h)#

Take one step using the RK45 algorithm.

Parameters:
  • fun (callable) – Right-hand side of the system.

  • t (float) – Current time.

  • y (ndarray, shape (n,)) – Current state.

  • f (ndarray, shape (n,)) – Current value of the derivative, i.e., fun(x, y).

  • h (float) – Step to use.

Returns:

  • y_new (ndarray, shape (n,)) – Solution at t + h computed with a higher accuracy.

  • f_new (ndarray, shape (n,)) – Derivative fun(t + h, y_new).

References

E. Hairer, S. P. Norsett G. Wanner, “Solving Ordinary Differential Equations I: Nonstiff Problems”, Sec. II.4.