2.222 views (last 30 days)

Roger Stafford
on 8 Sep 2013

Edited: Cris LaPierre
on 5 Apr 2019

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

Kaleesh Bala
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
on 27 Jul 2018

The foci are not enough information to determine the ellipse.

Image Analyst
on 7 Jul 2020

Here's a full demo:

% Define parameters.

fontSize = 15;

x1 = 1;

x2 = 20;

y1 = 2;

y2 = 8;

eccentricity = 0.85;

numPoints = 300; % Less for a coarser ellipse, more for a finer resolution.

% Make equations:

a = (1/2) * sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2);

b = a * sqrt(1-eccentricity^2);

t = linspace(0, 2 * pi, numPoints); % Absolute angle parameter

X = a * cos(t);

Y = b * sin(t);

% Compute angles relative to (x1, y1).

angles = atan2(y2 - y1, x2 - x1);

x = (x1 + x2) / 2 + X * cos(angles) - Y * sin(angles);

y = (y1 + y2) / 2 + X * sin(angles) + Y * cos(angles);

% Plot the ellipse as a blue curve.

subplot(2, 1, 1);

plot(x,y,'b-', 'LineWidth', 2); % Plot ellipse

grid on;

axis equal

% Plot the two vertices with a red spot:

hold on;

plot(x1, y1, 'r.', 'MarkerSize', 25);

plot(x2, y2, 'r.', 'MarkerSize', 25);

caption = sprintf('Ellipse with vertices at (%.1f, %.1f) and (%.1f, %.1f)', x1, y1, x2, y2);

title(caption, 'FontSize', fontSize);

xlabel('x', 'FontSize', fontSize);

ylabel('y', 'FontSize', fontSize);

% Plot the x and y. x in blue and y in red.

subplot(2, 1, 2);

plot(t, x, 'b-', 'LineWidth', 2);

grid on;

hold on;

plot(t, y, 'r-', 'LineWidth', 2);

legend('x', 'y', 'Location', 'north');

title('x and y vs. t', 'FontSize', fontSize);

xlabel('t', 'FontSize', fontSize);

ylabel('x or y', 'FontSize', fontSize);

% Set up figure

g = gcf;

g.WindowState = 'maximized';

g.NumberTitle = 'off';

g.Name = 'Ellipse Demo by Roger Stafford and Image Analyst'

Azzi Abdelmalek
on 8 Sep 2013

Edited: Azzi Abdelmalek
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)

Sandy M
on 27 Jul 2019

Hi, I do my ellipse graph

A=10;

B=7.5;

X=-10:.1:10;

Y=(7.5/10)*(1-x^2)^(1/2)

z=-(7.5/10)*(1-x^2)^(1/2)

Plot(x,y,x,z)

Its ok but i need it in cm units cause if i change properties of figure and paper to cm i get deference’s about 3 or 5 mm How can I justify the unit

Kate
on 24 Feb 2014

Image Analyst
on 25 Feb 2014

Devi Satya Cheerla
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)

Sandy M
on 27 Jul 2019

hi why u product the nmber with 100?

and, if i want the graph with cm units, what i do? cause i change garaph and paper properties but i still defreces about 4 mm when i prented it

Omar Maaroof
on 13 May 2019

you can use

Ellipse2d

Walter Roberson
on 13 May 2019

Opportunities for recent engineering grads.

Apply TodayFind the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 1 Comment

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/86615-how-to-plot-an-ellipse#comment_715864

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/86615-how-to-plot-an-ellipse#comment_715864

Sign in to comment.