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

5.475 visualizaciones (últimos 30 días)
MathWorks Support Team el 26 de Jul. de 2010
Respondida: Shengchao Lin el 11 de Ag. de 2023
I would like to plot a circle with a given radius and center.

MathWorks Support Team el 23 de Mzo. de 2022
Editada: MathWorks Support Team el 23 de Mzo. de 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:
##### 8 comentariosMostrar 6 comentarios más antiguosOcultar 6 comentarios más antiguos
Image Analyst el 9 de Sept. de 2022
@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).
Walter Roberson el 9 de Sept. de 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.)

Iniciar sesión para comentar.

### Más respuestas (3)

serwan Bamerni el 17 de Feb. de 2016
Editada: MathWorks Support Team el 26 de Mayo de 2023
There is now a function called viscircles():
##### 2 comentariosMostrar NingunoOcultar Ninguno
Walter Roberson el 17 de Oct. de 2016
This is part of the Image Processing Toolbox
Walter Roberson el 25 de Dic. de 2020

Iniciar sesión para comentar.

Steven Lord el 25 de Dic. de 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
##### 2 comentariosMostrar NingunoOcultar Ninguno
Walter Roberson el 9 de Jun. de 2021
Walter Roberson el 9 de Jun. de 2021
Remember that an equilateral triangle has a 60 degree range.

Iniciar sesión para comentar.

Shengchao Lin el 11 de Ag. de 2023
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;
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Polar Plots en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by