While loops and repeated reduction of a number.

2 views (last 30 days)
I am trying to write a function file that reduces a given radian such as "7pi" until in its most basic form- between 0 and 2pi. I struggle with while loops for some reason, and this is what I have so far. So if I inputted 7pi I should get out 1pi.
>>n=r-2*pi;
>>While 0<n<2*pi
>> n=r-2*pi;
>>End
I don't know how to structure this, and I need it to work in order to implement a larger script for my project. I'm self taught, so sorry if the above is way off from accurate.

Answers (2)

Joseph Cheng
Joseph Cheng on 9 Apr 2014
what you are missing from your loop is the updated variable. so what you should have is something like this
Originalr = r;
While 0<r<2*pi
r=r-2*pi;
End
your intial loop did not update so you are just performing the same subtraction over and over 7pi-2pi.

Star Strider
Star Strider on 9 Apr 2014
I suggest using the rem function:
r = (1:7)*pi; % Example argument vector
f = rem(r, 2*pi)
produces:
f =
3.1416 0.0000 3.1416 0.0000 3.1416 0.0000 3.1416

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!