%
% Example 3.5: DO Controller
%
% Bob Newell, March 1996
%
%-----------------------------------------------------------
%
function y = DO_Controller_Outputs( t, x )
global p
%
% x = [ 1=V 2=Ss 3=So 4=z ];
% y = [ 1=v_in 2=Ss_in 3=So_in, 
%       4=v_out 5=Ss_out 6=So_out,
%       7=v_a ];
% p = [ 1=Ka 2=So_sat
%       3=yd 4=uo 5=Kc 6=taui ];
%
%-----------------------------------------------------------
  [ n, m ] = size( x );
  if m == 1,
    x = x';
    n = 1;
  end;
  y = zeros( n, 2 );
%
  for i = 1:n,
    % influent conditions
    y(i,1) = 5;
    y(i,2) = 200;
    % influent disturbance and 
    %   setpoint change
    if t(i) < 0.125,
      p(3) = 3;
      y(i,3) = 0.2;
    elseif t(i) < 0.5,
      p(3) = 3;
      y(i,3) = 1.0;
    else,
      p(3) = 5;
      y(i,3) = 1.0;
    end; 
    % constant volume
    y(i,4) = y(i,1);
    % well mixed
    y(i,5) = x(i,2);
    y(i,6) = x(i,3);
    % PI controller
    y(i,7) = p(4) + p(5) * ...
         ( p(3) - y(i,6) + x(i,4) / p(6) );
  end;
%-----------------------------------------------------------
% the end!   
