I am having a strange problem with ForwardDiff that is internally used in Optim. The minimum returned by Optim is not the same value as the value of the objective function when the minimizer returned by Optim is used, see the following MWE.
using Julia
using DifferentialEquations
function f(du,u,p,t)
du[1] = dx = p[1]*u[1] - u[1]*u[2]
du[2] = dy = -3*u[2] + u[1]*u[2]
end
u0 = [1.0;1.0]
tspan = (0.0,10.0)
p = [1.5]
prob = ODEProblem(f,u0,tspan,p)
tstops = range(0.,stop=10.,length=10)
sol = solve(prob,Tsit5(),saveat=tstops)
randomized = VectorOfArray([(sol(t[i]) + .01randn(2)) for i in 1:length(tstops)])
data = convert(Array,randomized)
function least_squares(x)
_prob = remake(prob, u0=convert.(eltype(x),prob.u0),p=x)
sol = solve(_prob,Tsit5(),saveat=tstops)
sum((hcat(sol.u...) .- data).^2)
end
result = optimize(least_squares, [5.], Newton(),autodiff=:forward)
result.minimum # returns 275.97
least_squares(result.minimizer) # returns 276.68