function cost = RP95_cost(XX);
%XX;
nX=length(XX);
global rrs_obs wave

    [aw,bbw]=water_iops_PF_TScorr(wave,15,33);
    aph=phyto_avg_abs(wave);

if nX==3,chl = XX(1); ag = XX(2);  bbps = XX(3); Scdom = 0.015; S_bbps = 1.;
    atotal = aw + aph.*chl + ag.*exp(-Scdom*(wave-443*ones(size(wave))));
    bbtotal = bbw + bbps .* ((443*ones(size(wave)) ./ wave).^S_bbps) ;
elseif nX==4,chl = XX(1); ag = XX(2); an =XX(3);  bbps = XX(4); Scdom = 0.02; Snap = 0.01; S_bbps = 1.;
    atotal = aw + aph.*chl + ag.*exp(-Scdom*(wave-443*ones(size(wave)))) + an.*exp(-Snap*(wave-443*ones(size(wave))));
    bbtotal = bbw + bbps .* ((443*ones(size(wave)) ./ wave).^S_bbps) ;
elseif nX==5,chl = XX(1); ag = XX(2); an =XX(3);  bbps = XX(4); bbpl = XX(5);Scdom = 0.02; Snap = 0.01; S_bbps = 1.; S_bbpl = 0;
    atotal = aw + aph.*chl + ag.*exp(-Scdom*(wave-443*ones(size(wave)))) + an.*exp(-Snap*(wave-443*ones(size(wave))));
    bbtotal = bbw + bbps .* ((443*ones(size(wave)) ./ wave).^S_bbps) + bbpl .* ((443*ones(size(wave)) ./ wave).^S_bbpl) ;
end

%g1 = 0.0949; g2 = 0.0794; Scdom = 0.02; Snap = 0.01; S_bbps = 1.;
g1 = 0.0949; g2 = 0.0794; 

x = bbtotal ./ (atotal + bbtotal);
    
rrs = g1 .*x + g2 .* x .* x;

cost = sum((rrs-rrs_obs).^2);
