當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python SciPy integrate.ode用法及代碼示例


本文簡要介紹 python 語言中 scipy.integrate.ode 的用法。

用法:

class  scipy.integrate.ode(f, jac=None)#

數值積分器的通用接口類。

使用(可選)jac = df/dy 求解方程組

注意: 的前兩個參數f(t, y, ...)與所使用的係統定義函數中的參數順序相反scipy.integrate.odeint.

參數

f 可調用f(t, y, *f_args)

微分方程的右側。 t 是標量,y.shape == (n,).f_args通過調用設置set_f_params(*args).f應該返回一個標量、數組或列表(不是元組)。

jac 可調用 jac(t, y, *jac_args) ,可選

右側的雅可比行列式,jac[i,j] = d f[i] / d y[j]jac_args 通過調用 set_jac_params(*args) 設置。

注意

下麵列出了可用的集成商。可以使用 set_integrator 方法選擇它們。

“vode”

Real-valued Variable-coefficient Ordinary Differential Equation solver, with fixed-leading-coefficient implementation. It provides implicit Adams method (for non-stiff problems) and a method based on backward differentiation formulas (BDF) (for stiff problems).

Source: http://www.netlib.org/ode/vode.f

Warning

This integrator is not re-entrant. You cannot have two ode instances using the “vode” integrator at the same time.

This integrator accepts the following parameters in set_integrator method of the ode class:

  • atol : float or sequence absolute tolerance for solution

  • rtol : float or sequence relative tolerance for solution

  • lband : None or int

  • uband : None or int Jacobian band width, jac[i,j] != 0 for i-lband <= j <= i+uband. Setting these requires your jac routine to return the jacobian in packed format, jac_packed[i-j+uband, j] = jac[i,j]. The dimension of the matrix must be (lband+uband+1, len(y)).

  • method: ‘adams’ or ‘bdf’ Which solver to use, Adams (non-stiff) or BDF (stiff)

  • with_jacobian : bool This option is only considered when the user has not supplied a Jacobian function and has not indicated (by setting either band) that the Jacobian is banded. In this case, with_jacobian specifies whether the iteration method of the ODE solver’s correction step is chord iteration with an internally generated full Jacobian or functional iteration with no Jacobian.

  • nsteps : int Maximum number of (internally defined) steps allowed during one call to the solver.

  • first_step : float

  • min_step : float

  • max_step : float Limits for the step sizes used by the integrator.

  • order : int Maximum order used by the integrator, order <= 12 for Adams, <= 5 for BDF.

“zvode”

Complex-valued Variable-coefficient Ordinary Differential Equation solver, with fixed-leading-coefficient implementation. It provides implicit Adams method (for non-stiff problems) and a method based on backward differentiation formulas (BDF) (for stiff problems).

Source: http://www.netlib.org/ode/zvode.f

Warning

This integrator is not re-entrant. You cannot have two ode instances using the “zvode” integrator at the same time.

This integrator accepts the same parameters in set_integrator as the “vode” solver.

Note

When using ZVODE for a stiff system, it should only be used for the case in which the function f is analytic, that is, when each f(i) is an analytic function of each y(j). Analyticity means that the partial derivative df(i)/dy(j) is a unique complex number, and this fact is critical in the way ZVODE solves the dense or banded linear systems that arise in the stiff case. For a complex stiff ODE system in which f is not analytic, ZVODE is likely to have convergence failures, and for this problem one should instead use DVODE on the equivalent real system (in the real and imaginary parts of y).

“lsoda”

Real-valued Variable-coefficient Ordinary Differential Equation solver, with fixed-leading-coefficient implementation. It provides automatic method switching between implicit Adams method (for non-stiff problems) and a method based on backward differentiation formulas (BDF) (for stiff problems).

Source: http://www.netlib.org/odepack

Warning

This integrator is not re-entrant. You cannot have two ode instances using the “lsoda” integrator at the same time.

This integrator accepts the following parameters in set_integrator method of the ode class:

  • atol : float or sequence absolute tolerance for solution

  • rtol : float or sequence relative tolerance for solution

  • lband : None or int

  • uband : None or int Jacobian band width, jac[i,j] != 0 for i-lband <= j <= i+uband. Setting these requires your jac routine to return the jacobian in packed format, jac_packed[i-j+uband, j] = jac[i,j].

  • with_jacobian : bool Not used.

  • nsteps : int Maximum number of (internally defined) steps allowed during one call to the solver.

  • first_step : float

  • min_step : float

  • max_step : float Limits for the step sizes used by the integrator.

  • max_order_ns : int Maximum order used in the nonstiff case (default 12).

  • max_order_s : int Maximum order used in the stiff case (default 5).

  • max_hnil : int Maximum number of messages reporting too small step size (t + h = t) (default 0)

  • ixpr : int Whether to generate extra printing at method switches (default False).

“dopri5”

This is an explicit runge-kutta method of order (4)5 due to Dormand & Prince (with stepsize control and dense output).

Authors:

E. Hairer and G. Wanner Universite de Geneve, Dept. de Mathematiques CH-1211 Geneve 24, Switzerland e-mail: ernst.hairer@math.unige.ch, gerhard.wanner@math.unige.ch

This code is described in [HNW93].

This integrator accepts the following parameters in set_integrator() method of the ode class:

  • atol : float or sequence absolute tolerance for solution

  • rtol : float or sequence relative tolerance for solution

  • nsteps : int Maximum number of (internally defined) steps allowed during one call to the solver.

  • first_step : float

  • max_step : float

  • safety : float Safety factor on new step selection (default 0.9)

  • ifactor : float

  • dfactor : float Maximum factor to increase/decrease step size by in one step

  • beta : float Beta parameter for stabilised step size control.

  • verbosity : int Switch for printing messages (< 0 for no messages).

“dop853”

This is an explicit runge-kutta method of order 8(5,3) due to Dormand & Prince (with stepsize control and dense output).

Options and references the same as “dopri5”.

參考

[HNW93]

E. Hairer、S.P. Norsett 和 G. Wanner,求解常微分方程 i。非剛性問題。第 2 版。 Springer 計算數學係列,Springer-Verlag (1993)

例子

一個要整合的問題和相應的雅可比:

>>> 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')
>>> 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:
...     print(r.t+dt, r.integrate(r.t+dt))
1 [-0.71038232+0.23749653j  0.40000271+0.j        ]
2.0 [0.19098503-0.52359246j 0.22222356+0.j        ]
3.0 [0.47153208+0.52701229j 0.15384681+0.j        ]
4.0 [-0.61905937+0.30726255j  0.11764744+0.j        ]
5.0 [0.02340997-0.61418799j 0.09523835+0.j        ]
6.0 [0.58643071+0.339819j 0.08000018+0.j      ]
7.0 [-0.52070105+0.44525141j  0.06896565+0.j        ]
8.0 [-0.15986733-0.61234476j  0.06060616+0.j        ]
9.0 [0.64850462+0.15048982j 0.05405414+0.j        ]
10.0 [-0.38404699+0.56382299j  0.04878055+0.j        ]

屬性

t 浮點數

當前時間。

y ndarray

當前變量值。

相關用法


注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.integrate.ode。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。