function val = FitGEVRightTail(beta, low, up, Quant_low, Quant_up, f_low, f_up)

Mean = beta(1);
sigma = beta(2);
phi = beta(3);

w1 = 1e3;
w2 = 1e7;
w3 = 1e7;

% val = w1*(CDF_GEV(Quant_low,Mean,sigma,phi) - low)^2 + ...
%     w2*(PDF_GEV(Quant_low,Mean,sigma,phi) - f_low)^2 + ...
%     w3*(PDF_GEV(Quant_up,Mean,sigma,phi) - f_up)^2;

val = w1*(gevcdf(Quant_low,phi,sigma,Mean) - low)^2 + ...
    w2*(gevpdf(Quant_low,phi,sigma,Mean) - f_low)^2 + ...
    w3*(gevpdf(Quant_up,phi,sigma,Mean) - f_up)^2;


if sigma < 0 | isreal(val) == 0
    val = 1e100;
end

