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

8,984 views (last 30 days)
I would like to plot a circle with a given radius and center.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 23 Mar 2022
Edited: MathWorks Support Team on 23 Mar 2022
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)
  18 Comments
Walter Roberson
Walter Roberson on 9 Sep 2022
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 (9)

serwan Bamerni
serwan Bamerni on 17 Feb 2016
  3 Comments
Walter Roberson
Walter Roberson on 25 Dec 2020
viscircles(app.segmented, centres, radii, 'color', 'b')

Sign in to comment.


Steven Lord
Steven Lord on 25 Dec 2020
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
  3 Comments
Walter Roberson
Walter Roberson on 9 Jun 2021
Remember that an equilateral triangle has a 60 degree range.

Sign in to comment.


Supoj Choachaicharoenkul
Supoj Choachaicharoenkul on 2 Oct 2019
plot(x, y, 'bo', 'MarkerSize', 50);
  2 Comments
Walter Roberson
Walter Roberson on 5 Feb 2022
Depending which graphics driver you are using, when you ask for a circle marker drawn large, the result might not look circular. The drivers approximate a circle but they do not generally take into consideration the size of the circle when doing the approximation so it might look bad.

Sign in to comment.


amine bouabid
amine bouabid on 23 Jul 2018
Edited: amine bouabid on 23 Jul 2018
hello
you can plot a circle simply by writing :
syms x; syms y;
ezplot((x-xi).^2+(y-yi).^2-r.^2)
where xi and yi are the coordinates of the center and r is the radius
  2 Comments
Walter Roberson
Walter Roberson on 9 May 2021
Using viscircles() or using plot() with a 'o' marker and large 'MarkerSize' is even shorter.

Sign in to comment.


Ebrahim Soujeri
Ebrahim Soujeri on 26 Mar 2021
The shortest code for it could be this:
function plotcircle(r,x,y)
th = 0:pi/100:2*pi;
f = r * exp(j*th) + x+j*y;
plot(real(f), imag(f));
  3 Comments
Walter Roberson
Walter Roberson on 27 Mar 2021
Notice though that I used the shortcut of plotting a single variable instead of real() and imag() of the expression. This is a "feature" of plot: if you ask to plot() a single variable and the variable is complex valued, then it uses the real component as x and the imaginary component as y. Removing the temporary variables made the code more compact, but the change to plot() only a single expression is using a different algorithm than what you used.
.. and you did say "the shortest", but my version of your approach is shorter ;-)

Sign in to comment.


PATRICIA AGUILAR
PATRICIA AGUILAR on 4 May 2021
An object moves on a circle of radius 1. Plot this circle and place a point at an angle of 67º. Help me
  1 Comment
Walter Roberson
Walter Roberson on 4 May 2021
For 67 degrees, notice that in
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
you could use cosd() and sind() if your angle were in degrees.

Sign in to comment.


Devin Marcheselli
Devin Marcheselli on 17 Jan 2020
how do i plot a circle using the equation: (x-h).^2+(y-k).^2 = r.^2
  3 Comments
