I am trying to use forwardDiff to compute the gradient of a function in which I am solving an ODE, but I am getting an error that there is no method matching Float64. I have been stumped with this error for awile and would greatly appreciate any assistance that could be provided.
using DifferentialEquations
using ForwardDiff
function CostFun(x)
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 = [2.0, 0.0]
tspan = (0.0, 1.0)
prob = ODEProblem(SpringEqu!, u0, tspan, x)
sol = solve(prob)
Simpos = zeros(length(sol.t))
Simvel = zeros(length(sol.t))
tout = zeros(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
xin = [2000.0, 20000.0, 80.0]
g = ForwardDiff.gradient(CostFun, xin)
The error from the function is the following:
LoadError: MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{typeof(CostFun),Float64},Float64,3})
Closest candidates are:
Float64(::Real, ::RoundingMode) where T<:AbstractFloat at rounding.jl:200
Float64(::T) where T<:Number at boot.jl:715
Float64(::Int8) at float.jl:60
...
Stacktrace:
[1] convert(::Type{Float64}, ::ForwardDiff.Dual{ForwardDiff.Tag{typeof(CostFun),Float64},Float64,3})
at .\number.jl:7
[2] setindex!(::Array{Float64,1}, ::ForwardDiff.Dual{ForwardDiff.Tag{typeof(CostFun),Float64},Float64,3}, ::Int64) at .\array.jl:826
[3] CostFun(::Array{ForwardDiff.Dual{ForwardDiff.Tag{typeof(CostFun),Float64},Float64,3},1}) at C:\Users\spenc\github\MetaOpt\autodiffTest.jl:23
[4] vector_mode_dual_eval at C:\Users\spenc\.juliapro\JuliaPro_v1.4.1-1\packages\ForwardDiff\cXTw0\src\apiutils.jl:37 [inlined]
[5] vector_mode_gradient(::typeof(CostFun), ::Array{Float64,1}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(CostFun),Float64},Float64,3,Array{ForwardDiff.Dual{ForwardDiff.Tag{typeof(CostFun),Float64},Float64,3},1}}) at C:\Users\spenc\.juliapro\JuliaPro_v1.4.1-1\packages\ForwardDiff\cXTw0\src\gradient.jl:97
[6] gradient(::Function, ::Array{Float64,1}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(CostFun),Float64},Float64,3,Array{ForwardDiff.Dual{ForwardDiff.Tag{typeof(CostFun),Float64},Float64,3},1}}, ::Val{true}) at C:\Users\spenc\.juliapro\JuliaPro_v1.4.1-1\packages\ForwardDiff\cXTw0\src\gradient.jl:17
[7] gradient(::Function, ::Array{Float64,1}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(CostFun),Float64},Float64,3,Array{ForwardDiff.Dual{ForwardDiff.Tag{typeof(CostFun),Float64},Float64,3},1}}) at C:\Users\spenc\.juliapro\JuliaPro_v1.4.1-1\packages\ForwardDiff\cXTw0\src\gradient.jl:15 (repeats 2 times)
[8] top-level scope at C:\Users\spenc\github\MetaOpt\autodiffTest.jl:34
in expression starting at C:\Users\spenc\github\MetaOpt\autodiffTest.jl:34