Rendering an ellipse in polar coordinates that is not circumscribing the origin

18 views (last 30 days)
I need to render ellipses in a polar coordinate plot. I found on the Wikipedia entry for Ellipse the equation for the general polar form, which I implemented as a Matlab function. It is listed below. The function works well until values of r0 and theta0 cause the ellipse to no longer circumscribe the origin. At that point, equation Q generates complex numbers, which does not render properly using Matlab's polar plotting function. Is there an equation for the general polar form of an ellipse that does not have this restriction?
Thanks,
Mac
function [radii, thetas] = PolarEllipse(a, b, r0, theta0, phi, steps)
% This code only works when the polar coordinate (r0,theta0) falls within
% the ellipse. Otherwise, complex numbers are generated in the equation
% for Q.
thetas = -pi:pi/steps:pi;
P = r0 * (((b^2 - a^2) * cos(thetas + theta0 - 2 * phi)) + ...
((a^2 + b^2) * cos(thetas - theta0)));
R = (b^2 - a^2) * cos(2 * thetas - 2 * phi) + a^2 + b^2;
Q = sqrt(2) * a * b .* sqrt(R - (2 * r0^2 .* sin(thetas - theta0).^ 2));
radii = (P + Q) ./ R;

Answers (1)

Roger Stafford
Roger Stafford on 24 Jul 2013
Just generate the ellipse parametrically in cartesian coordinates and transform these to polar coordinates. That way it won't matter where the origin is - inside or outside the ellipse.
t = linspace(-pi,pi,n); % n values of the parameter
X = a*cos(t); % The ellipse in standard form
Y = b*sin(t);
x = r0*cos(theta0)+X*cos(phi)-Y*sin(phi); % Rotate & translate
y = r0*sin(theta0)+X*sin(phi)+Y*cos(phi);
radii = sqrt(x.^2+y.^2); % Transform to polar coordinates
thetas = atan2(y,x);
The quantities 'radii' and 'thetas' are your polar coordinates.
  1 Comment
Mac
Mac on 24 Jul 2013
Edited: Mac on 24 Jul 2013
Well, duh.... Thanks! I was actually wondering if there was an alternate equation that was purely in terms of polar coordinates, but this gets the job done as far as plotting the ellipse.
Now, if I could just get the polar plot function to display disjoint data sets properly within the same figure. I would have thought that the hold function would handle this correctly. When I use hold, a cartesian coordinate graph layout is displayed immediately. I want a polar coordinate graph layout, but it is not displayed when the polar plot function is executed multiple times. Only the plotted data is displayed within the cartesian coordinate graph layout.

Sign in to comment.

Categories

Find more on Polar Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!