function [Tnew,mu2]=optSep(mu,s,T,tgt)
numb=max(size(mu))-1;
mu2=mu;
% do loop
for j=1:numb
    a=j;
    b=j+1;
% do calc pair av prob %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i=1:20
        avp(i)=avPNumS(mu2,s,T,a,b);
        if i==1
            if avp(i) < tgt;
                dif(i) = - (sqrt(s(b))+sqrt(s(b)))*(tgt-avp(i))/5;
            else
                dif(i) = + (sqrt(s(b))+sqrt(s(b)))*(tgt-avp(i))/5;
            end
        else
            if abs(avp(i) - avp(i-1)) < 0.002
                dif(i)=dif(i-1);
                %i
                break;
            else
                dif(i) = (tgt-avp(i))/(avp(i)-avp(i-1))*dif(i-1);
            end
        end
        mu2(b)=mu2(b)+dif(i);
    end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% move everything down
    diffe=mu(j+1)-mu2(j+1);
    for k=j+2:numb+1
        mu2(k)=mu(k)-diffe;
    end
%
end
% Inverse temp function
Tnew(1)=T(1);
for j=2:numb+1
    idx = numb+1;
    for k=2:numb+1
        if mu2(j) < mu(k)
            idx=k;
            break;
        end
    end
    Tnew(j)=floor(T(idx-1)+(mu2(j)-mu(idx-1))*(T(idx)-T(idx-1))/(mu(idx)-mu(idx-1)));
end
%