Mark Rzewnicki
Mark Rzewnicki on 17 Mar 2020
Sadly I just saw this now, sorry.
The easiest way to do this would have been to write the original code twice (renaming the variables the second time) and plot both circles using a "hold on" statement.
This makes the code look brutally ugly - you really should vectorize things and define functions when scaling up code like this - but it will get the job done in a pinch. The result would look something like this (5-minute edit of my original code):
% Circle equation: (x-h)^2 + (y-k)^2 = r^2
% Center: (h,k) Radius: r
h = 1;
k = 1;
r = 1;
h1 = 2;
k1 = 2;
r1 = 2;
%% In x-coordinates, the circle "starts" at h-r & "ends" at h+r
%% x_res = resolution spacing between points
xmin = h - r;
xmax = h + r;
x_res = 1e-3;
X = xmin:x_res:xmax;
xmin1 = h1 - r1;
xmax1 = h1 + r1;
X1 = xmin1:x_res:xmax1;
%% There are 2 y-coordinates on the circle for most x-coordinates.
%% We need to duplicate every x-coordinate so we can match each x with
%% its pair of y-values.
%% Method chosen: repeat the x-coordinates as the circle "wraps around"
%% e.g.: x = [0 0.1 0.2 ... end end ... 0.2 0.1 0]
N = length(X);
x = [X flip(X)];
N1 = length(X1);
x1 = [X1 flip(X1)];
%% ytemp1: vector of y-values as we sweep along the circle left-to-right
%% ytemp2: vector of y-values as we sweep along the circle right-to-left
%% Whether we take positive or negative values first is arbitrary
ytemp1 = zeros(1,N);
ytemp2 = zeros(1,N);
ytemp11 = zeros(1,N1);
ytemp22 = zeros(1,N1);
for i = 1:1:N
square = sqrt(r^2 - X(i)^2 + 2*X(i)*h - h^2);
ytemp1(i) = k - square;
ytemp2(N+1-i) = k + square;
end
for i = 1:1:N1
square1 = sqrt(r1^2-X1(i)^2 + 2*X1(i)*h1 - h1^2);
ytemp11(i) = k1 - square1;
ytemp22(i) = k1 + square1;
end
y = [ytemp1 ytemp2];
y1 = [ytemp11 ytemp22];
%% plot the (x,y) points
figure(1)
plot(x,y)
hold on
plot(x1,y1)
axis([-5 5 -5 5]);

Sign in to comment.


ali yaman
ali yaman on 14 Apr 2022
Edited: ali yaman on 14 Apr 2022
I think there is no need to any of the above solutions, you can draw a circle by one of the following codes.
Let's say radius is 5
fimplicit(@(x,y) x^2+y^2-25)
ezplot('x^2+y^2-25',[-5,5])
% Note that the 25 in the codes comes from the square of 5. If you want
% draw a circle with 8 radius then write 64 instead of 25.
  4 Comments
ali yaman
ali yaman on 19 Apr 2022
Thanks @Walter Roberson. I did not know the default drawing area for fimplicit is [-5 5].

Sign in to comment.


Sam Zebrado
Sam Zebrado on 12 Jun 2022
Edited: Sam Zebrado on 12 Jun 2022
You can use a one-line function like this if you need to plot circles on unequal axis but do not what them to look like ellipses:
%% a function to plot using different radius
fc_circle_plot = @(xs,ys,rs,varargin)...
arrayfun(@(x,y,r)plot(x,y,'o','MarkerSize',r,varargin{:}),xs,ys,rs,'UniformOutput',false);
%% one demo
x = 1:5;
y = rand(1,5);
radius = randi([10,20],1,5);% random integer from 10 to 20, with a size of [1,5]
figure;
hold on;
fc_circle_plot(x,y,radius,...
'Color','b','LineWidth',2 ... any parameters supported by plot() could be placed here
);
hold off;
p.s. You can also replace function plot as other functions for other usages.
  3 Comments
DGM
DGM on 6 Mar 2023 at 20:48
%% a function to plot using different radius
fc_circle_plot = @(xs,ys,rs,varargin)...
arrayfun(@(x,y,r)plot(x,y,'o','MarkerSize',r,varargin{:}),xs,ys,rs,'UniformOutput',false);
% some data
x = 1:5;
y = rand(1,5);
radius = randi([10,20],1,5);% random integer from 10 to 20, with a size of [1,5]
% say there's a line plot in the figure to begin with
plot(x,y) % i'm going to reuse the same points
% then you can use hold to plot other things
hold on;
fc_circle_plot(x,y,radius,...
'Color','b','LineWidth',2 ... any parameters supported by plot() could be placed here
);
hold off;

Sign in to comment.

Categories

Find more on Polar Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!