# 高斯分布

，即

Mu = [2 3]';
Sigma = [0.9 0.4;0.4 0.2];
p = mvnrnd(Mu,Sigma,100);
plot(p(:,1),p(:,2),'.','MarkerSize',6)

r =1;
[V,D] = eig(Sigma);

y = linspace(-sqrt(r^2*D(2,2)),sqrt(r^2*D(2,2)),60);
% compute x
x(1,:) = sqrt((r^2-y(:).^2/D(2,2))*D(1,1));
x(1,:) = real(x(1,:));

Ellip = [x,-x(1,:)]; % x
Ellip(2,:) = [y,fliplr(y)]; %y
Ellip = Ellip'*inv(V); % rotate
Ellip(:,1) = Ellip(:,1)+Mu(1); %shift
Ellip(:,2) = Ellip(:,2)+Mu(2);
hold on;
plot(Ellip(:,1),Ellip(:,2));

plot(Mu(1),Mu(2),'+'); %Plot center

xhalf = linspace(sqrt(r^2*D(1,1)),0,Nint);
Ninthalf = round(Nint/2);
zsect = zeros(Nint,Ninthalf);
ysect = zeros(Nint,Ninthalf);
for ti = 1:Nint
r2d = r^2 - xhalf(ti).^2/D(1,1);
ysect(ti,:) = linspace(0,sqrt(r2d*D(2,2)),Ninthalf);

zsect(ti,:) = sqrt((r2d - ysect(ti,:).^2/D(2,2) )*D(3,3));
xsect(ti,1:Ninthalf) = xhalf(ti);
end
zsect = real(zsect);

%x&gt;0,Z&gt;0
xsect = [xsect,xsect];
ysect = [ysect,fliplr(ysect)];
zsect = [zsect,-fliplr(zsect)];
1/2
%x&gt;0
xsect = [xsect,xsect];
ysect = [ysect,-fliplr(ysect)];
zsect = [zsect,fliplr(zsect)];
1/1
% make it a whole
xsect = [xsect;-flipdim(xsect,1)];
ysect = [ysect;flipdim(ysect,1)];
zsect = [zsect;flipdim(zsect,1)];

% rotate
[lr,lc] = size(xsect);
for ti = 1:lr
for tj = 1:lc
newcodi = [xsect(ti,tj),ysect(ti,tj),zsect(ti,tj)]*inv(V);
xsect(ti,tj) = newcodi(1);
ysect(ti,tj) = newcodi(2);
zsect(ti,tj) = newcodi(3);
end
end
% shift
xsect = xsect+Mu(1);
ysect = ysect+Mu(2);
zsect = zsect+Mu(3);
surf(xsect,ysect,zsect);