MATLAB Answers

How to plot an Ellipse

2,716 ビュー (過去 30 日間)
Dimitris Arapidis
Dimitris Arapidis 2013 年 9 月 8 日
コメント済み: Sandy M 2019 年 7 月 27 日
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 件のコメント

muhammad  arfan
muhammad arfan 2019 年 6 月 18 日
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;

サインイン to comment.

採用された回答

Roger Stafford
Roger Stafford 2013 年 9 月 8 日
編集済み: Cris LaPierre 2019 年 4 月 5 日
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

  9 件のコメント

表示 6 件の古いコメント
Walter Roberson
Walter Roberson 2018 年 7 月 26 日
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 2018 年 7 月 27 日
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 2018 年 7 月 27 日
The foci are not enough information to determine the ellipse.

サインイン to comment.

その他の回答 (3 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 8 日
編集済み: Azzi Abdelmalek 2015 年 6 月 12 日
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 件のコメント

Ivailo Ivanov
Ivailo Ivanov 2016 年 1 月 14 日
It was very simple and comprehensible.
Cynthia Dickerson
Cynthia Dickerson 2018 年 6 月 27 日
Thanks! This code worked for me perfectly. :)
Sandy M
Sandy M 2019 年 7 月 27 日
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

サインイン to comment.


Kate
Kate 2014 年 2 月 24 日
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 件のコメント

表示 1 件の古いコメント
Image Analyst
Image Analyst 2014 年 2 月 25 日
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 2015 年 6 月 12 日
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 2019 年 7 月 27 日
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

サインイン to comment.


Omar Maaroof
Omar Maaroof 2019 年 5 月 13 日
you can use
Ellipse2d

  1 件のコメント

Walter Roberson
Walter Roberson 2019 年 5 月 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.

サインイン to comment.

サインイン してこの質問に回答します。


Translated by