Matlab Simplifies Function To 0

2 ビュー (過去 30 日間)
Will
Will 2020 年 3 月 15 日
コメント済み: Will 2020 年 3 月 15 日
Trying to create a sawtooth function using
sawtooth(x, a, f) = -a * mod((2 * x * f) + 1, 2) + a
but it simplifies to 0.
This function works fine in desmos, so I'm not sure why Matlab simplifies it to 0.
  2 件のコメント
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 3 月 15 日
That's seems like a very strange bug, it seems matlab is simply ignoring the mod function, you can see this as an easier example by running:
A = mod(x*f,2)
A =
f*x
I'm really not sure why this happen, if you can live without the symbolic I would simply define a annonymous function:
sawtooth = @(x,a,f) -a * mod(( 2*(x) * f) + 1, 2) + a;
sawtooth(1.1,2,3)
ans =
-1.2000
Will
Will 2020 年 3 月 15 日
That worked thank you.

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

回答 (1 件)

dpb
dpb 2020 年 3 月 15 日
That doesn't define a function in MATLAB syntax; it assigns the evaluation of the RHS of the assignment statement to a 3D array sawtooth presuming the variables x,f,a exist and are integer-valued and not zero.
One only defines a function in MATLAB in an m-file with a function statement at the beginning or as a variable with an anonymous function as
fnSawtooth=@(x,a,f) -a*mod(2*x*f+1,2)+a;
For the above functional, if x,f are single integer values, then mod(2xf+1,2) is always 1 and the expression reduces to -a*1+a --> -a+a --> 0.
NB: if have Signal Processing TB, there's a builtin sawtooth function in it...

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by