ode45
Builtin index · Signal processing
ode45(f, tspan, y0, [rtol=1e-6], [atol=1e-9], [max_step=], [t_eval=], [events=], [direction=0], [terminal=false], [max_steps=500000])
Input: f, tspan, y0, rtol, atol, max_step, t_eval, events, direction, terminal, max_steps · Output: no output
What it is
Dormand-Prince 5(4) with adaptive step size and the 4th-order continuous extension, for non-stiff problems. tspan = [t0, tf] (tf < t0 integrates backwards); a tspan with more than two entries means "report at exactly these times" and is the same as t_eval=, which must be strictly monotone and inside the span -- values between steps come from the interpolant, not from extra steps. The error per component is atol + rtol*
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
ode45(f, tspan, y0, [rtol=1e-6], [atol=1e-9], [max_step=], [t_eval=], [events=], [direction=0], [terminal=false], [max_steps=500000]) | f —not documented yettspan —tspan = [t0, tf] (tf < t0 integrates backwards).y0 —not documented yetrtol optional (default 1e-6)The error per component is atol + rtol*.atol optional (default 1e-9)The error per component is atol + rtol*.max_step optionalnot documented yett_eval optionala tspan with more than two entries means "report at exactly these times" and is the same as t_eval=, which must be strictly monotone and inside the span -- values between steps come from the interpolant, not from extra steps.events optionalnot documented yetdirection optional (default 0)not documented yetterminal optional (default false)not documented yetmax_steps optional (default 500000)not documented yet | not documented yet | Dormand-Prince 5(4) with adaptive step size and the 4th-order continuous extension, for non-stiff problems. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
function fall(t, y)
return [y[1], -9.81]
end function
function ground(t, y)
return y[0]
end function
t0 = 0
y0 = [5, 0] # dropped from 5 m
for k = 1 to 3
r = ode45("fall", [t0, 100], y0, events="ground", direction=-1, terminal=true, rtol=1e-9)
t0 = r.t_events[0]
v = r.y_events[0, 1]
print("bounce {k} at t = {t0}") # 1.009638, 2.625058, 3.917394 (analytic: t1 = sqrt(2h/g), then + 2 * 0.8^k * t1)
y0 = [0, -0.8 * v] # restitution 0.8
end for
bounce 1 at t = 1.009638 bounce 2 at t = 2.625058 bounce 3 at t = 3.917394
from signal-processing
function vdp(t, y)
return [y[1], 1.0 * (1 - y[0] * y[0]) * y[1] - y[0]]
end function
r = ode45("vdp", [0, 20], [2, 0])
print(r.status) # ok
print(r.y[length(r.t) - 1, 0]) # position at t = 20
print(quad((x) := exp(-x * x), -inf, inf)) # 1.7724538509 = sqrt(pi)
ok 2.008149 1.772454
from signal-processing
function sho(t, y)
return [y[1], -y[0]]
end function
te = linspace(0, 5, 51)
r = ode45("sho", [0, 5], [1, 0], t_eval=te, rtol=1e-10, atol=1e-12)
trace_save("reference.npz", {t = r.t, y = r.y[:, 0]})
ref = trace_load("reference.npz")
c = ode23("sho", [0, 5], [1, 0], t_eval=te) # the run under test
d = trace_diff(ref, {t = c.t, y = c.y[:, 0]}, rtol=1e-6, atol=1e-6)
print(d.ok) # false
print(d.worst_signal) # y
print(d.max_abs) # 2.652263e-6, at sample d.worst_index (32)
false y 2.652263e-6
from signal-processing
How it works
and general minimization. Only a user-defined function (by name, the same convention pmap/spawn use) can be the target of any of these — not a builtin. fzero(f, a, b, [tol=1e-10], [max_iter=100]) — Brent's method: robust root finding (guaranteed convergence given a bracket [a,b] where f changes sign), no derivative needed. The "something better than plain bisection" choice.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.