I’ve written some code to perform parameter estimation on a system of ODEs, and I keep getting the following error:

```
┌ Warning: First function call produced NaNs. Exiting.
└ @ OrdinaryDiffEq C:\Users\Michael\.julia\packages\OrdinaryDiffEq\5GBYL\src\initdt.jl:76
┌ Warning: Automatic dt set the starting dt as NaN, causing instability.
└ @ OrdinaryDiffEq
```

(I actually got several hundred of these warnings at once.)

Here are some relevant details about the problem:

- This is the ODE system whose parameters I’m trying to estimate:

```
function One_Age_Model(du,u,pars,t)
S, E, P, A, AR, I, H, R, D, Hc = u
βSA, βSI, μ, σ₁, σ₂, ϕ, M, M_AR, γ, ω, χ, ψ, ηSA, ηSI, tQ = pars
βSA_t = βSA*(ηSA + (1 - ηSA)*0.5*(1 - tanh(2*(t-tQ))))
βSI_t = βSI*(ηSI + (1 - ηSI)*0.5*(1 - tanh(2*(t-tQ))))
dS = -βSA_t*S*(A+P) - βSI_t*S*I - μ*S
dE = βSA_t*S*(A+P) + βSI_t*S*I - σ₁*E
dP = (1-ϕ)*σ₁*E - σ₂*P
dA = ϕ*σ₁*E - M_AR*A
dAR = M_AR*A
dI = σ₂*P - M*I
dH = γ*M*I - (1-ω)*χ*H - ω*ψ*H
dR = (1-γ)*M*I + (1-ω)*χ*H
dD = ω*ψ*H
dHc = γ*M*I
du .= [dS, dE, dP, dA, dAR, dI, dH, dR, dD, dHc]
return nothing
end
```

- Here’s my call to the integrator:

```
ODE_prob = ODEProblem(One_Age_Model, ODE_ICs_scaled, t_span, ODE_params)
ODE_sol = solve(ODE_prob, integrator, reltol = 1e-8, abstol = 1e-8 saveat = t_obs)
```

Here, `t_span = (0.0,100.0)`

and `t_obs = collect(0.0:1.0:100.0)`

.

- I’m using ForwardDiff for the optimization, and I recently learned that the ODE solvers apparently also use ForwardDiff (I did not know this before…)

Here’s a quick summary of my code: I’m using the so-called “boostrapping” approach to parameter estimation, in which I generated 200 sets of “simulated” data from a set of observed data by adding random noise to the observed data. For each set of simulated data, I’m running 5 optimizations using IPNewton from Optim (so 5 x 200 = 1000 total optimizations). For each optimization I give the optimizer a random initial guesses, and I record the best (out of 5) minimizers for each data set.

I got the above error message when the program was 97% of the way through the 1000 optimizations (after about 10 hrs!), and when it was finished, it returned a list of just 2 minimizers (arg!! Why?!). So it seems as if the integration was failing throughout the entire program pretty much…I’m very confused because I’ve successfully run my code on small batches (~50 total optimizations). The occurrence of the error seems somewhat random.

So my questions are: How did the initial dt even get set to NaN? Is there a way to set the initial dt manually to prevent this from happening? Is the issue with ForwardDiff somehow, or is it possibly the ODE model itself? Any other things I should try?

I’m less inclined to believe the ODE model is the problem, as I’ve solved it thousands of times before in previous parameter estimation problems. Most of the time it integrates without a hitch, but other times this NaN dt error happens…

Any insights or troubleshooting suggestions would be hugely appreciated!