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

    [aw,bbw]=water_iops_PF_TScorr(wave,15,33);
    aph=phyto_species_abs(wave);%dinoflagellate diatom chlorophyte cryptomonad

chl1 = XX(1); chl2 = XX(2); chl3 = XX(3); chl4 = XX(4); ag = XX(5); 
aphi= aph(:,1)*chl1 + aph(:,2)*chl2 + aph(:,3)*chl3 + aph(:,4)*chl4;

if nX==3,  bbps = XX(6); Scdom = 0.015; S_bbps = 1.;
    atotal = aw + aphi + ag.*exp(-Scdom*(wave-443*ones(size(wave))));
    bbtotal = bbw + bbps .* ((443*ones(size(wave)) ./ wave).^S_bbps) ;
elseif nX==4,  an =XX(6);  bbps = XX(7); Scdom = 0.02; Snap = 0.01; S_bbps = 1.;
    atotal = aw + aphi + 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,an =XX(6);  bbps = XX(7); bbpl = XX(8); Scdom = 0.02; Snap = 0.01; S_bbps = 1.; S_bbpl = 0;
    atotal = aw + aphi + 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);
