在MATLAB中绘制椭圆和椭圆体

ooi*_*ooi 13 matlab plot

如何使用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+(b-a)*(i-1)/(n-1);
    for j=1:m
        v=a+(d-c)*(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);
Run Code Online (Sandbox Code Playgroud)

但我想要的形状?

Amr*_*mro 41

维基百科上的Ellipse文章有一个简单的JavaScript代码来绘制省略号.

它使用参数形式:

x(theta) = a0 + ax*sin(theta) + bx*cos(theta)
y(theta) = b0 + ay*sin(theta) + by*cos(theta)
Run Code Online (Sandbox Code Playgroud)

哪里

(a0,b0) is the center of the ellipse
(ax,ay) vector representing the major axis
(bx,by) vector representing the minor axis
Run Code Online (Sandbox Code Playgroud)

我将代码翻译成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 = [X Y]; end
end
Run Code Online (Sandbox Code Playgroud)

以及测试它的一个例子:

%# 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
Run Code Online (Sandbox Code Playgroud)

示例旋转椭圆


Jac*_*cob 12

我已经根据您对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
Run Code Online (Sandbox Code Playgroud)

注意:更改N以定义椭圆的分辨率

下面是在中心的椭圆形(10,10)a = 30b = 10

Ellipse http://img715.imageshack.us/img715/834/59078110.png


gno*_*ice 12

JacobAmro的答案是计算和绘制椭圆点的非常好的例子.我会解决一些简单的方法,你可以绘制一个椭球 ......

首先,MATLAB有一个内置函数ELLIPSOID,它根据椭球中心和半轴长度生成一组网格点.下面创建矩阵x,y以及z对于以x,y和z方向分别为半轴长度为4,2和1的原点为中心的椭圆体:

[x, y, z] = ellipsoid(0, 0, 0, 4, 2, 1);
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用函数MESH绘制它,返回绘制的曲面对象的句柄:

hMesh = mesh(x, y, z);
Run Code Online (Sandbox Code Playgroud)

如果要旋转绘制的椭球,可以使用ROTATE函数.以下适用于围绕y轴旋转45度:

rotate(hMesh, [0 1 0], 45);
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述

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

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
Run Code Online (Sandbox Code Playgroud)


Hig*_*ark 2

创建两个向量,其中之一是椭球圆周点的 x 坐标,之一是 y 坐标。使这些向量足够长以满足您的精度要求。将两个向量绘制为连接在一起的 (x,y) 对。如果您使用矢量表示法,我会从您的代码中删除 for 循环,这样会更清晰。另外,我会使用代码的 SO 标记来格式化您的问题,以使您的受众更清楚。