Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Plot ellipse

Subject: Plot ellipse

From: Glyn Hudson

Date: 13 Jan, 2009 15:57:02

Message: 1 of 12

Hi everyone,

I'm trying to plot an ellipse in 2D given co-ordinates of the two foci and the distance between a point on the ellipse and the foci. What do you think is the best way to approach this?

Glyn.

Subject: Plot ellipse

From: Sadik

Date: 13 Jan, 2009 17:27:01

Message: 2 of 12

"Glyn Hudson" <g.d.hudson@warwick.ac.uk> wrote in message <gkidke$9f9$1@fred.mathworks.com>...
> Hi everyone,
>
> I'm trying to plot an ellipse in 2D given co-ordinates of the two foci and the distance between a point on the ellipse and the foci. What do you think is the best way to approach this?
>
> Glyn.

Hello Glyn,

You know that the sum of the distances from a given point to the two foci should be the same on the ellipse. From here, you can get the equation of the ellipse. Then, you could find the minimum and the maximum x values. These correspond to the left-most and the right-most points of the ellipse on the plane.

The general formula of an ellips:

(x-x0)^2/a^2+(y-y0)^2/b^2=1

Therefore, you can easily write x in terms of x0, y, y0, a and b. From here, you can easily identify the minimum and the maximum.

Once you do that, let's say you found xmin and xmax to be the extrema, you can let x = xmin:deltaX:xmax;

Now, we can write y in terms of x, just as we did for x and you get y as a function of x, x0, y0, a and b. If you take the positive square-root term, you can plot the upper portion of the ellipse and the negative square-root will yield the lower portion.

Caution: You should chose deltaX quite small so that the points on the ellipse will not look inhomogenous [because they will be, in general].

Hope this helps.

Subject: Plot ellipse

From: Sadik

Date: 13 Jan, 2009 18:11:01

Message: 3 of 12

"Glyn Hudson" <g.d.hudson@warwick.ac.uk> wrote in message <gkidke$9f9$1@fred.mathworks.com>...
> Hi everyone,
>
> I'm trying to plot an ellipse in 2D given co-ordinates of the two foci and the distance between a point on the ellipse and the foci. What do you think is the best way to approach this?
>
> Glyn.

I will make a correction [simplification, explanation, appendix] to my previous reply.

You don't need to write x as a function of y to determine its range. Once you write y as a function of x and the other constants x0, y0, a and b, then you will see that

y = y0 +/- b* sqrt(1-(x-x0)^2/a^2) ......................... (*)

Since we don't want any complex value for y, we would require the term in the square root be non-negative. For which range of x is it non-negative?

x0-sqrt(a) <= x <= x0+sqrt(a)

So you have your xmin = x0-sqrt(a) and xmax x0 + sqrt(a) .

The rest is the same as before. After you have your vector of values for x, you substitute it in (*) above, once for y0 + ... to get the vector yUpper, and once for y0 - ... to get the vector yLower. Then, as you already know,

plot(x,yUpper)
hold on
plot(x,yLower)

Of course, all these describe the case when the major [or minor] axis is parallel to the x-axis. In the oblique case, however, you can follow the similar "we don't want complex" approach.

Hope this helps.

Subject: Plot ellipse

From: Matt Fig

Date: 13 Jan, 2009 18:23:01

Message: 4 of 12

Have you looked on the file exchange?
If that doesn't pan out, please refer to the picture at the top here:

http://mathworld.wolfram.com/Ellipse.html

and tell us what exactly you are given, i.e., both r1 and r2 or (r1 + r2) or what.

Subject: Plot ellipse

From: Matt Fig

Date: 13 Jan, 2009 18:38:02

Message: 5 of 12

And also, given focus coords [x1,x2] and [y1,y2], is it necessarily true that:

(x1==x2 || y1==y2)

or can the ellipse be rotated? Also, what have you done so far?

Subject: Plot ellipse

From: Roger Stafford

Date: 13 Jan, 2009 22:59:02

Message: 6 of 12

