I do not fully agree to the answer of my predecessor or want to make some enhancements:
- $A$ needs to contain negative values because the linear constraints are defined as $Ax \leq b$.
You can forbid short selling, when you extend (add) a linear constraint:
A = [A;-eye(nAssets)];
b = [b;zeros(1,nAssets)];
- I was too stupid to annualize the returns and covariance.
The following code helped me to solve the Markowitz model:
data = ; %your data as column based price matrix
%data = xlsread('your_excel_sheet.xlsx');
nAssets = size(data, 2);
rets = data(2:end, :)./data(1:end-1,:)-1;
%annualize the returns and covariance
mu = 250 * mean(rets);
sigma = 250 * cov(rets);
%formulate the problem/optimization
r_target = 0.10; %r_target is the required return
f = zeros(nAssets, 1); %there is no constant
A = [-mu; -eye(nAssets)]; %besides the returns we forbid short selling
b = [-r_target; zeros(nAssets, 1)]; % required return and weights greater/eqauls 0
Aeq = ones(1, nAssets); %All weights should sum up...
beq = 1; %... to one (1)
%solve the optimization
w = quadprog(sigma,f,A,b,Aeq,beq);
%print the solution
fprintf(2, 'Risk: %.3f%%\n', sqrt(w'*sigma*w)*100);
fprintf(2, 'Ret: %.3f%%\n', w'*mu'*100);
Please let me know if you wish a more detailed description or some example data.