MATLAB: Row dimension of Aeq is inconsistent with length of beq.

fmincon

x = [0.3,.5,.6]
n = 3
lb = zeros(length(x),1)
ub = ones(length(x),1)
Aeq = [zeros(1,n); ones(1,n)]
beq = 1
x = fmincon(@(x) myfunct(x),x,[],[],Aeq,beq,lb,ub)
x
this is myfunction below
function [z] = myfunct(x)
n= length(x)/2;
y = 0:1:n;
p_y_x = zeros(n,n+1)
h_y_x = []
for i=1:n
for k =1:length(y)
p_y_x(i,k) = nchoosek(n,y(k)).*(x(i).^y(k)).*(1-x(i).^(n-y(k)))
end
index = find(p_y_x(i,:))
p_y_x(i,index)
h_y_x = [h_y_x -p_y_x(i,index)*log(p_y_x(i,index))']
end
sec_term = x(n+1:2*n)*h_y_x'
q_y = p_y_x(i,index).*x(n+1:2*n)
first_term = q_y*log(q_y)'
z = first_term - sec_term
end

Best Answer

Aeq = [zeros(1,n); ones(1,n)]
creates Aeq with two rows. The first of them expresses that sum of 0 times x is equal to something, namely beq(1) which is 1. 0 times something is always 0 unless the something is infinite, and sum of zeros is 0, so you are expressing that 0 must equal 1.
Then your second row of Aeq expresses that sum of one times x equals something, namely beq(2). But there is no beq(2) so you get the message.