# Math Help - Matlab - Plotting Circles

1. ## Matlab - Plotting Circles

Hey guys

I have the following problem:

Write a function m-ﬁle, PlotCircles.m, that can accept as input a vector, r,
having positive entries, and plots circles of radius r(i) on the same axis. If no input value for r is speciﬁed, the default is to plot a single circle with radius r = 1. Test your function by using r = 1:5.

Hints: Matlab commands that might be useful for this problem include max, min and any.
My solution so far:

Code:
function PlotCircles(r)

theta = linspace(0,2*pi,1000);

if nargin == 0
r = 1;
end

for i = min(r):max(r)
x = i*cos(theta);
y = i*sin(theta);
plot(x,y)
axis square
axis([-(r+1) (r+1) -(r+1) (r+1)])
end
However when I input a vector as follows:
Code:
EDU>> r = 1:5

r =

1     2     3     4     5

EDU>> PlotCircles(r)
??? Error using ==> axis>LocSetLimits at 236
Vector must have 4, 6, or 8 elements.

Error in ==> axis at 96
LocSetLimits(ax(j),cur_arg);

Error in ==> PlotCircles at 14
axis([-(r+1) (r+1) -(r+1) (r+1)])
And then it seems to draw only the first circle. Any help is appreciated.

2. Originally Posted by janvdl
Hey guys

I have the following problem:

My solution so far:

However when I input a vector as follows:

And then it seems to draw only the first circle. Any help is appreciated.
The simple answer is hold('on')/hold('off'), the following seems to work:

Code:
function rv=PlotCircles(r)
rv=[]
theta = linspace(0,2*pi,100);

if nargin == 0
r = 1;
end

for i = 1:length(r)
x = r(i)*cos(theta);
y = r(i)*sin(theta);
plot(x,y)
axis square
r0=max(r);
axis([-(r0+1) (r0+1) -(r0+1) (r0+1)])
hold('on');
end
hold('off');
CB