1 ## Copyright (c) 2012 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
17 %% @deftypefn {Function File} {@var{ellipse} = } cov2ellipse (@var{K})
18 %% @deftypefnx {Function File} {[@var{ra} @var{rb} @var{theta}] = } cov2ellipse (@var{K})
19 %% @deftypefnx {Function File} {@dots{} = } cov2ellipse (@dots{}, @samp{tol},@var{tol})
20 %% Calculates ellipse parameters from covariance matrix.
22 %% @var{K} must be symmetric positive (semi)definite. The optional argument
23 %% @samp{tol} sets the tolerance for the verification of the
24 %% positive-(semi)definiteness of the matrix @var{K} (see @command{isdefinite}).
26 %% If only one output argument is supplied a vector defining a ellipse is returned
27 %% as defined in @command{ellipses2d}. Otherwise the angle @var{theta} is given
30 %% Run @code{demo cov2ellipse} to see an example.
32 %% @seealso{ellipses2d, cov2ellipse, drawEllipse}
35 function varargout = cov2ellipse (K, varargin);
37 parser = inputParser ();
38 parser.FunctionName = "cov2ellipse";
39 parser = addParamValue (parser,'Tol', 100*eps*norm (K, "fro"), @(x)x>0);
40 parser = parse(parser,varargin{:});
42 if isdefinite (K,parser.Results.Tol) == -1
46 theta = atan (R(1,1)/R(2,2));
47 v = sort (diag(S), 'ascend')';
50 varargout{1} = [0 0 v theta*180/pi];
61 %! L = chol(K,'lower');
62 %! u = randn(1e3,2)*L';
64 %! elli = cov2ellipse (K)
67 %! plot(u(:,1),u(:,2),'.r');
69 %! drawEllipse(elli,'linewidth',2);