function ellipse(m, C, r)
% ELLIPSE Plot an ellipse.
%
% ELLIPSE(M, C, R) plots an ellipse that describes a two-variable Gaussian
% distribution with mean M and covariance matrix C. R controls the size of
% the ellipse.
%
% Example: ellipse([0;0], eye(2), 1) draws the unit circle.
%
% Author: Steve Tjoa
% Institution: University of Maryland (Signals and Information Group)
% Created: July 2, 2009
% Last modified: July 2, 2009
%
% This code was written during the workshop on Music Information Retrieval
% at the Center for Computer Research in Music and Acoustics (CCRMA) at
% Stanford University.
% Define 500 points spaced equally on the circle.
theta = 0:(2*pi/500):2*pi;
% Load a circle of radius r.
x = r*cos(theta);
y = r*sin(theta);
% Eigendecompose the covariance matrix.
[Q, D] = eig(C);
% Compute the ellipse.
z = Q*sqrt(D)*[x;y];
% Shift to center, and plot.
plot(z(1,:) + m(1), z(2,:) + m(2), 'r-');