function idsimsd(u,th,n,noise,ky)
%IDSIMSD Illustrates the uncertainty in simulated model responses.
%	IDSIMSD(U,TH)
%
%	U is a column vector (matrix) containing the input(s).
%	TH is a model given in the THETA format (See also THETA).
%
%	10 random models are created, consistent with the covariance informa-
%	tion in TH, and the responses of each of these models to U are plotted
%	in the same diagram.
%	
%	The number 10 can be changed to N by IDSIMSD(U,TH,N).
%
%	With IDSIMSD(U,TH,N,'noise',KY), additive noise (e) is added to the 
%	simulation in accordance with the noise model of TH.
%	KY denotes the output numbers to be plotted (default all).
%
%	See also IDSIM.

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

if nargin < 2
   disp('Usage: IDSIMSD(INPUT,MODEL)')
   disp('       IDISMSD(INPUT,MODEL,ADD_NOISE,OUTPUTS)')
   disp('       ADD_NOISE one of ''no_noise'', ''noise''.')
   return
end

if isthss(th),ny=th(1,4);else ny=1;end
if nargin<5,ky=[];end
if nargin <4,noise=[];end
if nargin<3,n=[];end,
if isempty(ky),ky=1:ny;end
if isempty(noise),noise='nonoise';end
if isempty(n),n=10;end

nu=th(1,3);Tsamp=abs(th(1,2));
[Nc,d]=getncap(th);
[N,nz]=size(u);
if nu~=nz, error('The input matrix U has not the correct number of columns!'),
return,end
[par,P,lam]=th2par(th);
if size(P)==0; disp('No covariance information given in TH'),P=zeros(d,d);end
P=chol(P);
u1=u;
for kk=ky
if noise(3)=='i', u1=[u randn(N,ny)];end %corr 9007
yh=idsim(u1,th);yh=yh(:,kk);
ndu=length(yh);y1=max(yh);y2=min(yh);
y12=y1-y2;y1=y1+0.2*y12;y2=y2-0.2*y12;
plot([1:ndu]*Tsamp,yh),axis([Tsamp ndu*Tsamp y2 y1]);hold on;
title(['Output number ',int2str(kk)])
u1=u;
for k=1:n-1
	th1=th; th1(3,1:d)=par+randn(1,d)*P;
	if noise(3)=='i', u1=[u randn(N,ny)];end
	yh=idsim(u1,th1);
	plot([1:ndu]*Tsamp,yh(:,kk))
end
hold off
if kk<ky(length(ky)),pause,end
end
set(gcf,'NextPlot','replace');

