%% Set MaxNumAssets Constraint for a Portfolio Object
% Set the maximum cardinality constraint for a three-asset portfolio for which you have the mean and covariance values of the asset returns.

AssetMean = [ 0.0101110; 0.0043532; 0.0137058 ];
AssetCovar = [ 0.00324625 0.00022983 0.00420395;
               0.00022983 0.00049937 0.00019247;
               0.00420395 0.00019247 0.00764097 ]; 
           
p = Portfolio('AssetMean', AssetMean, 'AssetCovar', AssetCovar);
p = setDefaultConstraints(p); 

% When working with a Portfolio object, the setMinMaxNumAssets function enables you to set up the limits on the number of assets invested. Use setMinMaxNumAssets to limit the total number of allocated assets to no more than two.

 p = setMinMaxNumAssets(p, [], 2);

% Use estimateFrontierByReturn to estimate optimal portfolios with targeted portfolio returns.

pwgt = estimateFrontierByReturn(p,[ 0.008, 0.01 ])

% Copyright 2018 The MathWorks, Inc.