MATLAB Answers

## How to plot an Ellipse

Asked by Dimitris Arapidis

### Dimitris Arapidis (view profile)

on 8 Sep 2013
Latest activity Commented on by Walter Roberson

### Walter Roberson (view profile)

on 13 May 2019 at 17:13
Accepted Answer by Roger Stafford

### Roger Stafford (view profile)

I want to plot an Ellipse. I have the verticles for the major axis: d1(0,0.8736) d2(85.8024,1.2157) (The coordinates are taken from another part of code so the ellipse must be on the first quadrant of the x-y axis) I also want to be able to change the eccentricity of the ellipse.

#### 0 Comments

Sign in to comment.

## 4 Answers

Answer by Roger Stafford

### Roger Stafford (view profile)

on 8 Sep 2013
Edited by Cris LaPierre

### Cris LaPierre (view profile)

on 5 Apr 2019
Accepted Answer

Let (x1,y1) and (x2,y2) be the coordinates of the two vertices of the ellipse's major axis, and let e be its eccentricity.
a = 1/2*sqrt((x2-x1)^2+(y2-y1)^2);
b = a*sqrt(1-e^2);
t = linspace(0,2*pi);
X = a*cos(t);
Y = b*sin(t);
w = atan2(y2-y1,x2-x1);
x = (x1+x2)/2 + X*cos(w) - Y*sin(w);
y = (y1+y2)/2 + X*sin(w) + Y*cos(w);
plot(x,y,'y-')
axis equal

Walter Roberson

### Walter Roberson (view profile)

on 26 Jul 2018
If you already have the minor axis length then you can use that directly in b. If you do not have the minor axis length then you need to calculate it, and you need the information to calculate it; the main other way of characterizing it is to specify the eccentricity. If you do not know the minor axis length and you do not know the eccentricity, then what do you know?
Kaleesh Bala

### Kaleesh Bala (view profile)

on 27 Jul 2018
ok fine let me put it in parametric type having two foci as x1 y1, x2 y2
determining r1,r2 to get the elliptical form? I think from r1 can get r2.
So how to determine r1.
xt = r1 * cos(t) + xc;
yt = r2 * sin(t) + yc;
Walter Roberson

### Walter Roberson (view profile)

on 27 Jul 2018
The foci are not enough information to determine the ellipse.

Sign in to comment.

Answer by Azzi Abdelmalek

### Azzi Abdelmalek (view profile)

on 8 Sep 2013
Edited by Azzi Abdelmalek

### Azzi Abdelmalek (view profile)

on 12 Jun 2015

a=5; % horizontal radius
b=10; % vertical radius
x0=0; % x0,y0 ellipse centre coordinates
y0=0;
t=-pi:0.01:pi;
x=x0+a*cos(t);
y=y0+b*sin(t);
plot(x,y)

Ivailo Ivanov

### Ivailo Ivanov (view profile)

on 14 Jan 2016
It was very simple and comprehensible.
Cynthia Dickerson

### Cynthia Dickerson (view profile)

on 27 Jun 2018
Thanks! This code worked for me perfectly. :)

Sign in to comment.

Answer by Kate

### Kate (view profile)

on 24 Feb 2014

how would you plot a ellipse with only knowing some co-ordinates on the curve and not knowing the y radius and x radius?

Roger Stafford

### Roger Stafford (view profile)

on 24 Feb 2014
That involves more work, Kate. I'll show you a first, easy step and let you struggle through the rest.
It requires a minimum of five points to uniquely determine a conic section - that is, an ellipse, a parabola, or a hyperbola. One way to approach the problem is to find a set of values A, B, C, D, E, and F such that
A*x^2 + B*x*y + C*y^2 + D*x + E*y + F
is as close to zero as possible for the points (assuming the six constants have been normalized by having the sum of their squares be unity.) With five points it should be essentially zero for each of the points. For more points, it depends on how close they all are to a valid conic section. If they actually lie on some conic, then with the appropriate constants A, B, etc., the above would also be zero at each point.
These constants can be found using matlab's 'svd' singular value decomposition function. Let x be a column vector of all the points' x-coordinates and y a column vector of their corresponding y-coordinates. Do this:
[U,S,V] = svd([x.^2,x.*y,y.^2,x,y,ones(size(x))]);
Then the desired constants will be in the vector V(:,6), namely, A = V(1,6), B = V(2,6), C = V(3,6), D = V(4,6), E = V(5,6), and F = V(6,6). These are associated with the smallest singular value in S. If that value is zero, then there is a precise match with all points.
Probably the easiest way to proceed from here on in plotting the conic section, would be to solve for y as a function of x, (or possibly x as a function of y,) in the quadratic equation
A*x^2 + B*x*y + C*y^2 + D*x + E*y + F = 0
The type of conic section is determined by the discriminant, B^2-4*A*C. If it is negative, the curve is an ellipse, if it is zero, the curve is a parabola, and if it is positive, it is a hyperbola. Recalling high school algebra, there will in general be two y solutions or none for each x, (and two x solutions or none for each y,) using the famous quadratic formula.
Making a successful plot would require a determination of the range of x (or of y) for which there are solutions and then generating a set of x values (or y values,) maybe with linspace, and making two separate plots of the two possible solutions for y (or for x).
Another approach to handling such a quadratic equation involves making the appropriate translation and rotations so as to place the equation in a standard form from which plots can readily be made. This, however, probably involves more effort than the above, though it is more informative.
Image Analyst

### Image Analyst (view profile)

on 25 Feb 2014
Perhaps of some interest, if you need to find the ellipse points: http://www.ecse.rpi.edu/homepages/qji/Papers/ellipse_det_icpr02.pdf
Devi Satya Cheerla

### Devi Satya Cheerla (view profile)

on 12 Jun 2015
in the equation of ellipse X2/a2 + Y2/b2 = 1. knowing the points on ellipse, can find a and b. then enter the code below to mathematically compute y and to plot x,y.
code:
x=(0:.01:a); # x value is from 0 to 'a' and discrete with 0.01 scale#
i=1:(a*100+1); # i is to calculate y at every discrete value. it should be for 1 i.e first x value to the last x value.. as it does not have a zero, add 1#
clear y # to clear any previous y value#
for i=1:(a*100+1)
y(i)=(b^2*(1-(x(i)^2)/a^2))^.5; #from the ellipse equation y=sqrt(b2(1-(x2/a2))#
end
plot(x,y)
hold on
plot(x,-y)
hold on
plot(-x,y)
hold on
plot(-x,-y)

Sign in to comment.

Answer by Omar Maaroof

### Omar Maaroof (view profile)

on 13 May 2019 at 13:17

you can use
Ellipse2d

Walter Roberson

### Walter Roberson (view profile)

on 13 May 2019 at 17:13
MATLAB does not offer Ellipse2d plotting directly. Instead, the Symbolic Toolbox's engine, MuPAD, offers plot::Ellipse2d https://www.mathworks.com/help/symbolic/mupad_ref/plot-ellipse2d.html which can only be used from within a MuPAD notebook . R2018b was intended to be the last release that included the MuPAD notebook, but it was carried on to R2019a as well.

Sign in to comment.