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

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 is infeasible, then introduce a slack vector variable slack_vector(M) nonnegative
Change the constraint A*x <= b to A*X <= b + slack_vector
Then instead of your original objective, use 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,