change=zeros(5,obs+1,obs+1);
cdet=zeros(5,obs+1,1);

for z=1:5

%ddsum=zeros(obs+1,step+1);
%ddets=zeros(obs+1,step+1);

for p=1:obs+1
%%%    
dsum=zeros(step+1,1);
dets=zeros(step+1,1);
for i=1:ne
    sum=sum-[aa(tim(p),i);bb(tim(p),i);cc(tim(p),i);dd(tim(p),i);ee(tim(p),i);ff(tim(p),i);gg(tim(p),i)]*[aa(tim(p),i);bb(tim(p),i);cc(tim(p),i);dd(tim(p),i);ee(tim(p),i);ff(tim(p),i);gg(tim(p),i)]';
end;
%invsum=inv(sum);

detsum=zeros(step+1,ne+np,ne+np);
for j=1:step+1
    for i=1:ne
        detsum(j,:,:)=reshape(detsum(j,:,:),ne+np,ne+np)+[aa(j,i);bb(j,i);cc(j,i);dd(j,i);ee(j,i);ff(j,i);gg(j,i)]*[aa(j,i);bb(j,i);cc(j,i);dd(j,i);ee(j,i);ff(j,i);gg(j,i)]';
        %dsum(j)=dsum(j)+[kaa(j,i);kbb(j,i);kcc(j,i);kdd(j,i);kee(j,i);kff(j,i);kgg(j,i)]'*invsum*[aa(j,i);bb(j,i);cc(j,i);dd(j,i);ee(j,i);ff(j,i);gg(j,i)];
    end;
    dets(j)=det(sum+reshape(detsum(j,:,:),ne+np,ne+np));
end; 
%%%%%%%%%%%%%%%%%%%
%ddsum(p,:)=dsum';
%ddets(p,:)=dets';

tim(p)=find(dets==max(dets));

%mini=Inf;
%for i=1:step
%    if dsum(i)>dsum(i+1)
%        if abs(dsum(i))<mini
%            mini=abs(dsum(i));
%            ind=i;
%        end;
%    end;
%end;

%if and(min(dsum)<0,max(dsum)>0)
%    tim(p)=ind;
%end;

for i=1:ne
    sum=sum+[aa(tim(p),i);bb(tim(p),i);cc(tim(p),i);dd(tim(p),i);ee(tim(p),i);ff(tim(p),i);gg(tim(p),i)]*[aa(tim(p),i);bb(tim(p),i);cc(tim(p),i);dd(tim(p),i);ee(tim(p),i);ff(tim(p),i);gg(tim(p),i)]';
end;
cdet(z,p)=det(sum);
change(z,p,:)=tim;
%%%
end;

%for i=1:obs+1
%subplot(4,4,i);
%plot(ddets(i,:),'.');
%end;

%plot(change,'.');
sort(change(end-1,:));
%improvedcovar=sdev^2*inv(sum);
%improvedcovar(6,6)

end;

eF=sum;
det(eF) % determinant value corresponding to the optimal sampling times
temp=zeros(1,obs+1);
for i=1:obs+1
    temp(i)=change(5,obs+1,i); 
end;
sort(temp) % optimal sampling time points

