# Adding NL expressions to an AffExpr type Vector

**URL:** <https://discourse.julialang.org/t/adding-nl-expressions-to-an-affexpr-type-vector/99279>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, arrays\
**Created:** [May 23, 2023, 1:29pm UTC](https://discourse.julialang.org/t/adding-nl-expressions-to-an-affexpr-type-vector/99279 "2023-05-23T13:29:49Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [May 23, 2023, 8:08pm UTC](https://discourse.julialang.org/t/adding-nl-expressions-to-an-affexpr-type-vector/99279/2 "2023-05-23T20:08:21Z")

</div>

Hello again 😄 this question is follow-up of [How to create an user-defined function with multiple vector as inputs](https://discourse.julialang.org/t/how-to-create-an-user-defined-function-with-multiple-vector-as-inputs/99068).

A few things:

1. `AffExpr` is a type for _affine_ expressions. You cannot add nonlinear terms to it
2. You cannot call a user-defined function outside of a macro
3. You cannot call `value` before the model has been solved

It’s hard to offer complete advice without a reproducible example that I can copy-and-paste (what is StateFunc, what are `X`, `g_0`, `c`, and `Δt`?), but this should point you in the right direction:

```julia
register(rocket2d_hs_2, :StateFunc, 10, StateFunc; autodiff=true)

for j in 1:n-1
    x_c = map(1:7) do i
        if i < 6
            @NLexpression(
                rocket2d_hs_2,
                0.5*(X[i,j]+X[i,j+1]) + (Δt/8)*(StateFunc(g_0,c,i,X[:,j]...)-StateFunc(g_0,c,i,X[:,j+1]...)),
            )
        else
            @NLexpression(rocket2d_hs_2, (X[i,j]+X[i,j+1])/2)
        end
    end
    @NLconstraint(
        rocket2d_hs_2,
        [i in 1:5],
        X[i,j+1] - X[i,j] == (Δt/6) * (StateFunc(g_0,c,i,X[:,j]...) + StateFunc(g_0,c,i,x_c...) + StateFunc(g_0,c,i,X[:,j+1]...))
    )
end

```

---

_[View the full topic](https://discourse.julialang.org/t/adding-nl-expressions-to-an-affexpr-type-vector/99279)._
