14
votes

How do I draw an ellipse and an ellipsoid using 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);

But I want the shape?

6

6 Answers

41
votes

Ellipse article on Wikipedia had a simple JavaScript code to draw ellipses.

It uses the parametric form:

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

where

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

I translated the code into a MATLAB function:

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

and an example to test it:

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

example rotated ellipse

12
votes

The answers from Jacob and Amro are very good examples for computing and plotting points for an ellipse. I'll address some easy ways you can plot an ellipsoid...

First, MATLAB has a built-in function ELLIPSOID which generates a set of mesh points given the ellipsoid center and the semi-axis lengths. The following creates the matrices x, y, and z for an ellipsoid centered at the origin with semi-axis lengths of 4, 2, and 1 for the x, y, and z directions, respectively:

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

You can then use the function MESH to plot it, returning a handle to the plotted surface object:

hMesh = mesh(x, y, z);

If you want to rotate the plotted ellipsoid, you can use the ROTATE function. The following applies a rotation of 45 degrees around the y-axis:

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

You can then adjust the plot appearance to get the following figure:

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

enter image description here

Also, if you want to use the rotated plot points for further calculations, you can get them from the plotted surface object:

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
12
votes

I've adapted this excellent ellipse plotting script from MATLAB Central for your requirement of equation_ellipse

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

Note: change N to define the resolution of your ellipse

Here's an ellipse centered at (10,10) with a = 30 and b = 10

Ellipse

3
votes

Ellipse article on Wikipedia and Rotation matrix.

Rewrite that functions:

  • rotate by rotAngle counter-clockwise around (0,0)

  • Coordinate transform to (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

and an example to test it:

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

Figure 1

2
votes

Create two vectors, one of the x-coordinates of the points of the circumference of the ellipsoid, one of the y-coordinates. Make these vectors long enough to satisfy your accuracy requirements. Plot the two vectors as (x,y) pairs joined up. I'd drop the for loops from your code, much clearer if you use vector notation. Also I'd format your question using the SO markup for code to make it all clearer to your audience.

-2
votes

The simplest way might be to use the Matlab function

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

For example:

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

However, this is simple solution is good to have a quick glance how the ellipse looks like. If you want to have a nice plot, look at the other solutions.

I don't know in which version of Matlab this was added, but it's available at least from version R2012b on.