% eda03_16
% Illustrate the calculation of a multivariate Normal distribution
% with for loops

% set up vectors da and d2
L=40;
Dd = 1.0;
d1 = Dd*[0:L-1]';
d2 = Dd*[0:L-1]';

% make a positively correlated normal distribution
d1bar=15;
d2bar=25;
C=[[6,4]',[4,10]'];
CI=inv(C);
norm=1/(2*pi*sqrt(det(C)));
Pp=zeros(L,L);
for i = [1:L]
for j = [1:L]
   dd = [d1(i)-d1bar, d2(j)-d2bar]';
   Pp(i,j)=norm*exp( -0.5 * dd' * CI * dd );
end
end

% make a negatively correlated normal distribution
d1bar=15;
d2bar=25;
C=[[6,-4]',[-4,10]'];
CI=inv(C);
norm=1/(2*pi*sqrt(det(C)));
Pn=zeros(L,L);
for i = [1:L]
for j = [1:L]
   dd = [d1(i)-d1bar, d2(j)-d2bar]';
   Pn(i,j)=norm*exp( -0.5 * dd' * CI * dd );
end
end

% make an uncorrelated normal distribution
d1bar=15;
d2bar=25;
C=[[6,0]',[0,10]'];
CI=inv(C);
norm=1/(2*pi*sqrt(det(C)));
Pu=zeros(L,L);
for i = [1:L]
for j = [1:L]
   dd = [d1(i)-d1bar, d2(j)-d2bar]';
   Pu(i,j)=norm*exp( -0.5 * dd' * CI * dd );
end
end

% use simple drawing function that encapsulates all the graphics
eda_draw(Pp,'caption +cor',Pn,'caption -cor',Pu,'caption uncor');

