【问题标题】:draw ellipse and ellipsoid in MATLAB在MATLAB中绘制椭圆和椭球
【发布时间】:2010-01-28 10:30:12
【问题描述】:

如何使用 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);

但我想要形状?

【问题讨论】:

    标签: matlab plot


    【解决方案1】:

    Wikipedia 上的椭圆文章有一个简单的 JavaScript 代码来绘制椭圆。

    它使用参数形式:

    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 = [X Y]; end
    end
    

    和一个测试它的例子:

    %# 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
    

    【讨论】:

      【解决方案2】:

      Jacob 和Amro 的答案是计算和绘制椭圆点的非常好的示例。我将介绍一些绘制椭球体的简单方法...

      首先,MATLAB 有一个内置函数ELLIPSOID,它在给定椭球中心和半轴长度的情况下生成一组网格点。下面为以原点为中心的椭球创建矩阵x、y 和z,x、y 和 z 方向的半轴长度分别为 4、2 和 1:

      [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
      

      【讨论】:

        【解决方案3】:

        我已经从 MATLAB Central 改编了这个 excellent ellipse plotting script,以满足您对 的要求

        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 = 30 和b = 10

        【讨论】:

          【解决方案4】:

          关于Wikipedia 和旋转矩阵的椭圆文章。

          重写那个函数:

          • 围绕(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
          

          以及一个测试它的例子:

          [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;
          

          【讨论】:

            【解决方案5】:

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

            【讨论】:

            • ooi:Mark 关于使用矢量符号的建议不仅使您的代码更具可读性,而且还提高了效率
            【解决方案6】:

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

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

            例如:

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

            但是,这是一个简单的解决方案,可以快速浏览一下椭圆的外观。如果您想有一个漂亮的情节,请查看其他解决方案。

            我不知道这是在哪个版本的 Matlab 中添加的,但至少从 R2012b 版本开始就可以使用它。

            【讨论】:

            • pdeellip 函数是“偏微分方程工具箱”的一部分,它将在PDE app 内绘制一个椭圆,不适用于常规图形/轴。
            • 正确,但 ooi 没有要求。问题只是如何绘制椭圆。如果您只是想快速浏览一下椭圆的外观,这可能是最快的方法。
            • 很公平(顺便说一句,我没有投反对票),我只是指出了这个函数的上下文
            • 你是对的。我本可以在答案中提到它(所以我确实编辑了答案)。
            猜你喜欢
            • 1970-01-01
            • 2015-05-08
            • 2013-10-09
            • 1970-01-01
            • 2012-07-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多