Another Type Problem with ForwardDiff : TypeError: in typeassert, expected Float64, got a value of type ForwardDiff.Dual{Nothing, Float64, 1}

using ForwardDiff, OrdinaryDiffEq

t0 = 0.0
tf = 10.0
x0 = [1.0; 2.0]
p = 0.0

F(x) = [x[1] - x[2]; 2 * x[1]]

function F!(dx, x, p, t)
    dx[1:2] = F(x)
end

function flow(t0, t, x0)
    ode1 = ODEProblem(F!, convert.(typeof(t),x0), (t0, t), p)
    z = solve(ode1, Tsit5(), abstol = 1e-12, reltol = 1e-12)
    return z[end]
end

foo = y -> flow(t0, y, x0)
y = tf
ForwardDiff.derivative(foo, y)

Needed to convert the type the state to dual if time is dual. I can patch this to happen automatically.

3 Likes