"Glyn Hudson" <g.d.hudson@warwick.ac.uk> wrote in message <gkidke$9f9$1@fred.mathworks.com>...
> Hi everyone,
>
> I'm trying to plot an ellipse in 2D given co-ordinates of the two foci and the distance between a point on the ellipse and the foci. What do you think is the best way to approach this?
>
> Glyn.

  Let the two foci have coordinates (x1,y1) and (x2,y2) and d be the given sum of the two distances from any point (x,y) on the ellipse to the two respective foci. Then do the following:

 c = 1/2*sqrt((x2-x1)^2+(y2-y1)^2);
 a = d/2;
 b = sqrt(a^2-c^2);
 t = linspace(0,2*pi,1000);
 x = (x1+x2)/2 +(x2-x1)/(2*c)*a*cos(t) - (y2-y1)/(2*c)*b*sin(t);
 y = (y1+y2)/2 +(y2-y1)/(2*c)*a*cos(t) +(x2-x1)/(2*c)*b*sin(t);
 plot(x,y)
 axis equal

  This should give you the desired ellipse. As the parameter t varies from 0 to 2*pi the ellipse will be traced from a point on the opposite side of (x2,y2) from (x1,y1) around the ellipse in a counterclockwise direction until it returns to the start point. The quantities a and b are respectively the major and minor semi-axis lengths of the ellipse. Note that we must have d >= 2*c to have a valid ellipse.

  I'll let you figure out the reasoning behind all this. It just involves knowing a few facts about ellipses together with a translation and rotation of coordinates to put the ellipse in standard form.

Roger Stafford

Subject: Plot ellipse

From: Glyn Hudson

Date: 14 Jan, 2009 15:01:18

Message: 7 of 12

"Matt Fig" <spamanon@yahoo.com> wrote in message <gkim65$jb9$1@fred.mathworks.com>...
> Have you looked on the file exchange?
> If that doesn't pan out, please refer to the picture at the top here:
>
> http://mathworld.wolfram.com/Ellipse.html
>
> and tell us what exactly you are given, i.e., both r1 and r2 or (r1 + r2) or what.

Im given (r1 + r2)

Its the total reflection distance between two transducers (the foci) and an object (the point on the ellispe)

Subject: Plot ellipse

From: Glyn Hudson

Date: 14 Jan, 2009 15:05:04

Message: 8 of 12

"Matt Fig" <spamanon@yahoo.com> wrote in message <gkin2a$ikg$1@fred.mathworks.com>...
> And also, given focus coords [x1,x2] and [y1,y2], is it necessarily true that:
>
> (x1==x2 || y1==y2)
>
> or can the ellipse be rotated? Also, what have you done so far?

No it is not always true that (x1==x2 || y1==y2) since the elipse will be rotated. i.e one of the ultrasound transducers (one of the foci) will be moved to simmulate an array of transducers.

So far I have ploted an elipse given lenghths the major and minor axis and rotated the elipse about its center

Subject: Plot ellipse

From: Glyn Hudson

Date: 14 Jan, 2009 15:47:01

Message: 9 of 12

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gkj6bm$ms7$1@fred.mathworks.com>...
> "Glyn Hudson" <g.d.hudson@warwick.ac.uk> wrote in message <gkidke$9f9$1@fred.mathworks.com>...
> > Hi everyone,
> >
> > I'm trying to plot an ellipse in 2D given co-ordinates of the two foci and the distance between a point on the ellipse and the foci. What do you think is the best way to approach this?
> >
> > Glyn.
>
> Let the two foci have coordinates (x1,y1) and (x2,y2) and d be the given sum of the two distances from any point (x,y) on the ellipse to the two respective foci. Then do the following:
>
> c = 1/2*sqrt((x2-x1)^2+(y2-y1)^2);
> a = d/2;
> b = sqrt(a^2-c^2);
> t = linspace(0,2*pi,1000);
> x = (x1+x2)/2 +(x2-x1)/(2*c)*a*cos(t) - (y2-y1)/(2*c)*b*sin(t);
> y = (y1+y2)/2 +(y2-y1)/(2*c)*a*cos(t) +(x2-x1)/(2*c)*b*sin(t);
> plot(x,y)
> axis equal
>
> This should give you the desired ellipse. As the parameter t varies from 0 to 2*pi the ellipse will be traced from a point on the opposite side of (x2,y2) from (x1,y1) around the ellipse in a counterclockwise direction until it returns to the start point. The quantities a and b are respectively the major and minor semi-axis lengths of the ellipse. Note that we must have d >= 2*c to have a valid ellipse.
>
> I'll let you figure out the reasoning behind all this. It just involves knowing a few facts about ellipses together with a translation and rotation of coordinates to put the ellipse in standard form.
>
> Roger Stafford

