Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot Circle in MATLAB

I was asked to find different ways to plot a circle in MATLAB,

It seems boring. However I could come up with some ideas (some might be inefficient!),

Method 1

ezpolar(@(x)1);

Method 2

t = linspace(0,2*pi,100);
plot(sin(t),cos(t));

Method 3

[X,Y,~] = cylinder(1,100);
plot(X(1,:),Y(1,:));

Method 4

ezplot('x^2 + y^2 - 1');

Method 5

theta = linspace(0,2*pi,100);
ro = ones(1,100);
[X,Y] = pol2cart(theta,ro);
plot(X,Y);

and it got interesting.

I'm curious if you have other ideas.

Thanks.

Edit

Method 11

azimuth = linspace(-pi,pi,100);
r = ones(1,100);
elevation = zeros(1,100);
[X,Y,Z] = sph2cart(azimuth,elevation,r);
patch(X,Y,Z)
%% (not sure how it works! any improvement suggestions?)
like image 908
Rashid Avatar asked Nov 02 '14 19:11

Rashid


1 Answers

If you're going to go to polar coordinates, then there's also

Method 6

theta = linspace(0,2*pi,100);
rho = ones(1,100);
polar(theta, rho)

Method 7

ezpolar('1') % Shortest?


You can also take advantage complex number and how they're handled by plot:

Method 8

theta = linspace(0,2*pi,100);
rho = ones(1,100);
z = rho.*exp(1i*theta);
plot(z)

The above could be done on one line. It could also be plotted as:

plot(real(z),imag(z))

Method 9

plot(0,0,'o','MarkerSize',100)

Method 10

text(0,0,'\circ','FontSize',200)

Many other unicode characters can be used to produce circles.


You could extend this to generating circles with differential equations, e.g., circular orbits and circular limit cycles (Hopf oscillator).

like image 125
horchler Avatar answered Oct 01 '22 19:10

horchler