HI,

I am working on a code and have all my constraints in ‘for’ loop.

Can I assign and get the dual variable values associated with an equality constraint? I’m trying the method given in the examples but the code always gives an error (Dual variable “lambda” has already been assigned.)

excerpt from the code:

cvx_begin

variables pG(5) qG(5)

dual variable lambda

minimize c*(pG)

subject to

```
for k=1:5
bus(:,k)'*pG - pD(k) == pf %%% need dual variable value for these set of equations
end
```

…

cvx_end

Dual variables can be assigned to equality constraints (you just have to be careful on the sign convention, or lack thereof, by various dual variable generators and users).

Have you looked at the example in http://cvxr.com/cvx/doc/advanced.html#indexed-dual-variables and used index dual variables?

Thank you, I tried implementing the dual variable similarly as in the example shared, however, the error I’m getting now is ‘Undefined operator ‘:’ for input arguments of type ‘cvx’.’ However, my other simple codes do not give such error when I define dual variables.

cvx_begin

variables pG(n) qG(n)

dual variables y{n}

minimize c*(pG)

subject to

```
for k=1:n
bus(:,k)'*pG - pD(k) == pf : y{k}; %%% need dual variable value for these set of equations
end
```

I don;t know why the error is occurring,

But I think you don’t need indexed dual variables.

Instead of the for loop, use

```
dual variable y
bus*pG - pD == pf :y
```

Although I’m not sure that works either.

Hmm, perhaps there is a bug in CVX. Full disclosure: I never use dual variables, so don’t have experience with them or know whether this syntax used to work.

However, this example from http://cvxr.com/cvx/doc/advanced.html#indexed-dual-variables works

```
n=2; b=[0;0];
cvx_begin
variable X( n, n ) symmetric
dual variables y{n}
minimize( ( n - 1 : -1 : 0 ) * diag( X ) );
for k = 0 : n-1,
sum( diag( X, k ) ) == b( k+1 ) : y{k+1};
end
X == semidefinite(n);
cvx_end
```