%% Plots the pdf of a 2D Gaussian as a surface
% From 'A first course in Machine Learning'
% Simon Rogers, 26th November 2009
% The Multivariate Gaussian pdf is defined as
% 
% 
% $$p(\mathbf{x}) = \frac{1}{(2\pi)^{D/2}|\Sigma|^{1/2}}\exp\left\{-\frac{1}{2}(\mathbf{x}-\mu)^T\Sigma^{-1}(\mathbf{x}-\mu)\right\}$$
% 
%
% With mean
%
% $$\mu$$
%
% And covariance
% 
% $$\Sigma$$
% 

% clear all;
% Define the Gaussian mean and covariance
% mu = [3;-2]; % Mean vector
% Sigma = [0.0627 -0.0809;-0.0809 0.1301]; % Covariance matrix

% Create a dense grid of points at which to evaluate the pdf
[Xt,Yt] = meshgrid(-5:0.1:5,-5:0.1:5);

% Compute the constant
const = 1/((2*pi)*sqrt(det(Sigma)));

% Evaluate the pdf at each grid point
Z = zeros(size(Xt));

% mu2 = mu + [2;3];

for i = 1:prod(size(Xt))
    ve = [Xt(i);Yt(i)];
    Z(i) = const * exp(-0.5 * (ve-mu)' * inv(Sigma)*(ve-mu));%+...
%         0.7*const*exp(-0.5 * (ve-mu2)' * inv(Sigma)*(ve-mu2));
end



% Create the surface plot and make it look nice
% figure(1)
% surf(X,Y,Z)
% h = get(1,'children');
% set(h,'fontsize',25)
% xlim([min(min(X)),max(max(X))])
% ylim([min(min(Y)),max(max(Y))])
% 
% xlabel('$x_1$','interpreter','latex','fontsize',30)
% ylabel('$x_2$','interpreter','latex','fontsize',30)


% Create the contour plot and make it look nice
contour(Xt,Yt,Z,7,'linewidth',2,'color',col)
colormap(gray)
xlabel('$w_1$','interpreter','latex','fontsize',30)
ylabel('$w_2$','interpreter','latex','fontsize',30)
