function scaledF = getF(dcgainC0,dcgainC,PMreq,nu,magOL,Tmag,STmag,Lroll0,Lroll,PM,phi,alpha,beta)
%GETF returns the merit function value as a weighted sum of the following cost elements:

%  Copyright 2020 The MathWorks, Inc.

%=================================================================================================================(Stability)
STmax = max(STmag);
PMmax = 2*atan2(1,STmax);
X = PMmax/PMreq;
ST = max([1 1-4*0.025*log(X)]);
%=======================================================================================================(Overshoot/Bandwidth)
Tmax = max(Tmag/Tmag(1));
OS = 100*exp(-pi/(Tmax+sqrt(Tmax^2-1)));
minOS = 100*exp(-pi);
BW = max(80*log10(Lroll0./magOL));
RT = max(OS,BW);
%=====================================================================================================(Disturbance-rejection)
DR = max(0,log10(dcgainC0/dcgainC));
%==================================================================================================================(Roll-off)
RO = max(0,max(20*log10(magOL./Lroll)));
%==============================================================================================================(Steady-state)
magOLSS = magOL(nu<=0.001);
if isempty(magOLSS)
    magOLSS = magOL(1);
end
SS = max(40-20*log10(magOLSS(end)),1);
%===============================================================================================================(Scaled cost)
% f = [ST, RT, DR, RO, SS, PM, alpha, beta, phi];
if RT < 10
    scaledF = [...
        ST,...
        [RT 1]*([minOS 1;10 1]\[1;1.10]),...
        [DR 1]*([0 1;1 1]\[1;2]),...
        [RO 1]*([0 1;10 1]\[1;2]),...
        [SS 1]*([0 1;10 1]\[1;2]),...
        PM, phi, alpha, beta];
else
    scaledF = [...
        ST,...
        [RT 1]*([10 1;20 1]\[1.10;2]),...
        [DR 1]*([0 1;1 1]\[1;2]),...
        [RO 1]*([0 1;10 1]\[1;2]),...
        [SS 1]*([0 1;10 1]\[1;2]),...
        PM, phi, alpha, beta];
end