function [Fopt,PMopt,phi,alpha,beta] = getOptimalDesign(f,design)
% ---

%  Copyright 2020 The MathWorks, Inc.

Fopt = Inf; PMopt = 0;  phi = []; alpha = []; beta = [];

% Extract designs that are within stability criterion range
SF = f(:,1);
minST = min(SF);
idx = (SF>=minST) & (SF<minST+0.025);
f = f(idx,:);

% Find the best design based on the design focus
if ~isempty(f)
    PF = f(:,2:5); % [RT, DR, RO, SS]
    [Fopt,idopt] = minDeTrend(PF); % find best "balanced" design
    
    if design == 1 % balanced
        % do nothing, keep Fopt, idopt from above
    elseif design == 2 % reference-tracking
        PF(:,2) = 0;
        [~,idopt] = minDeTrend(PF);
    elseif design == 3 % disturbance-rejection
        PF(:,1) = 0; PF(:,3) = 0.5*PF(:,3);
        [~,idopt] = minDeTrend(PF);
    end
    
    Fopt = (Fopt-1)/10;
    PMopt = f(idopt,6);
    phi = f(idopt,7);
    alpha = f(idopt,8);
    beta = f(idopt,9);
end
