function [y,t,x] = initial(varargin)
%INITIAL  Initial condition response of state-space models.
%
%   INITIAL(SYS,X0) plots the undriven response of the state-space model SYS 
%   (created with SS) with initial condition X0 on the states. This response 
%   is characterized by the equations
%                        .
%     Continuous time:   x = A x ,  y = C x ,  x(0) = x0 
%
%     Discrete time:  x[k+1] = A x[k],  y[k] = C x[k],  x[0] = x0 .
%
%   The time range and number of points are chosen automatically.  
%
%   INITIAL(SYS,X0,TFINAL) simulates the time response from t=0 to the final 
%   time t=TFINAL (expressed in the time units specified in SYS.TimeUnit).  
%   For discrete-time models with unspecified sample time, TFINAL is 
%   interpreted as the number of sampling periods.
%
%   INITIAL(SYS,X0,T) uses the time vector T for simulation (expressed in the 
%   time units of SYS). For discrete-time models, T should be of the form 
%   Ti:Ts:Tf where Ts is the sample time. For continuous-time models, T 
%   should be of the form Ti:dt:Tf where dt is the sampling period for the 
%   discrete approximation of SYS. 
%
%   INITIAL(SYS1,SYS2,...,X0,T) plots the response of several systems
%   SYS1,SYS2,... on a single plot. The time vector T is optional. You can 
%   also specify a color, line style, and marker for each system, for 
%   example:
%      initial(sys1,'r',sys2,'y--',sys3,'gx',x0).
%
%   When invoked with left hand arguments,
%      [Y,T,X] = INITIAL(SYS,X0)
%   returns the output response Y, the time vector T used for simulation, 
%   and the state trajectories X. No plot is drawn on the screen. The
%   matrix Y has LENGTH(T) rows and as many columns as outputs in SYS.
%   Similarly, X has LENGTH(T) rows and as many columns as states. The 
%   time vector T is expressed in the time units of SYS. 
%	
%   See also INITIALPLOT, IMPULSE, STEP, LSIM, LTIVIEW, DYNAMICSYSTEM.

%	Clay M. Thompson  7-6-90
%	Revised: ACWG 6-21-92
%	Revised: PG 4-25-96
%       Revised: A. DiVergilio, 6-16-00
%       Revised: B. Eryilmaz, 10-02-01
%   Copyright 1986-2011 The MathWorks, Inc.
ni = nargin;
no = nargout;

% Simulate the initial response
if no
   try
      % Call with output arguments. Parse input list
      [sysList,Extras] = DynamicSystem.parseRespFcnInputs(varargin);
      [sysList,t,x0] = DynamicSystem.checkInitialInputs(sysList,Extras);
      sys = sysList(1).System;
      if (numel(sysList)>1 || numsys(sys)~=1)
         ctrlMsgUtils.error('Control:analysis:RequiresSingleModelWithOutputArgs','initial');
      end
      % Compute response
      if no<3
         % No state vector requested
         [y,t,focus] = timeresp_(sys,'initial',t,x0);
         x = [];
      else
         [y,t,focus,x] = timeresp_(sys,'initial',t,x0);
      end
      % Clip to FOCUS
      [t,y,x] = ltipack.roundTimeFocus(focus,t,y,x);
   catch E
      throw(E)
   end

else
   % Initial response plot
   ArgNames = cell(ni,1);
   for ct=1:ni
      ArgNames(ct) = {inputname(ct)};
   end
   varargin = argname2sysname(varargin,ArgNames);
   try
      initialplot(varargin{:});
   catch E
      throw(E)
   end
end
