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
Jan
2018년 2월 15일
Then please tell us, how you call this function. Do you click on the green triangle in the editor?
답변 (1개)
Jan
2018년 2월 15일
Call it from another script or function or from the command window:
center = 23;
radius = 5;
[x, y] = getCircle(center,radius)
댓글 수: 6
Emma Pineiro
2018년 2월 15일
Walter Roberson
2018년 2월 15일
center = [6,6];
radius = 5;
[x, y] = getCircle(center,radius);
plot(x, y);
Emma Pineiro
2018년 2월 15일
Walter Roberson
2018년 2월 15일
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
2018년 2월 15일
@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
2018년 2월 15일
카테고리
도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!