在MATLAB中绘制椭圆和椭球

如何使用MATLAB绘制椭圆和椭球?

(x^2/a^2)+(y^2/b^2)=1

 n=40; a=0; b=2*pi; c=0; d=2*pi; for i=1:n u=a+(ba)*(i-1)/(n-1); for j=1:m v=a+(dc)*(j-1)/(m-1); x(i,j)=sin(u)*cos(v); y(i,j)=sin(u)*sin(v); z(i,j)=cos(u); end end mesh(x,y,z); 

但我想要的形状?

维基百科上的椭圆文章有一个简单的JavaScript代码来绘制椭圆。

它使用参数forms:

 x(theta) = a0 + ax*sin(theta) + bx*cos(theta) y(theta) = b0 + ay*sin(theta) + by*cos(theta) 

哪里

 (a0,b0) is the center of the ellipse (ax,ay) vector representing the major axis (bx,by) vector representing the minor axis 

我将代码翻译成MATLAB函数:

calculateEllipse.m

 function [X,Y] = calculateEllipse(x, y, a, b, angle, steps) %# This functions returns points to draw an ellipse %# %# @param x X coordinate %# @param y Y coordinate %# @param a Semimajor axis %# @param b Semiminor axis %# @param angle Angle of the ellipse (in degrees) %# narginchk(5, 6); if nargin<6, steps = 36; end beta = -angle * (pi / 180); sinbeta = sin(beta); cosbeta = cos(beta); alpha = linspace(0, 360, steps)' .* (pi / 180); sinalpha = sin(alpha); cosalpha = cos(alpha); X = x + (a * cosalpha * cosbeta - b * sinalpha * sinbeta); Y = y + (a * cosalpha * sinbeta + b * sinalpha * cosbeta); if nargout==1, X = [XY]; end end 

并用一个例子来testing它:

 %# ellipse centered at (0,0) with axes length %# major=20, ,minor=10, rotated 50 degrees %# (drawn using the default N=36 points) p = calculateEllipse(0, 0, 20, 10, 50); plot(p(:,1), p(:,2), '.-'), axis equal 

例如旋转的椭圆

我已经根据您对equation_ellipse的要求调整了MATLAB Central的这个优秀的椭圆绘图脚本 http://img121.imageshack.us/img121/5746/eqn1995.png

 function plotEllipse(a,b,C) % range to plot over %------------------------------------ N = 50; theta = 0:1/N:2*pi+1/N; % Parametric equation of the ellipse %---------------------------------------- state(1,:) = a*cos(theta); state(2,:) = b*sin(theta); % Coordinate transform (since your ellipse is axis aligned) %---------------------------------------- X = state; X(1,:) = X(1,:) + C(1); X(2,:) = X(2,:) + C(2); % Plot %---------------------------------------- plot(X(1,:),X(2,:)); hold on; plot(C(1),C(2),'r*'); axis equal; grid; end 

注意:改变N来定义椭圆的分辨率

这里是一个以(10,10)为中心的椭圆, a = 30b = 10

椭圆http://img715.imageshack.us/img715/834/59078110.png

Jacob和Amro的答案是计算和绘制椭圆点很好的例子。 我会解决一些简单的方法,你可以绘制一个椭球

首先,MATLAB有一个内置函数ELLIPSOID ,它给出椭球体中心和半轴长度,生成一组网格点。 下面为以x,y和z方向的半轴长度分别为4,2和1的原点为中心的椭球创buildmatrixxyz

 [x, y, z] = ellipsoid(0, 0, 0, 4, 2, 1); 

然后,您可以使用MESH函数来绘制它,并返回一个绘制的表面对象的句柄:

 hMesh = mesh(x, y, z); 

如果你想旋转绘制的椭球,你可以使用ROTATE函数。 以下内容适用于围绕y轴旋转45度:

 rotate(hMesh, [0 1 0], 45); 

然后可以调整绘图外观以获得下图:

 axis equal; %# Make tick mark increments on all axes equal view([-36 18]); %# Change the camera viewpoint xlabel('x'); ylabel('y'); zlabel('z'); 

在这里输入图像描述

此外,如果要使用旋转的绘图点进行进一步计算,则可以从绘制的曲面对象中获取它们:

 xNew = get(hMesh, 'XData'); %# Get the rotated x points yNew = get(hMesh, 'YData'); %# Get the rotated y points zNew = get(hMesh, 'ZData'); %# Get the rotated z points 

创build两个向量,即椭圆体圆周点的一个x坐标,即y坐标之一。 使这些向量足够长,以满足您的精度要求。 将这两个向量绘制为(x,y)对。 如果使用向量表示法,我会从代码中删除for循环。 另外,我会用代码的SO标记格式化你的问题,使你的听众更清楚。

维基百科和旋转matrix上的椭圆文章。

重写那个函数:

  • (0,0)逆时针旋转rotAngle

  • 坐标变换为(cx, cy)


 function [X,Y] = calculateEllipse(cx, cy, a, b, rotAngle) %# This functions returns points to draw an ellipse %# %# @param x X coordinate %# @param y Y coordinate %# @param a Semimajor axis %# @param b Semiminor axis %# @param cx cetner x position %# @param cy cetner y position %# @param angle Angle of the ellipse (in degrees) %# steps = 30; angle = linspace(0, 2*pi, steps); % Parametric equation of the ellipse X = a * cos(angle); Y = b * sin(angle); % rotate by rotAngle counter clockwise around (0,0) xRot = X*cosd(rotAngle) - Y*sind(rotAngle); yRot = X*sind(rotAngle) + Y*cosd(rotAngle); X = xRot; Y = yRot; % Coordinate transform X = X + cx; Y = Y + cy; end 

并用一个例子来testing它:

 [X,Y] = calculateEllipse(0, 0, 20, 10, 0); plot(X, Y, 'b'); hold on; % blue [X,Y] = calculateEllipse(0, 0, 20, 10, 45); plot(X, Y, 'r'); hold on; % red [X,Y] = calculateEllipse(30, 30, 20, 10, 135); plot(X, Y, 'g'); % green grid on; 

图1

最简单的方法可能是使用Matlab函数

 pdeellip(xc,yc,a,b,phi) 

例如:

 pdeellip(0,0,1,0.3,pi/4) 

但是,这是简单的解决scheme,可以快速浏览一下椭圆的外观。 如果你想有一个很好的情节,看看其他的解决scheme。

我不知道在哪个版本的Matlab中添加了这个版本,但是至less从版本R2012b开始。