# Still be confused in how to add a slack variable

Now i am trying to add a slack variable to this optimal problem: Why does the optimal value become NaN sometimes?

And here is my reference about adding slack variable section 6 in this website https://yalmip.github.io/debugginginfeasible/

And here is the slack code of the website,but there are somethings i don’t understand in it

``````slack1 = sdpvar(N,1);
slack2 = sdpvar(N,1);
Constraints = [slack1>=0]
for i = 1:N
Constraints = [Constraints, something1 <= slack1(i)];
Constraints = [Constraints, something2 == slack2(i)];
end
``````

My questions about the example are
1.so if i have three constraints,i need to produce three slack variables?
2.do i need to build Constraints = [slack2>=0] also?
3.what does the "something " mean in his example code?value?vector?matrix? or a formula
4.How do i define the N ? because in one constraint,there are not only one vector,but also the other vector,some vectors may be 5 by 1,may be 4 by 1,so i don’t know what value should i assume to the N

Here is my code below,i don’t think this is right,unless i know the explanation of the question i ask ,the N i assume is 4 ,because i only have one kind of vector,4 by 1 vector

``````slack_for_C3 = sdpvar(4,1);
slack_for_C5  = sdpvar(4,1);
slack_for_C10  = sdpvar(4,1);
Constraints = [ slack_for_C3 >=0]
for i = 1:4
Constraints = [Constraints, something1 <= slack_for_C3(i)];
Constraints = [Constraints, something2 == slack_for_C5(i)];
Constraints = [Constraints, something3 == slack_for_C10(i)];
end``````

Use a slack variable, or element of a slack variable, for each constraint which you wish to diagnose by slack.variables.

`variable X(N)`
if `A*x <= b i`s infeasible, then introduce a slack vector
`variable slack_vector(M) nonnegative`
Change the constraint `A*x <= b` to
`A*X <= b + slack_vector`
`minimize(sum(slack_vector))`

The optimal `slack_vector` will have positive elements for rows of `A*X <= b` which are not feasible.

For equality constraints, you can change
`A*x == b`
to

``````variable slack_vector_equality(M)
A*x == b + slack_vector_equality
minimize(slack_vector_equality'*slack_vector_equality)
``````

The optimal `slack_vector_equality` will have significantly non-zero elements for rows of `A*X == b` which are not feasible.

If you have both inequalities and equalities, you can add the objective terms for both into one objective.

Can you take my code for example? Why does the optimal value become NaN sometimes? Because it seems that you just said something information about slack variable,but i still don’t know how to use it in my code

Do not use the original objective function of the optimization problem, or any part of that objective function.

For each `<=` constraint, add a nonnegative slack variable to the RHS, and include a term in the objective to minimize the sum of all those slacks.

For a semidefinite constraint
`LHS == semidefinite(n)` (or `LHS >= 0` in sdp mode),
change it to
`LHS + slack*eye(n) == semidefinite(n)` (or `LHS + slack*eye(n) >= 0` in sdp mode) , where slack is a nonnegative variable, and include a term in the objective to minimize the sum of all those slacks.

For each `==` constraint, add an unconstrained slack variable to the RHS (or LHS, it doesn’t matter), and include a term in the objective to minimize the sum of squares of all those slacks,

That’s all there is to it.