Thanks for your fantastic post, very helpfull.

I have just plotted an elipse using your method, all looks good appart from the foci which seem to be in the wrong place. Here is a photo of the plot

x1=2 y2=1
x2=5 y2=5
d=7

http://picasaweb.google.co.uk/lh/photo/-xLzLXylUvPHEokBINbt5g?feat=directlink

What do you think?

Subject: Plot ellipse

From: Roger Stafford

Date: 14 Jan, 2009 16:43:02

Message: 10 of 12

"Glyn Hudson" <g.d.hudson@warwick.ac.uk> wrote in message <gkl1dl$mim$1@fred.mathworks.com>...
> .......
> I have just plotted an elipse using your method, all looks good appart from the foci which seem to be in the wrong place. Here is a photo of the plot
> ......

  I looked at the ellipse in your website. As nearly as my poor eyes can make out, the unit tick marks along the x-axis are 25/32 of an inch apart on my screen while the units along the y-axis are only 18/32" apart. This kind of skewness can produce the effect you are seeing where the foci look out of place. I get the same phenomenon on my ellipse graphs unless I do the "axis equal" instruction. You should make sure your x and y units are scaled to be precisely equal.

  In any case, there is a far better way of directly testing your numerical results by adding the various sums of distances from the two given points and seeing if that sum remains essentially constant throughout the ellipse. Such a constancy can be true for only two points, namely the two true foci of the ellipse.

  I used

 max(abs(sqrt((x-x1).^2+(y-y1).^2)+sqrt((x-x2).^2+(y-y2).^2)-d))

as a test before sending the code to you and it was always very small, down around the 14th or 15th decimal place.

Roger Stafford
 

Subject: Plot ellipse

From: Glyn Hudson

Date: 14 Jan, 2009 21:29:02

Message: 11 of 12

Yeah your totally right. Thanks a lot, much appreciated.

Glyn.

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gkl4mm$apq$1@fred.mathworks.com>...
> "Glyn Hudson" <g.d.hudson@warwick.ac.uk> wrote in message <gkl1dl$mim$1@fred.mathworks.com>...
> > .......
> > I have just plotted an elipse using your method, all looks good appart from the foci which seem to be in the wrong place. Here is a photo of the plot
> > ......
>
> I looked at the ellipse in your website. As nearly as my poor eyes can make out, the unit tick marks along the x-axis are 25/32 of an inch apart on my screen while the units along the y-axis are only 18/32" apart. This kind of skewness can produce the effect you are seeing where the foci look out of place. I get the same phenomenon on my ellipse graphs unless I do the "axis equal" instruction. You should make sure your x and y units are scaled to be precisely equal.
>
> In any case, there is a far better way of directly testing your numerical results by adding the various sums of distances from the two given points and seeing if that sum remains essentially constant throughout the ellipse. Such a constancy can be true for only two points, namely the two true foci of the ellipse.
>
> I used
>
> max(abs(sqrt((x-x1).^2+(y-y1).^2)+sqrt((x-x2).^2+(y-y2).^2)-d))
>
> as a test before sending the code to you and it was always very small, down around the 14th or 15th decimal place.
>
> Roger Stafford
>

Subject: Plot ellipse

From: Mahdiye

Date: 27 Jul, 2011 09:33:10

Message: 12 of 12

"Glyn Hudson" <g.d.hudson@warwick.ac.uk> wrote in message <gkidke$9f9$1@fred.mathworks.com>...
> Hi everyone,
>
> I'm trying to plot an ellipse in 2D given co-ordinates of the two foci and the distance between a point on the ellipse and the foci. What do you think is the best way to approach this?
>
> Glyn.
hi dear
you can use the "pdeellip" for plot in 2dellipse.
good luck

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us