%
% Function to integrate auxillart variable v 
%    dx/dt = Ax + Bu
%      y   = Cx + Du
% Input variables:
%    A   - State-space A matrix.
%    B   - State-space input matrix.
%    C   - State space output matrix.
%    D   - state space input matrix.
%    u   - input, ie u(t).
%    x0  - initial state.
%

%
% Written by: Kjetil Havre,  5/2-1996.
%
function [x,y] = lintf(A,B,C,D,u,x0)
      
   t  = getiv(u);
   nt = max(size(t));
   tf = t(nt);   t0 = t(1);
   
   for i = 1:nt-1
      tv = [t(i) t(i+1)];
      ui  = vunpck(xtract(u,t(i))).';
      ui1 = vunpck(xtract(u,t(i+1))).';
      uv  = [ui; ui1];
      [yh,xh] = lsim(A,B,C,D,uv,tv,x0);
      if i == 1
        Xh = [xh(1,:)];
      else
        Xh = [Xh; xh(1,:).'];
      end
      x0 = xh(2,:).';
   end
   Xh = [Xh; xh(2,:).'];
   
   x = vpck(Xh,t);
   y = madd( mmult(C,x), mmult(D,u) );
end

