While loops and repeated reduction of a number.

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.

답변 (2개)

Joseph Cheng
Joseph Cheng 2014년 4월 9일

0 개 추천

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 2014년 4월 9일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2014년 4월 9일

답변:

2014년 4월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by