Weird behavior of modulo function

Why is this code plotting a straight line?
syms x(t);
x(t) = mod(t, 3);
tSamp = -10:0.01:10;
figure(1);
plot(tSamp, x(tSamp));
mod(a, b) should return a value in the range [0, b), and here it is like mod(t, 3)==t.
What is happening???

 採用された回答

Walter Roberson
Walter Roberson 2018 年 5 月 31 日

1 投票

Symbolic mod() does not do what you think it does. And that's a nuisance that has caught me multiple times as well!!
"If a is a polynomial expression, then mod(a,b) finds the modulus for each coefficient."
t is a polynomial in t, equivalent to 1*t + 0 . That is coefficients [1 0]. mod([1 0], 3) is [1 0], so mod(t,3) is 1*t + 0, which is just t.
You have to use something like t - 3*floor(t/3)

3 件のコメント

Steven Lord
Steven Lord 2018 年 5 月 31 日
Or just don't use symbolic variables and functions at all.
>> tSamp = -10:0.01:10;
>> plot(tSamp, mod(tSamp, 3))
or
>> tSamp = -10:0.01:10;
>> f = @(t) mod(t, 3);
>> plot(tSamp, f(tSamp))
Milosh
Milosh 2018 年 5 月 31 日
Thank you very much!
Milosh
Milosh 2018 年 5 月 31 日
編集済み: Milosh 2018 年 5 月 31 日
@Steven No, I need the symbolic variables for some integration later...

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2017a

質問済み:

2018 年 5 月 31 日

編集済み:

2018 年 5 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by