Adding NL expressions to an AffExpr type Vector

Hello again :smile: this question is follow-up of How to create an user-defined function with multiple vector as inputs.

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:

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