# Nonlinear JuMP - vector of expressions+differences between @NL and no @NL

**URL:** <https://discourse.julialang.org/t/nonlinear-jump-vector-of-expressions-differences-between-nl-and-no-nl/102405>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [August 2, 2023, 4:20pm UTC](https://discourse.julialang.org/t/nonlinear-jump-vector-of-expressions-differences-between-nl-and-no-nl/102405 "2023-08-02T16:20:34Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)\
**Post date:** [August 3, 2023, 7:20am UTC](https://discourse.julialang.org/t/nonlinear-jump-vector-of-expressions-differences-between-nl-and-no-nl/102405/4 "2023-08-03T07:20:18Z")

</div>

Having written all this stuff, I realised what the error was, at least for Error 2 and Error 3, and have a guess for Error 1. As usual, the error was between the screen and the chair 🙃

For future reference:  
When I create an expression like this:

```julia
@expression(vdP,
        #RHS of ODE
        x_ODE[j=1:n-1,m=1:2, k=1:noScenarios], [
            x[j,2,k]
            scenariosMu[k]*(1.0-x[j,1,k]*x[j,1,k])*x[j,2,k]-x[j,1,k]
        ]
)

```

I am effectively saying that for every index `j=1:n-1, m=1:2, k=1:noScenarios`, my `x_ODE[j,m,k]` is a vector with two elements. For example:

![image](https://global.discourse-cdn.com/julialang/original/3X/f/2/f2d406bb1b5c0797d5e05b6ef8d2f6cfdbfc1c63.png)

This is why `@constraint(vdP,[j=1:n-1,m=1:2,k=1:noScenarios], x[j+1, m, k] == x[j, m, k] + Δt * x_ODE[j,m, k])` gives me the error `no method matching +(::VariableRef, ::Vector{AbstractJuMPScalar})`. My `x[j,m,k]` is a scalar (correct), but my `x_ODE[j,m, k]` is a vector (incorrect). Error 2 sorted.

If I do broadcasting `@constraint(vdP,[j=1:n-1,m=1:2,k=1:noScenarios], x[j+1, m, k] .== x[j, m, k] .+ Δt * x_ODE[j,m, k])` I assign two different equality constraints to the same `x[j,m,k]` which corresponds to overconstraining my problem. That’s why too few degrees of freedom. Error 3 sorted.

That leaves Error 1. I guess this thread explains this [issue](https://discourse.julialang.org/t/using-literal-1-0-in-jump-constraint-throws-an-error/98496). As my RHS `@constraint(vdP,[j=1:n-1,m=1:2,k=1:noScenarios], x[j+1, m, k] == 1.0-1.0+ x[j, m, k] + Δt * x_ODE[j,m, k])` is a vector (incorrectly) of abstract elements, the value `1.0-1.0` should also be a vector, but that doesn’t work, as indicated in the thread linked.

An ugly hack to sort out my incorrect indexing is to use:  
`@constraint(vdP,[j=1:n-1,m=1:2,k=1:noScenarios], x[j+1, m, k] == x[j, m, k] + Δt * x_ODE[j,1, k][m])`

Or actually write the expression with correct indexing:

```julia
@expressions(
    vdP,
    begin
        #initial conditions
        init_x[m=1:2, k=1:noScenarios], x[1, m, k] - x_0[m]
        #RHS of ODE - no need to have another index
        x_ODE[j=1:n-1,k=1:noScenarios], [
            x[j,2,k]
            scenariosMu[k]*(1.0-x[j,1,k]*x[j,1,k])*x[j,2,k]-x[j,1,k]
        ]
    end
)

@constraint(vdP,[j=1:n-1,k=1:noScenarios], x[j+1, 1:2, k] .== x[j, 1:2, k] .+ Δt * x_ODE[j,k])

```

That leaves only the different number of iterations in different formulations.

---

_[View the full topic](https://discourse.julialang.org/t/nonlinear-jump-vector-of-expressions-differences-between-nl-and-no-nl/102405)._
