function [a,b,c,d,k,x0,da,db,dc,dd,dk,dx0]=th2ss(th)
%TH2SS	Transforms a model in THETA-format to state-space.
%	[A,B,C,D,K,X0]=TH2SS(TH)
%
%	TH: The model, defined in the THETA format (see also THETA).
%
%	A,B,C,D,K,X0 : The matrices in the state-space description 
%	
%	Xn = A X + B u + K e       X0 initial value
%	y  = C X + D u + e
% 	
%	Here Xn is the derivative of X or the next value of X, depending
%	on whether TH is denoted as continuous or discrete time.
%
%	With [A,B,C,D,K,X0,dA,dB,dC,dD,dK,dX0] = TH2SS(TH) also the
%	standard deviations of the matrix elements are computed.
%	See also TH2FF, TH2POLY, TH2TF and TH2ZP.

%	L. Ljung 10-2-90
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.4 $  $Date: 1995/05/16 09:01:05 $

if nargin < 1
   disp('Usage: [A,B,C,D,K,X0] = TH2SS(TH)')
   disp('       [A,B,C,D,K,X0,dA,dB,dC,dD,dK,dX0] = TH2SS(TH)')
   return
end

T=th(1,2);
if isthss(th), if nargout<7, eval('[a,b,c,d,k,x0]=eta2ss(th);'),
else eval('[a,b,c,d,k,x0,da,db,dc,dd,dk,dx0]=eta2ss(th);'),end,return,end
[a,b,c,d,k,x0]=th2ssaux(th);

if nargout>6,
% This assumes that TH does not use both an A-polynomial and F/D polynomials
[thpar,p]=th2par(th);
thpar=thpar+sqrt(diag(p))';
th(3,1:length(thpar))=thpar;
[a1,b1,c1,d1,k1,x01]=th2ssaux(th);
da=abs(a-a1);db=abs(b-b1);dc=abs(c-c1);dd=abs(d-d1);dk=abs(k-k1);
dx0=abs(x0-x01);end

