function [ X, Y, Z ] = gellipse( mu, C, N )
%GELLIPSE returns three matrices to plot an arbitrary ellipse
%   Matrices X, Y, Z are NxN and contain the points needed to draw
%   an ellipse with surf or mesh. The ellipse is centered on 3x1 vector mu
%   and the shape and size of the ellipse is controlled by 3x3 matrix C

LL = chol(C)';
phi   = linspace(0,2*pi,N)';
theta = linspace(-pi/2,pi/2,N)';
Xc = cos(phi)*(cos(theta)');
Yc = sin(phi)*(cos(theta)');
Zc = ones(N,1)*(sin(theta)');

X = LL(1,1)*Xc + LL(1,2)*Yc + LL(1,3)*Zc + mu(1);
Y = LL(2,1)*Xc + LL(2,2)*Yc + LL(2,3)*Zc + mu(2);
Z = LL(3,1)*Xc + LL(3,2)*Yc + LL(3,3)*Zc + mu(3);
end

