๐ Course Materials
ยฉ๏ธ License CC BY 4.0
๐ | Code | ๐ | Worked Example |
๐ | Graph | ๐งฉ | Exercise |
๐ท๏ธ | Definition | ๐ป | Numerical Method |
๐ | Theorem | ๐งฎ | Analytical Method |
๐ | Remark | ๐ง | Theory |
โน๏ธ | Information | ๐๏ธ | Hint |
โ ๏ธ | Warning | ๐ | Solution |
Numerical approximation solution \(x(t)\) to the IVP
\[ \dot{x} = f(x), \; x(t_0) = x_0 \]
on some finite time span \([t_0, t_f]\).
Pick a (small) fixed time step \(\Delta t > 0\).
Then use repeatedly the approximation:
\[ \begin{split} x(t + \Delta t) & \simeq x(t) + \Delta t \times \dot{x}(t) \\ & = x(t) + \Delta t \times f(x(t)) \\ x(t + 2\Delta t) & \simeq x(t+\Delta t) + \Delta t \times \dot{x}(t+ \Delta t) \\ & = x(t+\Delta t) + \Delta t \times f(x(t+\Delta t)) \\ x(t+3\Delta t) & \simeq \cdots \end{split} \]
to compute a sequence of states \(x_k \simeq x(t+k \Delta t)\).
f
, vector field (\(n\)-dim \(\to\) \(n\)-dim),
t_span
, time span (t0, t1)
,
y0
, initial state (\(n\)-dim),
dt
, time step.
t
, 1-dim array
t = [t0, t0 + dt, ...]
.
x
, 2-dim array, shape (n, len(t))
x[i][k]
: value of x_i(t_k)
.
\[ \left| \begin{split} \dot{x}_1 &= -x_2 \\ \dot{x}_2 &= +x_1 \end{split} \right. \;\; \mbox{ with } \;\; \left| \begin{array}{l} x_1(0) = 1\\ x_2(0) = 0 \end{array} \right. \]
Now that you understand the basics
โ ๏ธ Do NOT use this basic solver (anymore)!
โ ๏ธ Do NOT roll your own ODE solver !
Instead
(Solvers are surprisingly hard to get right.)
Use (for example):
๐ Documentation: solve_ivp
Features: time-dependent vector field, error control, dense outputs, multiple integration schemes, etc.
Compute the solution \(x(t)\) for \(t\in[0,2\pi]\) of the IVP:
\[ \left| \begin{split} \dot{x}_1 &= -x_2 \\ \dot{x}_2 &= +x_1 \end{split} \right. \; \mbox{ with } \; \left| \begin{array}{l} x_1(0) = 1\\ x_2(0) = 0 \end{array} \right. \]
The solver is designed for time-dependent systems:
\[ \dot{x} = f(t, x) \]
The t
argument in the definition of fun
is
mandatory, even if the returned value doesnโt depend on it (when the
system is effectively time-invariant).
The result
is a dictionary-like object with
attributes:
t
: array, time points, shape
(n_points,)
,
y
: array, values of the solution at t, shape
(n, n_points)
,
โฆ
(See ๐ solve_ivp
documentation)
The step size is:
variable: \(t_{n+1} - t_n\) may not be constant,
automatically selected by the solver,
The solver shall meet the user specification, but should select the largest step size to do so to minimize the number of computations.
Optionally, you can specify a max_step
(default: \(+\infty\)).
We generally want to control the (local) error \(e(t)\): the difference between the numerical solution and the exact one.
atol
is the absolute tolerance
(default: \(10^{-6}\)),
rtol
is the relative tolerance
(default: \(10^{-3}\)).
The solver ensures (approximately) that at each step:
\[ |e(t)| \leq \mathrm{atol} + \mathrm{rtol} \times |x(t)| \]
Example:
Using a small max_step
is usually the wrong way to โget
more data pointsโ since this will trigger many (potentially expensive)
evaluations of fun
.
Instead, use dense outputs: the solver may return the discrete data
result["t"]
and result["y"]
and an approximate solution result["sol"]
as a function of t
with little extra
computations.