Using while loop in a function?

19 vues (au cours des 30 derniers jours)
Jenniluyn Nguyen
Jenniluyn Nguyen le 10 Mar 2020
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!

Réponse acceptée

David Hill
David Hill le 10 Mar 2020
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

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by