wolffd@0: % PLOTCOV2 - Plots a covariance ellipsoid with axes for a bivariate wolffd@0: % Gaussian distribution. wolffd@0: % wolffd@0: % Usage: wolffd@0: % [h, s] = plotcov2(mu, Sigma[, OPTIONS]); wolffd@0: % wolffd@0: % Inputs: wolffd@0: % mu - a 2 x 1 vector giving the mean of the distribution. wolffd@0: % Sigma - a 2 x 2 symmetric positive semi-definite matrix giving wolffd@0: % the covariance of the distribution (or the zero matrix). wolffd@0: % wolffd@0: % Options: wolffd@0: % 'conf' - a scalar between 0 and 1 giving the confidence wolffd@0: % interval (i.e., the fraction of probability mass to wolffd@0: % be enclosed by the ellipse); default is 0.9. wolffd@0: % 'num-pts' - if the value supplied is n, then (n + 1)^2 points wolffd@0: % to be used to plot the ellipse; default is 20. wolffd@0: % 'label' - if non-empty, a string that will label the wolffd@0: % ellipsoid (default: []) wolffd@0: % 'plot-axes' - a 0/1 flag indicating if the ellipsoid's axes wolffd@0: % should be plotted (default: 1) wolffd@0: % 'plot-opts' - a cell vector of arguments to be handed to PLOT3 wolffd@0: % to contol the appearance of the axes, e.g., wolffd@0: % {'Color', 'g', 'LineWidth', 1}; the default is {} wolffd@0: % 'fill-color' - a color specifier; is this is not [], the wolffd@0: % covariance ellipse is filled with this color wolffd@0: % (default: []) wolffd@0: % wolffd@0: % Outputs: wolffd@0: % h - a vector of handles on the axis lines wolffd@0: % wolffd@0: % See also: PLOTCOV3 wolffd@0: wolffd@0: % Copyright (C) 2002 Mark A. Paskin wolffd@0: % wolffd@0: % This program is free software; you can redistribute it and/or modify wolffd@0: % it under the terms of the GNU General Public License as published by wolffd@0: % the Free Software Foundation; either version 2 of the License, or wolffd@0: % (at your option) any later version. wolffd@0: % wolffd@0: % This program is distributed in the hope that it will be useful, but wolffd@0: % WITHOUT ANY WARRANTY; without even the implied warranty of wolffd@0: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU wolffd@0: % General Public License for more details. wolffd@0: % wolffd@0: % You should have received a copy of the GNU General Public License wolffd@0: % along with this program; if not, write to the Free Software wolffd@0: % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 wolffd@0: % USA. wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: function [h, s] = plotcov2New(mu, Sigma, varargin) wolffd@0: wolffd@0: h = []; wolffd@0: s = []; wolffd@0: wolffd@0: if size(Sigma) ~= [2 2], error('Sigma must be a 2 by 2 matrix'); end wolffd@0: if length(mu) ~= 2, error('mu must be a 2 by 1 vector'); end wolffd@0: wolffd@0: Sigma = checkpsd(Sigma); wolffd@0: wolffd@0: [p, ... wolffd@0: n, ... wolffd@0: label, ... wolffd@0: plot_axes, ... wolffd@0: plot_opts, ... wolffd@0: fill_color] = process_options(varargin, 'conf', 0.9, ... wolffd@0: 'num-pts', 20, ... wolffd@0: 'label', [], ... wolffd@0: 'plot-axes', 1, ... wolffd@0: 'plot-opts', {}, ... wolffd@0: 'fill-color', []); wolffd@0: holding = ishold; wolffd@0: % Compute the Mahalanobis radius of the ellipsoid that encloses wolffd@0: % the desired probability mass. wolffd@0: k = conf2mahal(p, 2); wolffd@0: % Scale the covariance matrix so the confidence region has unit wolffd@0: % Mahalanobis distance. wolffd@0: Sigma = Sigma * k; wolffd@0: % The axes of the covariance ellipse are given by the eigenvectors of wolffd@0: % the covariance matrix. Their lengths (for the ellipse with unit wolffd@0: % Mahalanobis radius) are given by the square roots of the wolffd@0: % corresponding eigenvalues. wolffd@0: [V, D] = eig(full(Sigma)); wolffd@0: V = real(V); wolffd@0: D = real(D); wolffd@0: D = abs(D); wolffd@0: wolffd@0: % Compute the points on the boundary of the ellipsoid. wolffd@0: t = linspace(0, 2*pi, n); wolffd@0: u = [cos(t(:))'; sin(t(:))']; wolffd@0: w = (V * sqrt(D)) * u; wolffd@0: z = repmat(mu(:), [1 n]) + w; wolffd@0: h = [h; plot(z(1, :), z(2, :), plot_opts{:})]; wolffd@0: if (~isempty(fill_color)) wolffd@0: s = patch(z(1, :), z(2, :), fill_color); wolffd@0: end wolffd@0: wolffd@0: % Plot the axes. wolffd@0: if (plot_axes) wolffd@0: hold on; wolffd@0: L = sqrt(diag(D)); wolffd@0: h = plot([mu(1); mu(1) + L(1) * V(1, 1)], ... wolffd@0: [mu(2); mu(2) + L(1) * V(2, 1)], plot_opts{:}); wolffd@0: h = [h; plot([mu(1); mu(1) + L(2) * V(1, 2)], ... wolffd@0: [mu(2); mu(2) + L(2) * V(2, 2)], plot_opts{:})]; wolffd@0: end wolffd@0: wolffd@0: wolffd@0: if (~isempty(label)) wolffd@0: th = text(mu(1), mu(2), label); wolffd@0: set(th, 'FontSize', 18); wolffd@0: set(th, 'FontName', 'Times'); wolffd@0: set(th, 'FontWeight', 'bold'); wolffd@0: set(th, 'FontAngle', 'italic'); wolffd@0: set(th, 'HorizontalAlignment', 'center'); wolffd@0: end wolffd@0: wolffd@0: if (~holding & plot_axes) hold off; end