% copyright T. Mc Avoy 1999
% linear ss results for TE plant
load scdistgn
load steady
us=u;
clf
K=G0;
K(12,:)=Gl(12,:);
K(15,:)=Gl(15,:);

while 1
	id=input('\n Input IDV Number from 2 to 7\n\n');
	if id>=2 & id<=7
		break
	else
		disp('Please Input a Number Between 1 and 7')
		disp(' ')
	end
end
while 1
if id==7
	disp('Input 1 If You Want the C Feed Flow Controller Closed')
	disp(' ')
	ic=input('Input 0 If You Want the C Feed Flow Controller Open\n');
	if ic==0 | ic==1
		if ic==0
			K=cascaden(K);% Don't close C Feed Controller
		else
			K=cascade(K);% Close C Feed Controller
		end
		break
	else
		disp('Please Input a Number Between 0 and 1')
		disp(' ')
	end
else
	K=cascaden(K);
	break
end	
end

while 1
	ii=input('\n Input 1 to Control Reactor Pressure\n Input 2 to Control Reactor Temperature\n');
	if ii>=1 & ii<=2
		break
	else
		disp('Please Input a Number Between 1 and 2')
		disp(' ')
	end
end

while 1
	jj=input('\n Input 1 to Manipulate E Feed\n Input 2 to Manipulate Condenser Cooling Water\n');
	if jj>=1 & jj<=2
		break
	else
		disp('Please Input a Number Between 1 and 2')
		disp(' ')
	end
end

if ii==1&jj==1;% control reactor pressure, manipulate E
	if id~=6
		cv=[7 8 12 15 23 30];
		mv=[2:3 6:8 10];
	else
		cv=[7 8 12 15 30];% IDV(6) eliminates A as a manipulated variable
		mv=[2 6:8 10];	
	end
elseif  ii==2&jj==1;% control reactor temperature, manipulate E
	if id~=6
		cv=[9 8 12 15 23 30];
		mv=[2:3 6:8 10];
	else
		cv=[9 8 12 15 30];% IDV(6) eliminates A as a manipulated variable. A feed controller is
		% not closed
		mv=[2 6:8 10];	
	end
elseif  ii==1&jj==2;% control reactor pressure, manipulate condenser
	if id~=6
		cv=[7 8 12 15 23 30];
		mv=[3 6:8 10:11];
	else
		cv=[7 8 12 15 30];% IDV(6) eliminates A as a manipulated variable. A feed controller is
		% not closed
		mv=[6:8 10:11];	
	end
else  ;% control reactor temperature, manipulate condenser
	if id~=6
		cv=[9 8 12 15 23 30];
		mv=[3 6:8 10:11];
	else
		cv=[9 8 12 15 30];% IDV(6) eliminates A as a manipulated variable. A feed controller is
		% not closed
		mv=[6:8 10:11];	
	end
end	
for i=1:11
	siz=.1*(i-1);
	[ss]=sscal(cv,mv,K,id,u,zs,siz);
	res(1,i)=ss(17);
	res(2,i)=ss(41);
	if ii==1
		res(3,i)=ss(9);
		lbl='Reactor Temperature';
	else
		res(3,i)=ss(7);
		lbl='Reactor Pressure';
	end
end

subplot(3,1,1),plot([0:10]*.1,res(1,:),'--')
xlabel('disturbance size')
if id==7
	if ic==0
		legend('Disturbance Number 7 C Feed Controller Off')
	else
		legend('Disturbance Number 7 C Feed Controller On')
	end
else
	legend(['Disturbance Number  ',num2str(id)])
end
ylabel('Product')
if jj==1
	title('Manipulated A,E,Pur,Sep,Prod,RCWT')
else
	title('Manipulated A,Pur,Sep,Prod,RCWT,CCWT')
end
hold on
subplot(3,1,2),plot([0:10]*.1,res(2,:),'--')
xlabel('disturbance size')
ylabel('Product %H')
hold on
subplot(3,1,3),plot([0:10]*.1,res(3,:),'--')
xlabel('disturbance size')
ylabel(lbl)

	[ss,uu]=sscal(cv,mv,K,id,u,zs,1);
	
disp('Hit any key to continue')
pause
%clf
disp(' ')
disp(' ')
disp(' ')

disp(['Change in Reactor Pressure (kPa)  ',num2str([ss(7)-zs(7)])])

disp(['Change in Reactor Temperature (deg. C)  ',num2str([ss(9)-zs(9)])])

disp(['Change inProduct Flow (m3/hr)  ',num2str([ss(17)-zs(17)])])

disp(['Change in %H in Product  ',num2str([ss(41)-zs(41)])])
