function Tprime=reguleringsim(t,T)
global mc mh Cp Cpc Q1 Q2 Q3 Npoint Tf cf dm1 dm2 dm3 dHrx u conc p  

% comment: For simplicity we use rx((T(i),c(i-1)) rather than rx((T(i),c(i))

%section1.--Reconstruction of unstable ammonia reactor------%
%if t<1200
%   p=170;
%elseif (t>=1200 & t<=7200)
%  p=150;
%else t>7200
%  p=200;
%end;
%-----------------------------------------------------------%

%section2.--New steady-state--------------------------------%
if t>1200
   p=150;
else
   p=200;
end;
%-----------------------------------------------------------%

%section3.--P-controller------------------------------------% 
Kc=3;
Q1=16.11+Kc*(T(1)-369.4); % positiv feedback, e=y-r
mc=mh-(Q1+Q2+Q3);
%-----------------------------------------------------------% 

m1=mc+Q1;
m2=m1+Q2;
m3=m2+Q3;

% Conc. in bed 1:
c(1)=cf+dm1*rx(T(1),cf)/m1;
for i=2:10
  c(i)=c(i-1)+dm1*rx(T(i),c(i-1))/m1;
end
% Quench 2: 
cin2=m1/m2*c(10)+Q2/m2*cf;
% Conc. in bed 2
c(11)=cin2+dm2*rx(T(11),cin2)/m2;
for i=12:20
  c(i)=c(i-1)+dm2*rx(T(i),c(i-1))/m2;
end
% Quench 3:
cin3=m2/m3*c(20)+Q3/m3*cf;
% Conc. in bed 3
c(21)=cin3+dm3*rx(T(21),cin3)/m3;
for i=22:30
    c(i)=c(i-1)+dm3*rx(T(i),c(i-1))/m3;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Quench 1:
Ti=hx(T(30),Tf); Tin1=mc/m1*Ti+Q1/m1*Tf;
% dTdt in bed 1:
dTdt(1)=(m1*Cp*(Tin1-T(1))+dm1*rx(T(1),cf)*dHrx)/(dm1*Cpc);
for i=2:10
  dTdt(i)=(m1*Cp*(T(i-1)-T(i))+dm1*rx(T(i),c(i-1))*dHrx)/(dm1*Cpc);
end
% Quench 2:
Tin2=m1/m2*T(10)+Q2/m2*Tf;
% dTdt in bed 2:
dTdt(11)=(m2*Cp*(Tin2-T(11))+dm2*rx(T(11),cin2)*dHrx)/(dm2*Cpc);
for i=12:20
  dTdt(i)=(m2*Cp*(T(i-1)-T(i))+dm2*rx(T(i),c(i-1))*dHrx)/(dm2*Cpc);
end
% Quench 3:
Tin3=m2/m3*T(20)+Q3/m3*Tf;
% dTdt in bed 3:
dTdt(21)=(m3*Cp*(Tin3-T(21))+dm3*rx(T(21),cin3)*dHrx)/(dm3*Cpc);
for i=22:30
  dTdt(i)=(m3*Cp*(T(i-1)-T(i))+dm3*rx(T(i),c(i-1))*dHrx)/(dm3*Cpc);
end
Tprime=dTdt;
conc=c; 		
Tprime=Tprime';





