How can I correct my function?

I am trying to make a function called getCircle that takes the inputs (center, radius)
my code looks like this:
function [x,y] = getCircle(center,radius)
p = 0:0.01:2*pi
x = cos(p)* radius + center(1)
y = sin(p)* radius + center(2)
end
when I try and run the function it tells me that i don't have enough input arguments.
This is the assignment, for clarification:
Write the function getCircle which is called as [x,y]=getCircle(center,radius) to get the x and y coordinates of points that fall on a circle. The circle should be centered at center (2-element vector containing the x and y values of the center) and have the provided radius. The function should return x and y such that a call to plot(x,y) will plot the circle. Note that going around a circle centered on the origin is going around from angle t = 0 to 2. In this case the x and y coordinates on a unit circle centered on the origin of radius 1 are x(t ) = cos (t )and y(t) = sin(t). The points can be scaled to the correct radius by multiplying by radius. The point can translated to have a center other than the origin by adding the center to each point. Write a program that uses getCircle to draw circles and makes a plot with 3 circles of radius 1,3,5 centered on (6,6) and 3 circles of radius 1,3,5 centered on (-6,6). The circles of radius 1,3, and 5 should be of different colors, and should use the same color for each value of radius in the two sets of circles.

1 comentario

Jan
Jan el 15 de Feb. de 2018
Then please tell us, how you call this function. Do you click on the green triangle in the editor?

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 15 de Feb. de 2018

0 votos

Call it from another script or function or from the command window:
center = 23;
radius = 5;
[x, y] = getCircle(center,radius)

6 comentarios

Emma Pineiro
Emma Pineiro el 15 de Feb. de 2018
the center is the origin of a circle. I need the function to be exactly [x,y] = getCircle(center,radius) because that is my assignment, but i have to use the function to graph 3 circles at the center point (6,6) with different radii and then 3 circles at the center point (-6,6). I want to be able to use the function getCircle to give me x and y values, so that when is plot(x,y) it gives me the circle.
center = [6,6];
radius = 5;
[x, y] = getCircle(center,radius);
plot(x, y);
Emma Pineiro
Emma Pineiro el 15 de Feb. de 2018
Yes, that's the correct code, but it wont run the function getCircle.
I put the code
center = [6,6];
radius = 5;
[x, y] = getCircle(center,radius);
plot(x, y);
into the file plotcircle.m . I put the code
function [x,y] = getCircle(center,radius)
p = 0:0.01:2*pi
x = cos(p)* radius + center(1)
y = sin(p)* radius + center(2)
end
into getCircle.m .
I ran plotcircle . There was no error message produced, and the plot looked appropriate.
If you happen to be getting an oval output instead of a visual circle, it is because plot() uses the aspect ratio of the axes it is contained in and your axes is likely wider than it is tall. In such a case to fix the aspect ratio as 1:1 you would use
axis equal
Jan
Jan el 15 de Feb. de 2018
@Emma: I have posted the code to call your getCircle function already. Walter has posted it also. Now you still write "it wont run the function getCircle." The examples we gave do run your function. So please explain, what you are doing instead of the suggested code.
Emma Pineiro
Emma Pineiro el 15 de Feb. de 2018
I talked with my professor, and there was something wrong with the file itself. I deleted the file and called it in a different script and everything worked! Thank you so much for all your help.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 15 de Feb. de 2018

Comentada:

el 15 de Feb. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by