function MarkowitzWeight = fsolve_markowitz_NAGnew()
%Solve the Markowitz problem by finding an optimal portfolio with minimum variance for a targeted return
%Authors: Biao Guo (http://www.mathfinance.cn/) and Mike Croucher
%(www.walkingrandomly.com)
global r targetR sumrCOV;
%seed random generator to ensure same results every time for benchmark purposes
RandStream.setDefaultStream (RandStream('mt19937ar','seed',1));

% simulate 500 stocks return
SimR = randn(1000,500);
r = mean(SimR)'; % use mean return as expected return
targetR = 0.02;
sumrCOV = sum(cov(SimR)); % covariance matrix
NumAsset = length(r);

startX = [1/NumAsset*ones(1,NumAsset), 1, 1];
x = c05nb('fsolve_obj_NAGnew',startX); 
MarkowitzWeight = x(1:NumAsset);
end
