I am trying to optimize local sensitivities of an ode problem using ODEForwardSensitivityProblem
from the DifferentialEquations
suite, but the last line throws a TypeError
.
I am using forward sensitivites as the ode system is small.
The minimal example is taken from here.
using DiffEqSensitivity
using ForwardDiff
function f(du,u,p,t)
du[1] = dx = p[1]*u[1] - p[2]*u[1]*u[2]
du[2] = dy = -p[3]*u[2] + u[1]*u[2]
end
p = [1.5,1.0,3.0]
prob = ODEForwardSensitivityProblem(f,[1.0;1.0],(0.0,10.0),p)
function sensitivity(x)
_u0 = convert.(eltype(x), prob.u0)
_u0[1] = x[1]
_p = convert.(eltype(x), prob.p)
_p[3] = x[2]
_prob = remake(prob,u0=_u0,p=_p)
sol = solve(_prob,Tsit5())
dp = extract_local_sensitivities(sol)[2]
dp[1][end]
end
sensitivity(ones(2))
eval_grad = x -> ForwardDiff.gradient(sensitivity, x)
eval_grad(ones(2))