matthiasm@8: % PLOTCOV3 - Plots a covariance ellipsoid with axes for a trivariate matthiasm@8: % Gaussian distribution. matthiasm@8: % matthiasm@8: % Usage: matthiasm@8: % [h, s] = plotcov3(mu, Sigma[, OPTIONS]); matthiasm@8: % matthiasm@8: % Inputs: matthiasm@8: % mu - a 3 x 1 vector giving the mean of the distribution. matthiasm@8: % Sigma - a 3 x 3 symmetric positive semi-definite matrix giving matthiasm@8: % the covariance of the distribution (or the zero matrix). matthiasm@8: % matthiasm@8: % Options: matthiasm@8: % 'conf' - a scalar between 0 and 1 giving the confidence matthiasm@8: % interval (i.e., the fraction of probability mass to matthiasm@8: % be enclosed by the ellipse); default is 0.9. matthiasm@8: % 'num-pts' - if the value supplied is n, then (n + 1)^2 points matthiasm@8: % to be used to plot the ellipse; default is 20. matthiasm@8: % 'plot-opts' - a cell vector of arguments to be handed to PLOT3 matthiasm@8: % to contol the appearance of the axes, e.g., matthiasm@8: % {'Color', 'g', 'LineWidth', 1}; the default is {} matthiasm@8: % 'surf-opts' - a cell vector of arguments to be handed to SURF matthiasm@8: % to contol the appearance of the ellipsoid matthiasm@8: % surface; a nice possibility that yields matthiasm@8: % transparency is: {'EdgeAlpha', 0, 'FaceAlpha', matthiasm@8: % 0.1, 'FaceColor', 'g'}; the default is {} matthiasm@8: % matthiasm@8: % Outputs: matthiasm@8: % h - a vector of handles on the axis lines matthiasm@8: % s - a handle on the ellipsoid surface object matthiasm@8: % matthiasm@8: % See also: PLOTCOV2 matthiasm@8: matthiasm@8: % Copyright (C) 2002 Mark A. Paskin matthiasm@8: % matthiasm@8: % This program is free software; you can redistribute it and/or modify matthiasm@8: % it under the terms of the GNU General Public License as published by matthiasm@8: % the Free Software Foundation; either version 2 of the License, or matthiasm@8: % (at your option) any later version. matthiasm@8: % matthiasm@8: % This program is distributed in the hope that it will be useful, but matthiasm@8: % WITHOUT ANY WARRANTY; without even the implied warranty of matthiasm@8: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU matthiasm@8: % General Public License for more details. matthiasm@8: % matthiasm@8: % You should have received a copy of the GNU General Public License matthiasm@8: % along with this program; if not, write to the Free Software matthiasm@8: % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 matthiasm@8: % USA. matthiasm@8: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% matthiasm@8: matthiasm@8: function [h, s] = plotcov3(mu, Sigma, varargin) matthiasm@8: matthiasm@8: if size(Sigma) ~= [3 3], error('Sigma must be a 3 by 3 matrix'); end matthiasm@8: if length(mu) ~= 3, error('mu must be a 3 by 1 vector'); end matthiasm@8: matthiasm@8: [p, ... matthiasm@8: n, ... matthiasm@8: plot_opts, ... matthiasm@8: surf_opts] = process_options(varargin, 'conf', 0.9, ... matthiasm@8: 'num-pts', 20, ... matthiasm@8: 'plot-opts', {}, ... matthiasm@8: 'surf-opts', {}); matthiasm@8: h = []; matthiasm@8: holding = ishold; matthiasm@8: if (Sigma == zeros(3, 3)) matthiasm@8: z = mu; matthiasm@8: else matthiasm@8: % Compute the Mahalanobis radius of the ellipsoid that encloses matthiasm@8: % the desired probability mass. matthiasm@8: k = conf2mahal(p, 3); matthiasm@8: % The axes of the covariance ellipse are given by the eigenvectors of matthiasm@8: % the covariance matrix. Their lengths (for the ellipse with unit matthiasm@8: % Mahalanobis radius) are given by the square roots of the matthiasm@8: % corresponding eigenvalues. matthiasm@8: if (issparse(Sigma)) matthiasm@8: [V, D] = eigs(Sigma); matthiasm@8: else matthiasm@8: [V, D] = eig(Sigma); matthiasm@8: end matthiasm@8: if (any(diag(D) < 0)) matthiasm@8: error('Invalid covariance matrix: not positive semi-definite.'); matthiasm@8: end matthiasm@8: % Compute the points on the surface of the ellipsoid. matthiasm@8: t = linspace(0, 2*pi, n); matthiasm@8: [X, Y, Z] = sphere(n); matthiasm@8: u = [X(:)'; Y(:)'; Z(:)']; matthiasm@8: w = (k * V * sqrt(D)) * u; matthiasm@8: z = repmat(mu(:), [1 (n + 1)^2]) + w; matthiasm@8: matthiasm@8: % Plot the axes. matthiasm@8: L = k * sqrt(diag(D)); matthiasm@8: h = plot3([mu(1); mu(1) + L(1) * V(1, 1)], ... matthiasm@8: [mu(2); mu(2) + L(1) * V(2, 1)], ... matthiasm@8: [mu(3); mu(3) + L(1) * V(3, 1)], plot_opts{:}); matthiasm@8: hold on; matthiasm@8: h = [h; plot3([mu(1); mu(1) + L(2) * V(1, 2)], ... matthiasm@8: [mu(2); mu(2) + L(2) * V(2, 2)], ... matthiasm@8: [mu(3); mu(3) + L(2) * V(3, 2)], plot_opts{:})]; matthiasm@8: h = [h; plot3([mu(1); mu(1) + L(3) * V(1, 3)], ... matthiasm@8: [mu(2); mu(2) + L(3) * V(2, 3)], ... matthiasm@8: [mu(3); mu(3) + L(3) * V(3, 3)], plot_opts{:})]; matthiasm@8: end matthiasm@8: matthiasm@8: s = surf(reshape(z(1, :), [(n + 1) (n + 1)]), ... matthiasm@8: reshape(z(2, :), [(n + 1) (n + 1)]), ... matthiasm@8: reshape(z(3, :), [(n + 1) (n + 1)]), ... matthiasm@8: surf_opts{:}); matthiasm@8: matthiasm@8: if (~holding) hold off; end