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

Hi everyone!

I have a small question concerning the Forwarddiff package. I coded basically the flow function of a differential equation, and I can differentiate it with respect to the initial condition, but cannot take its derivative with respect to time, I get a type error.

The most basic example I could come with is the following (the dynamics itself is irrelevant I just put a random vector field):

using ForwardDiff, OrdinaryDiffEq, DiffEqSensitivity, DifferentialEquations

t0 = 0.
tf = 10.
x0 = [1.; 2.]
p = 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!, 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)

This gives “TypeError: in typeassert, expected Float64, got a value of type ForwardDiff.Dual{Nothing, Float64, 1}”

But when I try to do the whole time - state jacobian it works fine:
foo = y → flow(t0, y[1], y[2:3])
y = [ tf; x0 ]
ForwardDiff.jacobian(foo, y).

Of course the time derivative is just an evaluation of the field but I need to be able to do this for a bigger program. This is one of the ingredients that blocks. Any idea?
Maybe my way of differentiating a flow function like this is highly inefficient, but I need a flow function to do a shooting method later (with nlsolve). I’ll take any recommendation!

Thank you y’all

Michael

PS: one last thing, this doesn’t work either

foo = y → flow(t0, y[1], y)
y = [ tf ]
ForwardDiff.jacobian(foo, y).

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

Ok it’s working now thanks a lot! I’m not familiar with the nature of dual number, got to read the doc from Forwarddiff a bit more in details