# Parameter estimation DifferentialEquations.jl with Optim.jl

**URL:** https://discourse.julialang.org/t/parameter-estimation-differentialequations-jl-with-optim-jl/11145
**Category:** New to Julia
**Tags:** diffeq, optim, optimization
**Created:** [May 25, 2018, 1:11pm UTC](https://discourse.julialang.org/t/parameter-estimation-differentialequations-jl-with-optim-jl/11145 "2018-05-25T13:11:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![moesphere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moesphere/32/3348_2.png) [@moesphere](https://discourse.julialang.org/u/moesphere)
#### Post date: [May 25, 2018, 1:11pm UTC](https://discourse.julialang.org/t/parameter-estimation-differentialequations-jl-with-optim-jl/11145/1 "2018-05-25T13:11:35Z")

</div>

Hello,

I get a dimension mismatch while broadcasting when trying to estimate parameters with objects from the DifferentialEquations.jl package using the Optim.jl package. I have tried different numbers of data points. It seems that the output of the model, i.e. the differential equation, has two more elements than there are data points (I checked that in the supervised.jl file). I have provided the following example:

```julia
using DifferentialEquations,Optim,LossFunctions
xdata = collect(linspace(0.1,9.5,10))
ydata = randn(length(xdata))
function model4(dx,x,p,t)
    dx[1] = -p[1]*x[1]
end
tspan = (0.0,10.0)
prob = ODEProblem(model4,[10.0],tspan,[1.0])
lossfcn = CostVData(xdata,ydata;loss_func=L2DistLoss)
cost_function = build_loss_objective(prob,Tsit5(),lossfcn)
result = optimize(cost_function, [1.0])

```

The optimize function throws the DimensionMismatch error. I also tried to enforce the saveat option, but didn’t work out. Any ideas?

Regards,  
Moritz

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [May 25, 2018, 1:54pm UTC](https://discourse.julialang.org/t/parameter-estimation-differentialequations-jl-with-optim-jl/11145/2 "2018-05-25T13:54:37Z")

</div>

This sounds a bit like the problem I had here: [https://github.com/JuliaDiffEq/DiffEqBayes.jl/pull/42#discussion\_r186036462](https://github.com/JuliaDiffEq/DiffEqBayes.jl/pull/42#discussion_r186036462)

Maybe you can patch it to not use `saveat` and instead use interpolation like I used: [https://github.com/JuliaDiffEq/DiffEqBayes.jl/pull/42/files#r186114099](https://github.com/JuliaDiffEq/DiffEqBayes.jl/pull/42/files#r186114099).

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [August 23, 2018, 2:27pm UTC](https://discourse.julialang.org/t/parameter-estimation-differentialequations-jl-with-optim-jl/11145/3 "2018-08-23T14:27:59Z")

</div>

This was due to an error in how `saveat` was defined which made it save start and save end only using `save_start` and `save_end` commands, rather than checking whether the start and end are in `saveat`. This was changed in DiffEq v5.0 (on Julia v1.0) to just work like you expected.
