How do I plot a circle with a given radius and center?

I would like to plot a circle with a given radius and center.

 Accepted Answer

Here is a MATLAB function that plots a circle with radius 'r' and locates the center at the coordinates 'x' and 'y':
function h = circle(x,y,r) hold on th = 0:pi/50:2*pi; xunit = r * cos(th) + x; yunit = r * sin(th) + y; h = plot(xunit, yunit); hold off
An alternative method is to use the 'rectangle' function:
function h = circle2(x,y,r) d = r*2; px = x-r; py = y-r; h = rectangle('Position',[px py d d],'Curvature',[1,1]); daspect([1,1,1])
If you are using version R2012a or later and have Image Processing Toolbox, then you can use the 'viscircles' function to draw circles:
viscircles(centers,radii)

8 Comments

If you would like to plot a circle given two points [Center, Point on circle], rather than [Center, Radius], you can simply calculate the distance between your two points, and then use that distance as the radius.
Yes, you can just write 0:2*pi
>> 0:2*pi
ans =
0 1 2 3 4 5 6
This would give you 7 different radians values, and will approximate the circle as a heptagon, which might be good enough for your purposes. Each side would cover a bit less than 60 degrees. This will not at all look round to most people.
What does pi/50 mean
There are 2*pi radians in a circle, so using pi/50 as the step means that you would be approximating the circle as (2*pi)/(pi/50) = 100 sides, so each side would be roughly 3.6 degrees. That is enough sides that the circle would look curved to most people.
r * cos(th) is part of polar to cartesian coordinate conversion. For a given theta, for a circle of radius r centered on the origin, then the x coordinate is r multiplied by cos(theta) and the y coordinate is r multiplied by sin(theta). And then you add the actual center of the circle, x and y, to those coordinates.
A more common mathematical notation would be and for a circle of radius r centered on and
r*cos(theta) is used because of the definition of cos. cos(theta) = y/r where r = sqrt(x^2 + y^2) so y = r*cos(theta)
@Yuvin Wickramanayake no. It means that you put that code into a script, maybe it was called testcircle.m and preceding that code you had code to call the function like
h = circle(x, y, r);
At least that's one possibility. If so, you'd need to close your circle() function with the "end" keyword, which will allow a function to be defined and called from within a script.
function h = circle(x,y,r)
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
end
If you still have trouble, attach your whole m-file in a NEW question (not here).
You would get a different error message if you had a script with a function after and the function did not have a matching end statement.
That error could happen if you try to create a function inside a script in a MATLAB version before R2015b. It might perhaps also happen if you try to define a function at the command line (I seem to remember the wording as being slightly different for that case, but perhaps that is the wording in an older version than I am using.)

Sign in to comment.

More Answers (3)

There is now a function called viscircles():

2 Comments

This is part of the Image Processing Toolbox
viscircles(app.segmented, centres, radii, 'color', 'b')

Sign in to comment.

Another possibility is to approximate the circle using a polyshape with a large number of sides and plot that polyshape.
p = nsidedpoly(1000, 'Center', [2 3], 'Radius', 5);
plot(p, 'FaceColor', 'r')
axis equal
Using function "fplot" would be my simplest way:
Unit circle:
fplot(@(t) sin(t), @(t) cos(t));
grid on
Circle with center "x", "y" and radius "r":
x = 3; y = 4; r = 5;
fplot(@(t) r*sin(t)+x, @(t) r*cos(t)+y);
grid on;

Categories

Products

Community Treasure Hunt

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

Start Hunting!