在matlab中绘制椭圆和椭球
ezplot 绘制椭圆
ezplot('x^2+2*x*y+3*y^2+4*x+5*y = 6')
axis([-6 3 -3 3]);
ellipsoid
[x, y, z] = ellipsoid(0,0,0,1,1,3,50);
surfl(x, y, z);
colormap copper;
axis equal;
上述椭球也可由下面的代码绘出
[theta,phi] = meshgrid(0:10:360,-90:10:90);
X = cos(phi*pi/180).*cos(theta*pi/180);
Y = cos(phi*pi/180).*sin(theta*pi/180);
Z = 3*sin(phi*pi/180);
hold on; surfl(X, Y, Z);
axis equal;
绘制一个矩阵对应的特征向量为主轴的椭球
用meshgrid生成 \theta 、 \phi 格点, X, Y, Z 是计算出的球面上的格点。利用矩阵的性质可知,矩阵与某坐标相乘,相当于进行坐标变换。利用矩阵对向量的线性变换的性质,用矩阵 \bf{A} 与球面上所有格点坐标形成的向量相乘
\bf{P}'=\bf{A} \begin{bmatrix} x\\y\\z \end{bmatrix}
即将球面上格点的位置旋转拉伸到到椭球面的位置,且该椭球的三个主轴方向分别对应矩阵 \bf{A} 的三个特征向量。
[theta,phi] = meshgrid(0:10:360,-90:10:90);
[nr,nc] = size(theta);
X = cos(phi*pi/180).*cos(theta*pi/180);
Y = cos(phi*pi/180).*sin(theta*pi/180);
Z = sin(phi*pi/180);
P=[reshape(X,nr*nc,1),reshape(Y,nr*nc,1),reshape(Z,nr*nc,1)];
A = [1,2,0; 2,3,1; 0,1,1];
P = P*A';
PX = reshape(P(:,1),nr,nc);
PY = reshape(P(:,2),nr,nc);
PZ = reshape(P(:,3),nr,nc);
hold on; surfl(X, Y, Z);
hold on; surfl(PX, PY, PZ);
colormap copper;
axis equal;
计算生成椭球的球面坐标
绘制主轴与矩阵特征向量对应的椭球。方法2.直接计算生成椭球的球面坐标
\mathbf{P} = \lambda_3*\sin \phi*\Phi_3 + \lambda_2*\cos \phi \cos \theta *\Phi_2+\lambda_1*\cos \phi \sin \theta *\Phi_1
把 \Phi_3 看作直角坐标系中的z坐标、 \Phi_2 作直角坐标系中的x坐标、 \Phi_3 作直角坐标系中的y坐标.
[theta,phi] = meshgrid(0:10:360,-90:10:90);
[nr,nc] = size(theta);
Theta = reshape(theta,nr*nc,1)*pi/180;
Phi = reshape(phi,nr*nc,1)*pi/180;
A = [1,2,0; 2,3,1; 0,1,1];
[R,V] = eig(A);
nd = size(A,1);
V = abs(V);
%R = R*(V+max(diag(V)/5)*eye(nd));
%绘制向量箭头