# Error with ForwardDiff no method matching Float64

**URL:** https://discourse.julialang.org/t/error-with-forwarddiff-no-method-matching-float64/41905
**Category:** Numerics
**Created:** [June 23, 2020, 1:40am UTC](https://discourse.julialang.org/t/error-with-forwarddiff-no-method-matching-float64/41905 "2020-06-23T01:40:51Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [June 23, 2020, 3:40am UTC](https://discourse.julialang.org/t/error-with-forwarddiff-no-method-matching-float64/41905/2 "2020-06-23T03:40:57Z")

</div>

You need to allow numbers of type `ForwardDiff.Dual` to propagate through `CostFun`, something like this:

```julia
julia> function CostFun(x::AbstractVector{T}) where T
           
           function SpringEqu!(du, u, x, t)
               du[1] = u[2]
               du[2] = -(x[1] / x[3]) * u[2] - (x[2] / x[3]) * u[1] + 50 / x[3]
           end
           
           u0 = T[2.0, 0.0]
           tspan = (0.0, 1.0)
           prob = ODEProblem(SpringEqu!, u0, tspan, x)
           sol = solve(prob)

           Simpos = zeros(T, length(sol.t))
           Simvel = zeros(T, length(sol.t))
           tout = zeros(T, length(sol.t))
           for i = 1:length(sol.t)
               tout[i] = sol.t[i]
               Simpos[i] = sol[1, i]
               Simvel[i] = sol[2, i]
           end

           totalCost = sum(Simpos)
           return totalCost
       end
CostFun (generic function with 2 methods)

julia> g = ForwardDiff.gradient(CostFun, xin)
3-element Array{Float64,1}:
  0.0013186230010380866
 -0.00014123587229879185
  0.001994165580567926

```

---

_[View the full topic](https://discourse.julialang.org/t/error-with-forwarddiff-no-method-matching-float64/41905)._
