# Problems with Dual type using JuMP, NLopt and DifferentialEquations

**URL:** https://discourse.julialang.org/t/problems-with-dual-type-using-jump-nlopt-and-differentialequations/17159
**Category:** New to Julia
**Created:** [November 5, 2018, 8:57am UTC](https://discourse.julialang.org/t/problems-with-dual-type-using-jump-nlopt-and-differentialequations/17159 "2018-11-05T08:57:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![liubomyrv](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@liubomyrv](https://discourse.julialang.org/u/liubomyrv)
#### Post date: [November 5, 2018, 8:57am UTC](https://discourse.julialang.org/t/problems-with-dual-type-using-jump-nlopt-and-differentialequations/17159/1 "2018-11-05T08:57:13Z")

</div>

Hello Julia community. I am using `Julia v.0.6.4` and trying to understand how an optimization works in Julia, using `JuMP.jl` and `NLopt.jl`. I am also using `DifferentialEquations.jl` to specify the objective function. And now, I’ve got stuck with the Dual format that is probably used to define a gradient. Here is an example:

```julia
using JuMP
using NLopt
using DifferentialEquations

function f_tetra!(du,u,p,t)
    du[1] = -p[1]*u[1]
    du[2] = p[1]*u[1] - p[2]*u[2]
end

function run_tetra(theta1,theta2,theta3)
    println(theta1,theta2,theta3)
    tdata = [1, 2, 3, 4, 6, 8, 10, 12, 16]
    ydata = [0.7, 1.2, 1.4, 1.4, 1.1, 0.8, 0.6, 0.5, 0.0]
    prob = ODEProblem(f_tetra!,[theta3,0.0],(0.0,tdata[end]),(theta1,theta2))
    sol = DifferentialEquations.solve(prob,saveat=tdata,save_start = false)
    return(sum(ydata-convert(Array,sol)[2,:]).^2)
end

modd = Model(solver=NLoptSolver(algorithm=:LD_MMA))
JuMP.register(modd, :run_tetra, 3, run_tetra, autodiff=true)

@variable(modd, x1 >= 1e-7)
@variable(modd, x2 >= 1e-7)
@variable(modd, x3 >= 1e-7)

@NLobjective(modd, Min, run_tetra(x1, x2, x3))
@constraint(modd, x1-x2 >= 1e-7)

println(modd)

setvalue(x1, 1.0)
setvalue(x2, 1.0)
setvalue(x3, 1.0)

status = JuMP.solve(modd)

println("got ", getobjectivevalue(modd), " at ", [getvalue(x1),getvalue(x2),getvalue(x3)])

```

Here I get a followed error:

 ![Ex_error](https://global.discourse-cdn.com/julialang/original/3X/6/3/63af9364942f4a0d6ed7e970bf92650cd0bc7774.png)

Could someone please help me and show what I am doing wrong?

---

<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: [November 5, 2018, 3:02pm UTC](https://discourse.julialang.org/t/problems-with-dual-type-using-jump-nlopt-and-differentialequations/17159/2 "2018-11-05T15:02:54Z")

</div>

> [@liubomyrv](#):
>
> prob = ODEProblem(f\_tetra!,[theta3,0.0],(0.0,tdata[end]),(theta1,theta2))

This is a v0.6 problem which has since been corrected on v1.0 so I would recommend upgrading when you can. But the issue is that, on v0.6, it was required that time was specified to be a dual number to propogate it effectively through the time stepping adaptivity. Thus you’d want:

```julia
prob = ODEProblem(f_tetra!,[theta3,0.0],(0.0,eltype(theta3)(tdata[end])),(theta1,theta2))

```

so that way when `theta3` is a Dual number it will upconvert time to Duals. Again, on v1.0 this isn’t necessary.

---

<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: [November 5, 2018, 3:13pm UTC](https://discourse.julialang.org/t/problems-with-dual-type-using-jump-nlopt-and-differentialequations/17159/3 "2018-11-05T15:13:03Z")

</div>

And FWIW I just ran your code on v1.0 and it worked without changing anything.

```julia
got 1.400982598809288e-14 at [1.31674, 0.126062, 1.71748]

```

which seems like it would be how you generated the true values given how low the least squares is. Given the difference I know about the two, the change I proposed should make it work on v0.6, though I only tested on v1.0.

---

<div class="post-metadata">

### Author: ![liubomyrv](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@liubomyrv](https://discourse.julialang.org/u/liubomyrv)
#### Post date: [November 6, 2018, 9:36am UTC](https://discourse.julialang.org/t/problems-with-dual-type-using-jump-nlopt-and-differentialequations/17159/4 "2018-11-06T09:36:27Z")

</div>

Thanks Chris, now it is clearer.
