Programming: keep theta between specific values

1 回表示 (過去 30 日間)
Babak
Babak 2014 年 8 月 26 日
コメント済み: Star Strider 2014 年 8 月 26 日
Hi every one,
I have a problem in programming. I have theta which start from 0 and goes to unspecified value. I want to reset theta if it reached to 2pi and keep it always between [0,2pi]. I wrote a code but it is not true for all situation(Ex after 4pi). My code is:
if theta>2*pi
theta=theta-2*pi;
end

回答 (2 件)

Star Strider
Star Strider 2014 年 8 月 26 日
編集済み: Star Strider 2014 年 8 月 26 日
You can do that with the rem function:
x = linspace(0,10*pi);
theta = rem(x, 2*pi);
figure(1)
plot(x/pi,theta/pi)
grid
xlabel('Angle (x \pi)')
ylabel('Angle (x \pi)')
They are plotted as multiples of pi for convenience in visualising the result.
  2 件のコメント
Babak
Babak 2014 年 8 月 26 日
I think my explanation was not complete. I should say I get a value of theta each time not a vector. theta is a [1*1] matrix and it will increase.
Star Strider
Star Strider 2014 年 8 月 26 日
A (1x1) matrix is a scalar.
My code does exactly what you illustrated. It produces vectors of values from (0,2*pi) from your linearly increasing vector that goes from zero to at least 6*pi.

サインインしてコメントする。


Babak
Babak 2014 年 8 月 26 日
I should write something like this: ( there should be an easier way!)
if 0<theta<2*pi
theta=theta-2pi;
end
if 2*pi<theta<4*pi
theta=theta-4pi;
end
if 4*pi<theta<6*pi
theta=theta-6pi;
end
  1 件のコメント
Star Strider
Star Strider 2014 年 8 月 26 日
Run your code and see if it produces the result you illustrated.
Mine does.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by