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.

1 comentario

muhammad  arfan
muhammad arfan el 18 de Jun. de 2019
dears!!!
i have asigned to write a matlab code for 8 point to fit it in ellipse by using least square method..
i am new in using matlab and try my best but my points are not fit on ellipse. i use annealing method so that i have satisfied my teacher by my work. please chk my work and help me.
thanks
arfan khan
clc;
clear all;
close all;
r1 = rand(1);
r2 = [1+rand(1)]; % r2>r1
x0 = 0;
y0 = 0;
N = 8;
n= 100;
x1 = 1;
x2 = 2;
y1 = 1;
y2 = 2;
for i = 1:n
x = x1 +(x2-x1).*rand(N,1);
y = y1 +(y2-y1).*rand(N,1);
f = ((((x./r1).^2) +(y./r2).^2)-1).^2;
[m,l] = min(f);
z =.001* exp(10*(1-i/n));
v = z/2;
% disp('v');
% disp(v)
x1 = x(l)*v;
x2 = x(l)*v;
% disp('x1')
% disp(x1)
% disp('x2')
% disp(x2)
% ay = v./y(l);
% by = v./y(l);
% disp(v);
%
% % hold on;
disp('f');
disp(f);
end
% plot(f,'or')
plot(x,y, '*b');
x=((x(i)-x0)*cos(z)) - ((y(i)-y0)*sin(z))
y=(x(i)-x0)*sin(z)-(y(i)-y0)*cos(z)
xa(i)=rand(1)
x(i)= a+(b-a)*rand(1);
y(i)= rand(1);
for
m(i) = ((((x).^2)/a^2) + (((y).^2)/b^2)-1).^2
end
hold on;

Iniciar sesión para comentar.

 Respuesta aceptada

Roger Stafford
Roger Stafford el 8 de Sept. de 2013
Editada: Cris LaPierre el 5 de Abr. de 2019

9 votos

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

11 comentarios

Dimitris Arapidis
Dimitris Arapidis el 8 de Sept. de 2013
that worked nicely, thank you
Image Analyst
Image Analyst el 8 de Sept. de 2013
Several other ellipse and circle entries are in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_create_an_ellipse.3F - check it out.
Benjamin
Benjamin el 22 de Dic. de 2016
what are the "extra" -y*sin(w) and y*cos(w) terms for in lines 7 and 8?
Roger Stafford
Roger Stafford el 22 de Dic. de 2016
@Benjamin. They are to produce the needed rotation by angle w of the ellipse to the slant of the given major axis with respect to the x-y axes. (The angle t in X and Y is needed to produce the required eccentricity.)
Ramesh Bala
Ramesh Bala el 26 de Jul. de 2018
but in the solution provided above x1 x2 ,y1 y2 are fine. How to get the eccentricity value to get b?
Ramesh Bala
Ramesh Bala el 26 de Jul. de 2018
or one assume it as e→1 ,it tends to a line,
e0 ,it tends to a circle.
Walter Roberson
Walter Roberson el 26 de Jul. de 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?
Ramesh Bala
Ramesh Bala el 27 de Jul. de 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 el 27 de Jul. de 2018
The foci are not enough information to determine the ellipse.
Image Analyst
Image Analyst el 7 de Jul. de 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'
Maite Osaba
Maite Osaba el 25 de Ag. de 2022
Editada: Maite Osaba el 25 de Ag. de 2022
@Image Analyst I found this implementation really useful! Thanks! Do you think there is a way to plot this so the ellipse is filled with color?

Iniciar sesión para comentar.

Más respuestas (5)

Azzi Abdelmalek
Azzi Abdelmalek el 8 de Sept. de 2013
Editada: Azzi Abdelmalek el 12 de Jun. de 2015

30 votos

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)

3 comentarios

Ivailo Ivanov
Ivailo Ivanov el 14 de En. de 2016
It was very simple and comprehensible.
Cynthia Dickerson
Cynthia Dickerson el 27 de Jun. de 2018
Thanks! This code worked for me perfectly. :)
Sandy M
Sandy M el 27 de Jul. de 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

Iniciar sesión para comentar.

Kommi
Kommi el 24 de Nov. de 2022

1 voto

For ellipse
>> clc
clear all
%length of major axis
a = input('please enter the length of major axis: ');
b = input('please enter the length of minor axis: ');
x1 = input('please input the x coordinate of the ellipse: ');
y1 = input('please enter the y coordinate of the ellipse: ');
t = -pi:0.01:pi;
x = x1+(a*cos(t));
y = y1+(b*sin(t));
plot(x,y);
Kate
Kate el 24 de Feb. de 2014

0 votos

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

4 comentarios

Roger Stafford
Roger Stafford el 24 de Feb. de 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 el 25 de Feb. de 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 el 12 de Jun. de 2015
Editada: Walter Roberson el 26 de Ag. de 2022
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
Sandy M el 27 de Jul. de 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

Iniciar sesión para comentar.

Omar Maaroof
Omar Maaroof el 13 de Mayo de 2019

0 votos

you can use
Ellipse2d

1 comentario

Walter Roberson
Walter Roberson el 13 de Mayo de 2019
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.

Iniciar sesión para comentar.

Matt J
Matt J el 24 de Nov. de 2022
Editada: Matt J el 24 de Nov. de 2022

0 votos

Using this FEX download,
[x1,y1,x2,y2, e]=deal(1,2,20,8 ,0.85); %hypothetical input
a = 1/2*sqrt((x2-x1)^2+(y2-y1)^2);
b = a*sqrt(1-e^2);
center=[x1+x2,y1+y2]/2;
theta=atan2d(y2-y1,x2-x1); %rotation angle
obj=ellipticalFit.groundtruth([], center,[a,b], theta);
plot(obj); hold on;
plot([x1,x2],[y1,y2],'xk'); hold off;
axis padded

Preguntada:

el 8 de Sept. de 2013

Editada:

el 24 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by