Nonlinear expressions may contain only scalar expressions

using JuMP,Gurobi
 n=4
PL=[0.3;0.55;0;0]
model=Model(Gurobi.Optimizer)
G=[1.04209 -0.588235 -0.0 -0.453858;-0.588235 1.069 0 -0.480769;-0 0 0 0;-0.453858 -0.480769 0 0.934627]
Q=[-8.24288   2.35294   3.66667   1.89107;  2.35294  -4.72738   0.0       2.40385;3.66667   0.0      -3.33333   0.0;  1.89107   2.40385   0.0      -4.26159]
@variable(model, U[1:n])
@variable(model, P[1:n])
@variable(model, Ang[1:n])
@variable(model, Angle[1:n,1:n])
@constraint(model, con5[i = 1:n, j = 1:n], Angle[i,j]==Ang[i] .-Ang[j])
@expression(model, my_expr, P-PL)
@NLconstraint(model,co1,my_expr==U.*((G.*cos.(Angle)+Q.*sin.(Angle))*U))

error:


Is P-PL wrong? How could I solve it?

Two issues.

  1. As stated in the error, you can only use scalar expressions. That means no vectors, and no broadcasting. Here is the documentation: https://jump.dev/JuMP.jl/v0.21.1/nlp/#Syntax-notes-1

  2. Gurobi doesn’t support nonlinear. Use Ipopt instead.

1 Like