% ***************************************************************************
% COPYRIGHT TJ Mc Avoy 1995
% FILENAME : dynamic.m
function dynamic(K)
% K = gain matrix 41x12, Glabel gives labels 
clc;
cv=zeros(1,13);
disp('*****************************************************')
disp('*               CALCULATION OF RELATIVE GAIN MATRIX                        *')
disp('*                              AND NIEDERLINSKI INDEX                                  *')
disp('*                       FOR THE TENNESSEE EASTMAN PLANT                      *')
disp('*                      USING THE ARKUN AND DOWNS METHOD                     *')
disp('*                    ****************************                          *')
disp('*                                               *')
disp('*              ========================================                  *')
disp('*                                                                                                      *')
disp('*****************************************************')
disp(' ')
disp('THE RAW GAIN MATRIX, K, IS 41X12 (MEASURED x MANIPULATED VARIABLES)')
disp('ANALYZER MEASUREMENTS AND THE AGITATOR AND RECYCLE VALVE ARE ELIMINATED')
disp('FIRST. NEXT THE INNER CASCADE MEASUREMENTS AND THE LEVEL MEASUREMENTS ')
disp('ARE SELECTED.  THE RESULT IS A 13x10 SYSTEM. THREE MEASUREMENTS ')
disp('NEED TO BE ELIMINATED FOR RGA AND NI ANALYSIS. CASCADED MANIPULATED')
disp('VARIABLES ARE USED FOR LEVEL CONTROL.  BE CAREFUL TO USE THE')
disp('THE NUMBERS LISTED HERE (1-13) FOR ELIMINATING VARIABLES ')
disp(' ')
G=K(1:22,:);% eliminate analyzers
G=[G(:,1:4),G(:,6:11)];%eliminate recycle and agitator
G=[G(1:4,:);G(8,:);G(10,:);G(12,:);G(14:15,:);G(17,:);G(19,:);G(21:22,:)];
[Gl1s,Gl2s,Gl3s]=labl;% labl sets up labels
disp('HIT RETURN TO CONTINUE')
Glabel=[Gl1s,Gl2s,Gl3s];
Gsave=G;
pause
LBSAVE=Glabel;
disp(' ')
disp('BEGIN RGA ANALYSIS WHICH REQUIRES A SQUARE SYSTEM')
lloop=1;
while lloop==1
	cv=zeros(1,13);
	LBL=LBSAVE;
	Gl1=Gl1s;
	Gl2=Gl2s;
	Gl3=Gl3s;
	G=Gsave;
	disp('')
	disp(LBL)
	disp(' ')
	disp('INPUT 3 MEASURED VARIABLES TO BE ELIMINATED')
	loop=1;
	i=0;
	while loop==1
		disp(' ')
		i=i+1;
		if i>=4;%finished
			break;
		else
		end
		c=input('');
	 	cv(i)=round(c); % ROUNDS OFF SELECTION
	 	if cv(i)<0|cv(i)>13
	 		disp(sprintf('INCORRECT CHOICE. No. NEEDS TO BE BETWEEN 1 AND 13'))
			disp('PLEASE INPUT AGAIN')
	 		disp('')
			i=i-1;
		else
		end
	end
	cv=-sort(-cv);
	l1=zeros(1,3);
for j=1:3;% find cascades used for level control
	if cv(j)==3
		l1(j)=2;
		Gl3(3,:)='      E FLOW SP      ';
	elseif cv(j)==8
		l1(j)=6;
		Gl3(7,:)='     SEP EXIT SP     ';
	elseif cv(j)==10
		l1(j)=7;
		Gl3(8,:)='      PRODUCT SP     ';
	elseif cv(j)==11
		l1(j)=8;
		Gl3(9,:)='       STEAM SP      ';
	elseif cv(j)==12
		l1(j)=9;
		Gl3(10,:)='    REACT TEMP SP    ';
	elseif cv(j)==13
		l1(j)=10;
		Gl3(11,:)='    CONDS TEMP SP    ';
	else
	end
end
l1=-sort(-l1);
%only allow 3 cascaded mv's to be used for level control
if l1(1,3)~=0
	for jk=1:3
	cv(jk);
			for mm=1:13
				kk(mm,:)=G(mm,:)-G(cv(jk),:)*G(mm,l1(jk))/G(cv(jk),l1(jk));
			end
			kk(:,l1(jk))=G(:,l1(jk))/G(cv(jk),l1(jk));
			G=kk;
	end
else
end
	
%remove rows here and update labels
	for j=1:13
		if cv(j)~=0
			[M,N]=size(G);
			% change labels
			G=rowremv(G,cv(j),M);
			Gl2=rowremv(Gl2,cv(j)+1,M+1);
		else
		end
	end
	Gl1=Gl1(1:M,:);
[kk1,kk2]=size(G);
Gl3=Gl3(1:kk2+1,:);
% kk1, kk2 give number of controlled and manipulated variables remaining

	if kk2~=kk1
		lloop1=0;
		disp('SYSTEM MUST BE SQUARE FOR RGA ANALYSIS. PLEASE START OVER')
	else
		disp(' ')
		LBL=[Gl1,Gl2,Gl3];
		disp(LBL)
		disp(' ')
		RGA=G.*(inv(G)')
		disp('')
		disp('DO YOU WANT TO CALCULATE THE NIEDERLINSKI INDEX?')
		i=input('INPUT y (YES) OR n (NO):  ','s');
		disp(' ')
		if i=='y'
			nloop=1;
			while nloop==1
				disp('THE PAIRING HAS TO BE INPUT. GIVE ROW PAIRED WITH COLUMN i')
				disp('')
				check=0;
				pair=zeros(1,kk1);
				for j=1:kk1
					pair(j)=round(input((sprintf('INPUT ROW FOR COLUMN %6.0f    ',j))));
					check=check+j;
				end
				if sum(pair)~=check
					disp('CANNOT USE SAME COLUMN MORE THAN ONCE. TRY AGAIN')
					disp('')
				else
					nloop=0;
				end
			end
			NI=nieder(G,pair)
		else
		end
		disp('DO YOU WANT TO CARRY OUT MORE CALCULATIONS?')
		i=input('INPUT y (YES) OR n (NO):  ','s');
			if i=='y'
			else
				break
			end
		end
		i=0;
end


		




