MATLAB: Minimizing a scalar value using “fmincon” derived through many variables

fminconscalar minimization

I have portfolio optimal allocation problem (two assets equity and bond) and I want to use "fmincon" to minimize "PCS", which is probability of consumption shortfall.
Using Matlab code I have derived the "PCS" which is a scalar. The code is below:
Return_portfolio = equity_share*Return_equity+(1-equity_share)*Return_bond
W = matrix of wealth in each period
B = matrix of withdrawal in each period
P = (B < b); % matrix showing the incidence of shortfall (realized benefit is less than target)
S = (cumsum(P, 2) == 1) .* P; % Finding the first incidence of shortfall
shortprob = sum(S)/sim; % vector of shortfall probability
PCS = sum(shortprob) ; % Probability of consumption shortfall
Now I want to minimize PCS by chooing right allocation (weight) of portfoilio. Initially I assumed it to be 0.5. This is my "fmincon" set up
fun = @(eq_share) PCS
x0 = [0.5; 0.5]; % initial weight of two assets
Aeq = [1, 1; ER_equity, ER_bonds];
beq = [1; CS];
lb = [0, 0];
ub = [1, 1];
[weights, PCS] = fmincon(fun, x0, Aeq, beq, lb, ub);
Can anyone tell me how is the set up correct because I am not even able to set it because my PCS is scalar and I do not how to define it in a function.

Best Answer

Much is unclear from your post including what your unknowns are and how they determine shortprob. However, if S is a function of the unknowns, then it is clear that fmincon is not applicable to your problem. In particular, S is a binary-valued matrix and therefore sum(S) can only assume a finite number of values. That means PCS cannot be a continuous, differentiable function of whatever the unknowns are, and therefore it cannot be minimized by a derivative-based solver like fmincon.