필터 지우기
필터 지우기

Using while loop in a function?

조회 수: 9 (최근 30일)
Jenniluyn Nguyen
Jenniluyn Nguyen 2020년 3월 10일
답변: David Hill 2020년 3월 10일
Hello! First of all thank you for helping me out, this forum has done a lot to teach me more about MatLab.
I have a function that rotates a shape on a plot by however many degrees is inputted, which looks like this:
function [newx newy] = rotate(xcoords, ycoords, angle)
angle = angle*(pi/180); % convert angle to radians
newx = xcoords*cos(angle) - ycoords*sin(angle);
newy = xcoords*sin(angle) + ycoords*cos(angle);
I'm trying to write a second function with this function (we'll call rotate) with a while loop, but it does not seem to be working. What I want to do is when there is an input of x coordinates, y coordinates, and a number (which I assigned to repeats), it plots the specified number of rotations on a graph.
function [xc1, xc2] = spin(xcoords1, ycoords1,repeats)
degreeangle = 360/repeats;
hold on
while repeats > 1
[xc1, xc2] = rotate(xcoords1,ycoords1,degreeangle);
plot(xcoords1,ycoords1,'b-')
end
hold off
I am not sure why my code isn't working. Would appreciate any help! Thank you!

채택된 답변

David Hill
David Hill 2020년 3월 10일
function spin(xcoords1, ycoords1,repeats)
degreeangle = 360/repeats;
hold on
while repeats > 1
[xcoords1, ycoords1] = rotate(xcoords1,ycoords1,degreeangle);
plot(xcoords1,ycoords1,'b-')
repeats=repeats-1;
end
hold off

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by