# DiffEqParamEstim TypeAssert problem with forward AD

**URL:** https://discourse.julialang.org/t/diffeqparamestim-typeassert-problem-with-forward-ad/46553
**Category:** Modelling & Simulations
**Tags:** diffeq, ode, forwarddiff
**Created:** [September 13, 2020, 4:36pm UTC](https://discourse.julialang.org/t/diffeqparamestim-typeassert-problem-with-forward-ad/46553 "2020-09-13T16:36:36Z")
**Posts on this page:** 4
**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: [September 13, 2020, 4:36pm UTC](https://discourse.julialang.org/t/diffeqparamestim-typeassert-problem-with-forward-ad/46553/1 "2020-09-13T16:36:36Z")

</div>

I am getting a typeassert problem when I use the `build_objective_function` in combination with forward AD. Optimization is via `Optim`. It works if I use finite differencing or e.g. `Tsit5()` instead of `Rosenbrock23()` as the ode solver.

MWE:

```julia
using DiffEqParamEstim, DifferentialEquations, Optim
function ODE(dxdt,x,p,t)

    C = 70000. + p[1]*t/500. + p[2]
    D = 1.

    dxdt[1] = - C - D
    dxdt[2] = C
    dxdt[3] = D 

end

u0 = [0.25235; 0.; 0.]
tspan = (0.,200.)
p0 = [1.;1.]

prob = ODEProblem(ODE, u0, tspan,p0)

sol = solve(prob,Rosenbrock23())
t = collect(range(0,stop=1110.,length=200))
randomized = VectorOfArray([(sol(t[i]) + .01randn(3)) for i in 1:length(t)])
data = convert(Array,randomized)

cost_function = build_loss_objective(prob,Rosenbrock23(),L2Loss(t,data),maxiters=10000,verbose=false)
result = optimize(cost_function, zeros(2), 10.0*ones(2), [4.,5.],autodiff=:forward)

```

```julia

```

---

<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: [September 14, 2020, 6:54am UTC](https://discourse.julialang.org/t/diffeqparamestim-typeassert-problem-with-forward-ad/46553/2 "2020-09-14T06:54:49Z")

</div>

The `Project.toml` file is the following:

![image](https://global.discourse-cdn.com/julialang/original/3X/9/3/937259355aff61041e765af5906454599fd0f6b7.png)

---

<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: [September 14, 2020, 6:56am UTC](https://discourse.julialang.org/t/diffeqparamestim-typeassert-problem-with-forward-ad/46553/3 "2020-09-14T06:56:12Z")

</div>

I would recommend using DiffEqFlux instead these days.

---

<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: [September 14, 2020, 7:08am UTC](https://discourse.julialang.org/t/diffeqparamestim-typeassert-problem-with-forward-ad/46553/4 "2020-09-14T07:08:00Z")

</div>

Thanks for the hint, I will give it a try.
