Plot Circle in MATLAB
Solution 1
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).
Solution 2
Method 12
rectangle('Position', [-0.5, -0.5, 1, 1], 'Curvature', [1 1]);
Generalized:
circle = @(x, y, r) rectangle('Position', [x-r, y-r, 2*r, 2*r], ...
'Curvature', [1 1]);
circle(0, 0, 1);
circle(10, 20, 5);
axis equal;
Rashid
I love programming. I like how it boosts my mind and I try to get better by learning from the best members we have here.
Updated on June 15, 2022Comments
-
Rashid almost 2 years
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?)