%% Sampling Uncertain LTI Dynamics
% When sampling an ultidyn element or an uncertain object that contains a ultidyn element, the result is always a state-space (ss) object. The property SampleStateDimension of the ultidyn class determines the state dimension of the samples. The same is true when sampling umargin objects, since these are a type of dynamic uncertainty.
% Create a 1-by-1, gain bounded ultidyn object with gain bound 4. Verify that the default state dimension for samples is 3.

del = ultidyn('del',[1 1],'Bound',4); 
del.SampleStateDimension

%
% Sample the uncertain element at 30 points. Verify that this creates a 30-by-1 ss array of 1-input, 1-output, 1-state systems.

rng(0)  % for reproducibility
delS = usample(del,30); 
size(delS)

%
% Plot the Nyquist plot of these samples and add a disk of radius 4, the gain bound of del.

nyquist(delS) 
hold on; 
theta = linspace(-pi,pi); 
plot(del.Bound*exp(sqrt(-1)*theta),'r'); 
hold off;

%
% Change SampleStateDimension to 1, and repeat entire procedure. The Nyquist plots again satisfy the gain bound, but the Nyquist plots are all circles, indicative of 1st order systems.

del.SampleStateDimension = 1; 
delS = usample(del,30);   
nyquist(delS) 
hold on; 
theta = linspace(-pi,pi); 
plot(del.Bound*exp(sqrt(-1)*theta),'r'); 
hold off;

% With SampleStateDimension = 1, all Nyquist plots touch the gain boundary at either (–1,0) or (1,0) (frequency = 0 or Inf). Higher sampling dimension yields a Nyquist curve that reaches the gain bound at more frequencies, yielding more thorough coverage.
%
% Create a umargin object using the default SampleStateDimension. The umargin block models uncertain gain and phase. The modeled variations are within bounded ranges.  For this example use a umargin block that captures relative gain variations of a factor of two in either direction, and phase variations of ±30°.

DGM = getDGM(2,30,'tight');
F = umargin('F')

% The samples of a umargin block are also state-space models.

Fs = usample(F,30);
size(Fs)

%  Plot the samples on the Nyquist plane.

nyquist(Fs)

% The Nyquist plot of any sample of F stays within the disk of uncertainty modeled by F. To confirm this bound, use plot to examine the uncertainty disk. Compare the Nyquist plot above with the right side of the following plot.

plot(F)

% For further details about the gain and phase uncertainty model, see umargin.
%
% Copyright 2015 The MathWorks, Inc.