Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Function to plot circle !!!!

Subject: Function to plot circle !!!!

From: Animesh Pandey

Date: 20 Jan, 2011 20:10:07

Message: 1 of 11

I wish to write a function [x, y] = getCircle (center,r) to get the x and y
coordinates of a circle.
The circle will be centered at center (2-element vector containing the x and y values of the center) and will have radius r.
x and y, have to be returned such that plot(x, y) will plot the circle.

I have used the polar equation for the circle.

function [x,y] = getCircle(center, r)
for i=0 : 0.001 : 2*pi;
    x = r*cos(i) + center(1);
    y = r*sin(i) + center(2);
end

I do not know how to call this function......so that output values [x,y] can be used to plot the circle on the command window.
If I wish to use the Cartesian equation....
(x-a)^2 - (y-b)^2 = r^2, how am I supposed to write the function ?

Please help !!!!!

Subject: Function to plot circle !!!!

From: ImageAnalyst

Date: 20 Jan, 2011 20:14:13

Message: 2 of 11

On Jan 20, 3:10 pm, "Animesh Pandey" <apanimesh...@gmail.com> wrote:
[snip]
> Please help !!!!!
-------------------------------------------------------------------------

Did you see the FAQ!!!!!
http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F

Subject: Function to plot circle !!!!

From: Animesh Pandey

Date: 21 Jan, 2011 06:46:04

Message: 3 of 11

Sir, I read the FAQ but that wasn't what what I needed.
I am basically having a problem in calling the function I mentioned before.
I am not able to use the output values [x,y] to plot the circle from the command window.

Subject: Function to plot circle !!!!

From: Paulo Silva

Date: 21 Jan, 2011 07:02:04

Message: 4 of 11

"Animesh Pandey" wrote in message <ihba3c$1fh$1@fred.mathworks.com>...
> Sir, I read the FAQ but that wasn't what what I needed.
> I am basically having a problem in calling the function I mentioned before.
> I am not able to use the output values [x,y] to plot the circle from the command window.

function [x,y] = getCircle(center, r)
%quick and dirty fix
for i=1:1000
    x(i) = r*cos(i) + center(1);
    y(i) = r*sin(i) + center(2);
end

Subject: Function to plot circle !!!!

From: Paulo Silva

Date: 21 Jan, 2011 07:11:04

Message: 5 of 11

function [x,y] = getCircle(center, r)
%better results than before, find out why
for i=1:1000
    x(i) = r*cos(i/100) + center(1);
    y(i) = r*sin(i/100) + center(2);
end

Subject: Function to plot circle !!!!

From: Yumnam Kirani

Date: 21 Jan, 2011 08:39:06

Message: 6 of 11

"Paulo Silva" wrote in message <ihbbi8$5qg$1@fred.mathworks.com>...
> function [x,y] = getCircle(center, r)
> %better results than before, find out why
> for i=1:1000
> x(i) = r*cos(i/100) + center(1);
> y(i) = r*sin(i/100) + center(2);
> end

r=10; % radius
center=[20,30]; % center cordinates
t=-pi:0.001:pi;
x=r*cos(t)+center(1);
y=r*sin(t)+center(2);

plot(x,y), axis square

Yumnam Kirani Singh
Trongalobi Awang Leikai

Subject: Function to plot circle !!!!

From: Animesh Pandey

Date: 21 Jan, 2011 12:42:04

Message: 7 of 11

"Paulo Silva" wrote in message <ihbbi8$5qg$1@fred.mathworks.com>...
> function [x,y] = getCircle(center, r)
> %better results than before, find out why
> for i=1:1000
> x(i) = r*cos(i/100) + center(1);
> y(i) = r*sin(i/100) + center(2);
> end

This gave a better plot....thanks !!!!
I want to know how should I call this function so that it plots the circle
'cause if I write

hold on
grid on
plot(x , y ,'--r'), axis square

it plots only one point !!!!!
Can you help me with this !!!!

Subject: Function to plot circle !!!!

From: Animesh Pandey

Date: 21 Jan, 2011 12:44:04

Message: 8 of 11

"Yumnam Kirani" <kirani.singh@gmail.com> wrote in message <ihbgna$4po$1@fred.mathworks.com>...
>
> plot(x,y), axis square
>
This command gave the 'circular' plot.....thankyou....
I was getting an elliptical plot before !!!! :D

Subject: Function to plot circle !!!!

From: Steven_Lord

Date: 21 Jan, 2011 14:31:09

Message: 9 of 11



"Animesh Pandey" <apanimesh061@gmail.com> wrote in message
news:ihba3c$1fh$1@fred.mathworks.com...
> Sir, I read the FAQ but that wasn't what what I needed. I am basically
> having a problem in calling the function I mentioned before.
> I am not able to use the output values [x,y] to plot the circle from the
> command window.

theta = 0:0.01:2*pi;
r = 5;
x = r*cos(theta);
y = r*sin(theta);
plot(x, y)
axis square

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Function to plot circle !!!!

From: ImageAnalyst

Date: 21 Jan, 2011 16:39:00

Message: 10 of 11

On Jan 21, 9:31 am, "Steven_Lord" <sl...@mathworks.com> wrote:

> theta = 0:0.01:2*pi;
> r = 5;
> x = r*cos(theta);
> y = r*sin(theta);
> plot(x, y)
> axis square
>
-------------------------------------------------------------
On Jan 21, 7:44 am, "Animesh Pandey" <apanimesh...@gmail.com> wrote:
> > plot(x,y), axis square
>
> This command gave the 'circular' plot.....thankyou....
> I was getting an elliptical plot before !!!! :D
--------------------------------------------------------------------------------------

Note: "axis square" does not guarantee a circle on your monitor. For
example I just ran that on my widescreen notebook and it definitely
looks elliptical. It also depends on the video adapter settings.
With different adapter settings, it could look square.

Subject: Function to plot circle !!!!

From: TideMan

Date: 21 Jan, 2011 20:07:19

Message: 11 of 11

On Jan 22, 5:39 am, ImageAnalyst <imageanal...@mailinator.com> wrote:
> On Jan 21, 9:31 am, "Steven_Lord" <sl...@mathworks.com> wrote:
>
> > theta = 0:0.01:2*pi;
> > r = 5;
> > x = r*cos(theta);
> > y = r*sin(theta);
> > plot(x, y)
> > axis square
>
> -------------------------------------------------------------
> On Jan 21, 7:44 am, "Animesh Pandey" <apanimesh...@gmail.com> wrote:> > plot(x,y), axis square
>
> > This command gave the 'circular' plot.....thankyou....
> > I was getting an elliptical plot before !!!! :D
>
> --------------------------------------------------------------------------------------
>
> Note: "axis square" does not guarantee a circle on your monitor.  For
> example I just ran that on my widescreen notebook and it definitely
> looks elliptical.  It also depends on the video adapter settings.
> With different adapter settings, it could look square.

Yes, I always use:
set(gca,...
    'DataAspectRatio',[1 1 1])

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us