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

**URL:** https://discourse.julialang.org/t/another-type-problem-with-forwarddiff-typeerror-in-typeassert-expected-float64-got-a-value-of-type-forwarddiff-dual-nothing-float64-1/77363
**Category:** New to Julia
**Tags:** question, forwarddiff
**Created:** [March 3, 2022, 3:30pm UTC](https://discourse.julialang.org/t/another-type-problem-with-forwarddiff-typeerror-in-typeassert-expected-float64-got-a-value-of-type-forwarddiff-dual-nothing-float64-1/77363 "2022-03-03T15:30:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Michael](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@Michael](https://discourse.julialang.org/u/Michael)
#### Post date: [March 3, 2022, 3:30pm UTC](https://discourse.julialang.org/t/another-type-problem-with-forwarddiff-typeerror-in-typeassert-expected-float64-got-a-value-of-type-forwarddiff-dual-nothing-float64-1/77363/1 "2022-03-03T15:30:22Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [March 3, 2022, 7:10pm UTC](https://discourse.julialang.org/t/another-type-problem-with-forwarddiff-typeerror-in-typeassert-expected-float64-got-a-value-of-type-forwarddiff-dual-nothing-float64-1/77363/2 "2022-03-03T19:10:10Z")

</div>

```julia
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.

---

<div class="post-metadata">

### Author: ![Michael](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@Michael](https://discourse.julialang.org/u/Michael)
#### Post date: [March 7, 2022, 1:43pm UTC](https://discourse.julialang.org/t/another-type-problem-with-forwarddiff-typeerror-in-typeassert-expected-float64-got-a-value-of-type-forwarddiff-dual-nothing-float64-1/77363/3 "2022-03-07T13:43:14Z")

</div>

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
