How to analyze sensitivity w.r.t. the initial values?

The problem seems to be that the Float64 variable loss defined inside the function loss is overwriting the function. So at line 34, when preparing the optimization problem to calculate the loss, loss refers to the value of the loss, not the function to compute it. Placing the definition of the function loss in at the top level instead of inside the function get_iv, marking that value as an explicit local

     local loss
     loss = sum(abs2, sol.u[end] - target)
     return loss, sol

or renaming the Float64 value to something else (for example lss = sum(abs2, sol.u[end] - target)) all restore the behavior you hoped for.

2 Likes