P=ctrseq(XX,U,xgoal, ugoal, m,mc,nd,case); 
function P=ctrseq(XX,U,xgoal, ugoal, m,mc,nd,case);
% [StateTransitionVector]=CTRSEQ(X,U, XGOAL, UGOAL, m,mc,nd,case,);
% Function CTRSEQ rearranges vectors of N applied perturbations U(:,1...N)
% and vectors of the system responses X(:,1....N) into the vectors of
% the StateTransitions to the nearest steady state.
% XGOAL, UGOAL defines the goal dynamics
% mis the dimensionality of the state vector, mcis the length
% of the controlling sequence, ndis the delay between perturbation
% and its effect on the system. Usually mc=m.
% case  is the goal dynamics type
% Valery Petrov, CNLD 102996
% email: Val.Petrov@chaos.ph.utexas.edu
% Copyright (c) The University of Texas at Austin
% Calculate indices for delayed observations and perturbations
% mlength of the embedding sequence
% mclength of the controlling sequence
% nddelay between perturbation application and its effect on the system
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Direct specification of the x and u
if (case==0)
indxp=(0:m1); % X indeces for the present state
indup=(0:m+nd2); % U indeces for the present state
DX=XX; %Create differences
DDX=delsig2(DX,[indxp]);
GDX=delsig2(xgoal*ones(1,m+1),1:m);
XP=[GDX' DDX(:,size(DDX,2))'];
P=[XP'];
if(m + nd > 1)
DDU=delsig2(U,[indup]);
GDU=delsig2(ugoal*ones(1,m),1:(m1));
UP=[GDU' DDU(:,size(DDU,2))'];
P=[XP';UP'];
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Xspecified differentially, U directly
if (case==1)
indxp=(0:m1); % X indices for the past state
indup=(0:m+nd1);% U indeces for the past state
DX=diff(XX')'; %Create differences
DDX=delsig2(DX,[indxp]);
DDU=delsig2(U,[indup]);
GDX=delsig2(0*ones(size(xgoal))*ones(1,m+1),1:m);
GDU=delsig2(ugoal*ones(1,m+1),1:m);
XP=[GDX' DDX(:,size(DDX,2))'];
UP=[GDU' DDU(:,size(DDU,2))'];
P=[XP';UP'];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Direct specification of x, differential u
if (case==2)
indxp=(0:m); % X indices for the past state
indup=(0:m+nd2);% U indices for the past state
DX=XX; %Create differences
DU=diff(U')'; %Create differences
DDX=delsig2(DX,[indxp]);
GDX=delsig2(xgoal*ones(1,m+2),1:m+1);
XP=[GDX' DDX(:,size(DDX,2))'];
P=[XP'];
if (m + nd > 1)
DDU=delsig2(DU,[indup]);
GDU=delsig2(0*ones(size(ugoal))*ones(1,m),1:(m1));
UP=[GDU' DDU(:,size(DDU,2))'];
P=[P; UP'];
end
end

