MATLAB: How can i find error when using fmincon and nonlcon

fminconfunctionmatlab functionnonlinearoptimization

I use fmincon and problems appeared when using nonlcon.
R_tek=@(dec,acc) ...
S_tek= ...
L_tek=@(dec,acc) ...
first=@(acc) 15.2+numel(zero_slo)*acc;
dec_rate=@(dec,acc) ...
B _tek=@(dec,acc) (2000*1/(2*5)*dec_rate(dec,acc));
B=@(dec,acc) sum(B _tek(dec,acc));
L=@(dec,acc) sum(L_tek(dec,acc));
S =sum(S_tek);
R =@(dec,acc) sum(R_tek(dec,acc));
T_tek=@(dec,acc) B_tek(dec,acc)+R_tek(dec,acc)+S_tek+L_tek(dec,acc)>=0; %T_tek must be greater than zero.
T=@(dec,acc) sum(T_tek(dec,acc));
TT=@(YH) T_acc(YH(2))+T(YH(1),YH(2));
x0=[0,0];
A=[1 ,-1.066441];
b= 0.058766;
Aeq=[];
beq=[];
lb=[0,0];
ub=[];
YH=fmincon(TT,x0,A,b,Aeq,beq,lb,ub)
TT should be minimized provided that T_tek>=0. When i run this code,
YH=[0.0043 0] and TT=2.7426e+03
But, for ex. for YH(1)= 0.05 and YH(2)=0
TT=2.7426e+03 (same results)
All of the TT values are same. I don't know what is origin of this problem. How can i achieve it?
And, When nonlcon is active, for YH=[0.0043 0] TT results as 2.7426e+03. If I remove nonlcon, for same YH value, TT results as. 1.5081e+04 Why are they different?
Thanks a lot.

Best Answer

Your T_tek() function is binary, and hence T() is piecewise constant. This means that small changes (and maybe large ones) in YH do not affect the T() term in TT. Effectively, the objective is only a function of YH(2), through the term T_acc() whatever that is.
You cannot minimize anything you want with fmincon. The objective and constraint functions must be continuous and differentiable, so piecewise constant functions are out. It is strange that you don't include
B_tek(dec,acc)+R_tek(dec,acc)+S_tek+L_tek(dec,acc)>=0
in your nonlcon function. That would be the correct way to implement this constraint, assuming B,R,L_tek are smooth functions.
In any case, it is natural that you would get a lower objective when constraints are relaxed. The minimum over a larger, less constrained region cannot be greater than the original region, and may be less.