%% Create Uncertain Real Parameters
% This example shows how to create uncertain real parameters, modify properties such as range of uncertainty, and sample uncertain parameters.
% Create an uncertain real parameter, nominal value 3, with default values for all unspecified properties (including plus/minus variability of 1).

a = ureal('a',3)

%
% View the properties and their values, and note that the Range and Percentage descriptions of variability are automatically maintained.

get(a)

%
% Create an uncertain real parameter, nominal value 2, with 20% variability. Again, view the properties, and note that the Range and PlusMinus descriptions of variability are automatically maintained.

b = ureal('b',2,'Percentage',20) 
get(b)

%
% Change the range of the parameter. All descriptions of variability are automatically updated, while the nominal value remains fixed. Although the change in variability was accomplished by specifying the Range, the Mode is unaffected, and remains Percentage.

b.Range = [1.9 2.3];
get(b)

%
% As mentioned, the Mode property signifies what aspect of the uncertainty remains unchanged when NominalValue is modified. Hence, if a real parameter is in Percentage mode, then the Range and PlusMinus properties are determined from the Percentage property and NominalValue. Changing NominalValue preserves the Percentage property, and automatically updates the Range and PlusMinus properties.

b.NominalValue = 2.2;
get(b)

%
% Create an uncertain parameter with an asymmetric variation about its nominal value. Examine the properties to confirm the asymmetric range.

c = ureal('c',-5,'Percentage',[-20 30]); 
get(c)

%
% Create an uncertain parameter, specifying variability with Percentage, but force the Mode to be Range.

d = ureal('d',-1,'Mode','Range','Percentage',[-40 60]); 
get(d)

%
% Finally, create an uncertain real parameter, and set the AutoSimplify property to 'full'.

e = ureal('e',10,'PlusMinus',[-23],'Mode','Percentage','AutoSimplify','Full') 
get(e)

%
% Specifying conflicting values for Range/Percentage/PlusMinus when creating a ureal element does not result in an error. In this case, the last specified property is used. This last occurrence also determines the Mode, unless Mode is explicitly specified, in which case that is used, regardless of the property/value pairs ordering.

f = ureal('f',3,'PlusMinus',[-2 1],'Percentage',40) 
g = ureal('g',2,'PlusMinus',[-2 1],'Mode','Range','Percentage',40) 
g.Mode

%
% Create an uncertain real parameter, use usample to generate 1000 instances (resulting in a 1-by-1-by-1000 array), reshape the array, and plot a histogram, with 20 bins (within the range of 2 to 4).

h = ureal('h',3); 
hsample = usample(h,1000); 
hist(reshape(hsample,[1000 1]),20);

%
% Make the range nonsymmetric about the nominal value, and repeat the sampling, and histogram plot (with 40 bins over the range of 2-to-6)

h.Range = [2 6]; 
hsample = usample(h,1000); 
hist(reshape(hsample,[1000 1]),40);

%
% The distribution is still uniform. The distribution used by usample is uniform in the actual value of the uncertain real parameter. However, highly skewed ranges can lead to poor numeric conditioning and poor results. Therefore, for meaningful results, avoid highly skewed ranges where the nominal value is orders of magnitude closer to one end of the range than to the other.
% There is no notion of an empty ureal (or any other uncertain element, for that matter).  ureal, by itself, creates an element named 'UNNAMED', with default property values.
% Copyright 2015 The MathWorks, Inc.