# Optimization on Vector-ODEs for parameter estimation

**URL:** https://discourse.julialang.org/t/optimization-on-vector-odes-for-parameter-estimation/103008
**Category:** New to Julia
**Tags:** question
**Created:** [August 20, 2023, 4:18pm UTC](https://discourse.julialang.org/t/optimization-on-vector-odes-for-parameter-estimation/103008 "2023-08-20T16:18:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Laurendie\_Magistrali](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurendie_magistrali/32/52366_2.png) [@Laurendie\_Magistrali](https://discourse.julialang.org/u/Laurendie_Magistrali)
#### Post date: [August 20, 2023, 4:18pm UTC](https://discourse.julialang.org/t/optimization-on-vector-odes-for-parameter-estimation/103008/1 "2023-08-20T16:18:48Z")

</div>

So I’m trying to estimate Parameters from given Data, which I generated by solving the ODE problem with given parameters first and then adding a random stray around the result. Using the build\_loss\_objective I generated the cost function which is then inserted into optimization. From here, the estimated parameter values can be gathered by result.minimizer .

My first working example for regular ODE looked like that:

```julia
using DifferentialEquations, Random, Plots
using DiffEqParamEstim, Optim

\###############################################

# ODE Model

```

###############################################

```julia

function fun(G,p,t)
 k, β = p
 #ODE
 dG_dt = k - β\*G
 return dG_dt
end

\#Parameter values
p = [0.8, 0.2] #"true" Parameter values
G_ini = 0.0
tspan = (0.0,10.0)
\#ODE Problem
prob = ODEProblem(fun,G_ini,tspan,p)
\#solving ODE Problem
sol = solve(prob,Tsit5())

\###############################################

# Synthetic Data

\###############################################

dataset = [(t,sol(t)+0.2randn()) for t in 0:0.01:10]

\#########
\#Cost function and Optimization
\############################

\#Gathering

dataTime = [d[1] for d in dataset] #time points
dataValues = [d[2] for d in dataset] #Data

cost_function = build_loss_objective(prob,Tsit5(),L2Loss(dataTime,dataValues),maxiters=10000,verbose=false)

initialGuess = ones(2) #guesssed parameter values
result = optimize(cost_function, initialGuess, BFGS())

```

with that example I can get estimated values for the 2 parameters k and β

But now I want to extend the parameter estimation and for that I’d like to estimate parameters on a system of Vector-ODEs. Going from my last simpler example I got something like:

```julia
using DifferentialEquations,DiffEqParamEstim,RecursiveArrayTools
using Optim,Plots

\###############################################

# ODE Model

 \###############################################

n=10
function f(du,u,p,t)
 du[1,1] = dc_n = 4*p[1]*u[1,1] -u[1,1]*u[1,2]  
 du[1,2] = dc_d = p[2]*u[1,2]^2 + u[1,1]*u[1,2]

 du[n,1] = dc_n = 3*p[1]*u[n,1] -u[n,1]*u[n,2]                     
 du[n,2] = dc_d = -2*p[2]*u[n,2]^2 + u[n,1]*u[n,2]

 for i in 2:(n-1)
    du[i,1] = dc_n = p[1]*u[i-1,1] -u[i,1]*u[i+1,2]               
    du[i,2] = dc_d = -p[2]*u[i-1,2]^2 + u[i,1]*u[i+1,2]
 end

end

u0 = hcat(ones(n), ones(n))
tspan = (0.0,10.0)
p = [1.5,2.0,1.2]

prob = ODEProblem(f,u0,tspan,p)
sol = solve(prob,Tsit5())

\###############################################

# Synthetic Data

\###############################################
dataset = [(t,sol(t) .+0.2randn()) for t in 0:0.01:10]

dataTime = [d[1] for d in dataset] #Auffassen Zeitpunkte
dataValues = [d[2] for d in dataset] #Auffassen Exp. Data
\#until here everything works

cost_function = build_loss_objective(prob,Tsit5(),L2Loss(dataTime,dataValues),maxiters=10000,verbose=false)
initialGuess = ones(3) #alle Parameter auf eins gesetzt (Ratewerte)
result = optimize(cost_function, initialGuess, BFGS())

```

when running the “optimze” function, it tells me in the terminal that there has been a failure and no parameters have been estimated. Where exactly would I need to change something to make this code work?

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [August 21, 2023, 7:46pm UTC](https://discourse.julialang.org/t/optimization-on-vector-odes-for-parameter-estimation/103008/2 "2023-08-21T19:46:03Z")

</div>

When running your code I see:

```julia
julia> sol = solve(prob, Tsit5());
┌ Warning: dt(1.7763568394002505e-15) <= dtmin(1.7763568394002505e-15) at
│ t=0.35552611924380084, and step error estimate = 0.19101346055879606. Aborting.
│ There is either an error in your model specification or the true solution is
│ unstable.
└ @ SciMLBase /home/user/.julia/packages/SciMLBase/kTUaf/src/integrator_interface.jl:599

```

I suspect this is what is causing the optimization to report failure.

---

<div class="post-metadata">

### Author: ![Laurendie\_Magistrali](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurendie_magistrali/32/52366_2.png) [@Laurendie\_Magistrali](https://discourse.julialang.org/u/Laurendie_Magistrali)
#### Post date: [August 21, 2023, 11:33pm UTC](https://discourse.julialang.org/t/optimization-on-vector-odes-for-parameter-estimation/103008/3 "2023-08-21T23:33:35Z")

</div>

Thank your for the Reply, what could be meant with “model specification”?

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [August 21, 2023, 11:53pm UTC](https://discourse.julialang.org/t/optimization-on-vector-odes-for-parameter-estimation/103008/4 "2023-08-21T23:53:46Z")

</div>

It means there is no timestep small enough to successfully integrate `f(du,u,p,t)` as implemented beyond `t=0.35552611924380084`. Probably a typo, missing term, swapped sign or something like that.
