Difference between a<t<b and t>a && t<b

Hi I am trying to verify existence of even and odd harmonics in e^(-t) periodic with period pi. For that I try to verify f(t)=f(t+pi). and for f(t+pi) i try to add shift using if-else condition according to this link answer should have even harmonics, http://www.intmath.com/fourier-series/5-harmonic-analysis.php I am trying to solve Example 2(b) in Matlab.
Now if i use the condition 0<=t(i)<pi it shows me even harmonic but if i use 0>=t(i) && t(i)<pi it gives neither even nor odd.
So, my question is why isn't Matlab showing same result for both formats. and which format is correct.

 採用された回答

Guillaume
Guillaume 2015 年 4 月 28 日
編集済み: Guillaume 2015 年 4 月 28 日

4 投票

The first form is not going to give you the result you expect, it is equivalent to:
(0 <= t(i)) < pi
Thus, it will compare t(i) to 0, and the result is either 1 (true) or 0 (false). It then compares that 0 or 1 to pi, which is always smaller. Hence the result will always be true.
The second form is the correct one. You can't link comparisons. You have to perform them one at a time and link the results with logical operators.

5 件のコメント

Ahmet Cecen
Ahmet Cecen 2015 年 4 月 28 日
You are kidding me, how have I not noticed this years an years of writing conditionals in MATLAB... I wonder if I just got lucky or there is a bug somewhere in some of my old codes...
Guillaume
Guillaume 2015 年 4 月 28 日
I'm not kidding. I don't think there are many languages where chaining comparisons would work since the result of the first comparison is normally a boolean.
John D'Errico
John D'Errico 2015 年 4 月 28 日
If you have been doing this for years, then there are definitely bugs hidden in your code.
Ahmet Cecen
Ahmet Cecen 2015 年 4 月 28 日
Thankfully I found only one in a rotation code for visualization. Apparently I like to separate out the conditionals by default. Still many thanks for pointing this out, could have been a disaster. Would give more bumps if I could!
Maheen
Maheen 2015 年 5 月 1 日
Thank you So much Guillaume, resolved my problem.

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

その他の回答 (1 件)

Ahmet Cecen
Ahmet Cecen 2015 年 4 月 28 日

1 投票

might be because you are supposed to write:
*t(i)>=0 && t(i)<pi*
NOT:
*0>=t(i) && t(i)<pi*

カテゴリ

ヘルプ センター および File ExchangeIntroduction to Installation and Licensing についてさらに検索

タグ

質問済み:

2015 年 4 月 28 日

コメント済み:

2015 年 5 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by