1 %>@ingroup graphicsapi maths
3 %>@brief error ellipse,
"ellipse walls" version
6 %> ERROR_ELLIPSE(C22) - Given a 2x2 covariance matrix, plot the
7 %> associated error ellipse, at the origin. It returns a graphics handle
8 %> of the ellipse that was drawn.
10 %> ERROR_ELLIPSE(C33) - Given a 3x3 covariance matrix, plot the
11 %> associated error ellipsoid, at the origin,
as well
as its projections
12 %> onto the three axes. Returns a vector of 4 graphics handles,
for the
13 %> three ellipses (in the X-Y, Y-Z, and Z-X planes, respectively) and for
16 %> ERROR_ELLIPSE(C,MU) - Plot the ellipse, or ellipsoid, centered at MU,
17 %> a vector whose length should match that of C (which is 2x2 or 3x3).
19 %> ERROR_ELLIPSE(...,'Property1',Value1,'Name2',Value2,...) sets the
20 %> values of specified properties, including:
21 %> 'C' - Alternate method of specifying the covariance matrix
22 %> 'mu' - Alternate method of specifying the ellipse (-oid) center
23 %> 'conf' - A value betwen 0 and 1 specifying the confidence interval.
24 %> the default is 0.5 which is the 50%> error ellipse.
25 %> 'scale' - Allow the plot the be
scaled to difference units.
26 %> 'style' - A plotting style used to format ellipses.
27 %> 'clip' - specifies a clipping ruleRadii. Portions of the ellipse, -oid,
28 %> outside the ruleRadii will not be shown.
30 %> NOTES: C must be positive definite for this function to work properly.
35 default_properties = struct(...
36 'C', [], ... % The covaraince matrix (required)
37 'mu', [], ... % Center of ellipse (optional)
38 'conf', 0.5, ... % Percent confidence/100
39 'scale', 1, ... % Scale factor, e.g. 1e-3 to plot m
as km
40 'style', '', ... % Plot style
42 'xyz', [0, 0, 0]); % Clipping ruleRadii
44 if length(varargin) >= 1 & isnumeric(varargin{1})
45 default_properties.C = varargin{1};
49 if length(varargin) >= 1 & isnumeric(varargin{1})
50 default_properties.mu = varargin{1};
54 if length(varargin) >= 1 & isnumeric(varargin{1})
55 default_properties.conf = varargin{1};
59 if length(varargin) >= 1 & isnumeric(varargin{1})
60 default_properties.scale = varargin{1};
64 if length(varargin) >= 1 & ~ischar(varargin{1})
65 irerror(
'Invalid parameter/value pair arguments.')
68 prop =
getopt(default_properties, varargin{:});
72 mu = zeros(length(C),1);
81 if conf <= 0 | conf >= 1
82 irerror('conf parameter must be in range 0 to 1, exclusive')
86 if r ~= c | (r ~= 2 & r ~= 3)
87 irerror(['Don''t know what to do with ',num2str(r),'x',num2str(c),' matrix'])
93 % Compute quantile for the desired percentile
94 k = sqrt(
qchisq(conf,r)); % r is the number of dimensions (degrees of freedom)
96 hold_state = get(gca,'nextplot');
101 % Make the matrix has positive eigenvalues - else it's not a valid covariance matrix!
103 irerror('The covariance matrix must be positive definite (it has non-positive eigenvalues)')
106 % C is 3x3; extract the 2x2 matricies, and plot the associated error
107 % ellipses. They are drawn in space, around the ellipsoid; it may be
108 % preferable to draw them on the axes.
111 Czx = C([3 1],[3 1]);
113 % zz = get(gca, 'ZLim');
115 % xx = get(gca, 'XLim');
117 % yy = get(gca, 'YLim');
122 % Julio Trevisan 09/12/2009: Let's draw them in the axes!
126 h1=plot3(x0+k*x,y0+k*y, z, 'Color', prop.style, 'LineWidth',
scaled(2));hold on
130 h2=plot3(x, y0+k*y,z0+k*z, 'Color', prop.style, 'LineWidth', scaled(2));hold on
134 h3=plot3(x0+k*x, y, z0+k*z, 'Color', prop.style, 'LineWidth', scaled(2));hold on
139 % [eigvec,eigval] = eig(C);
141 % [X,Y,Z] = ellipsoid(0,0,0,1,1,1);
142 % XYZ = [X(:),Y(:),Z(:)]*sqrt(eigval)*eigvec';
144 % X(:) = scale*(k*XYZ(:,1)+x0);
145 % Y(:) = scale*(k*XYZ(:,2)+y0);
146 % Z(:) = scale*(k*XYZ(:,3)+z0);
155 % Make the matrix has positive eigenvalues - else it's not a valid covariance matrix!
157 irerror('The covariance matrix must be positive definite (it has non-positive eigenvalues)')
161 h1=plot(scale*(x0+k*x),scale*(y0+k*y),prop.style);
167 irerror('C (covaraince matrix) must be specified
as a 2x2 or 3x3 matrix)')
171 set(gca,'nextplot',hold_state);
173 %---------------------------------------------------------------
174 %
getpoints - Generate x and y points that define an ellipse, given a 2x2
175 % covariance matrix, C. z, if requested, is all zeros with same shape
as
177 function [x,y,z] =
getpoints(C,clipping_ruleRadii)
179 n=100; % Number of points around ellipse
180 p=0:pi/n:2*pi; % angles around a circle
182 [eigvec,eigval] = eig(C); % Compute eigen-stuff
183 xy = [cos(p'),sin(p')] * sqrt(eigval) * eigvec'; % Transformation
188 % Clip data to a bounding ruleRadii
190 r = sqrt(sum(xy.^2,2)); % Euclidian distance (distance from center)
191 x(r > clipping_ruleRadii) = nan;
192 y(r > clipping_ruleRadii) = nan;
193 z(r > clipping_ruleRadii) = nan;
196 %---------------------------------------------------------------
198 % QCHISQ(P,N) - quantile of the chi-square
distribution.
206 x = 0.5*ones(size(P));
214 if all(abs(dx) < 1e-6)
219 %---------------------------------------------------------------
221 % PCHISQ(X,N) - Probability function of the chi-square
distribution.
231 k = k + (x(s)/2).^jj/factorial(jj);
233 F(s) = 1-exp(-x(s)/2).*k;
237 F(ii) = quadl(@
dchisq,0,x(ii),1e-6,0,n);
244 %---------------------------------------------------------------
245 function f=dchisq(x,n)
246 % DCHISQ(X,N) - Density function of the chi-square
distribution.
252 f(s) = x(s).^(n/2-1).*exp(-x(s)/2)./(2^(n/2)*gamma(n/2));
254 %---------------------------------------------------------------
255 function properties =
getopt(properties,varargin)
256 %GETOPT - Process paired optional arguments
as 'prop1',val1,'prop2',val2,...
258 %
getopt(properties,varargin) returns a modified properties structure,
259 % given an initial properties structure, and a list of paired arguments.
260 % Each argumnet pair should be of the form property_name,val where
261 % property_name is the name of one of the field in properties, and val is
262 % the value to be assigned to that structure field.
264 % No validation of the values is performed.
267 % properties = struct('zoom',1.0,'aspect',1.0,'gamma',1.0,'file',[],'bg',[]);
268 % properties =
getopt(properties,'aspect',0.76,'file','mydata.dat')
277 % Typical usage in a function:
278 % properties =
getopt(properties,varargin{:})
280 % Process the properties (optional input arguments)
281 prop_names = fieldnames(properties);
283 for ii=1:length(varargin)
288 irerror('Propery names must be character strings');
290 f = find(strcmp(prop_names, arg));
292 irerror('%s ',['invalid property ''',arg,'''; must be one of:'],prop_names{:});
296 % properties.(TargetField) = arg; % Ver 6.5 and later only
297 properties = setfield(properties, TargetField, arg); % Ver 6.1 friendly
301 if ~isempty(TargetField)
302 irerror('Property names and values must be specified in pairs.');
function pchisq(in x, in n)
function getopt(in properties, in varargin)
function getpoints(in C, in clipping_ruleRadii)
function distribution(in x, in no_points, in range, in wid)
function error_ellipse2(in varargin)
function dchisq(in x, in n)
function qchisq(in P, in n)
Analysis Session (AS) base class.