% To visualize a 2D covariance matrix with an ellipse:
% the eigenvalues of a covariance matrix are proportional to the radii of the ellipse, 
% and the eigenvectors denote the direction of the radii. 
% If the scaling parameter for the radii is \Chi_{2, 0.05}^2 = 5.991464, 
% the true value is inside the ellipse with a probability of 95%.

function drawCov(mu, cova)

n = 32;

x0 = mu(1);
y0 = mu(2);

for i = 0:n
	
	sxx = cova(1,1);
	sxy = cova(1,2);
	syy = cova(2,2);

	la = (sxx + syy + sqrt( (sxx-syy)*(sxx-syy) + 4*sxy*sxy ))/2;
	lb = (sxx + syy - sqrt( (sxx-syy)*(sxx-syy) + 4*sxy*sxy ))/2;

	a = sqrt(5.991464*la); % 95% ellipse
	b = sqrt(5.991464*lb);
	phi = atan2(2*sxy,sxx-syy)/2;

	f =  2 * pi * i / n;
	x = a*sin(f);
	y = b*cos(f);

	xr(i+1) = cos(phi)*x - sin(phi)*y + x0;
	yr(i+1) = sin(phi)*x + cos(phi)*y + y0;

end
plot(xr, yr);

