Symbolic sin(pi) in Matlab 2020 a not simplify

18 ビュー (過去 30 日間)
Jiefeng Sun
Jiefeng Sun 2020 年 10 月 6 日
回答済み: Steven Lord 2023 年 1 月 7 日
I am using symbolic toolbox, but it does not simplify.
But it works great for Matlab 2017 b.
>> syms pi
>> sin(pi)
ans =
sin(pi)
>> simplify(ans)
ans =
sin(pi)

採用された回答

Walter Roberson
Walter Roberson 2020 年 10 月 6 日
R2020a changed sym so that now there are no special symbols. Before sym pi resulted in a symbolic version of the irrational constant π but now it is just another variable. Likewise Euler gamma constant and one other constant that is not coming to mind at the moment.
Now if you want the symbolic version of the irrational number you need to
sym(pi)
and count on sym being able to recognize the finite numeric approximation that is the function pi()
I personally do not think that this was the best way for Mathworks to have proceeded. I personally think that should be possible for a user to directly name symbolic version of the constant. I had filed an enhancement to have the special treatment documented so as to reduce problems for people who were not aware of it, and they choose to get rid of the special treatment instead.
  2 件のコメント
Jiefeng Sun
Jiefeng Sun 2020 年 10 月 6 日
I agree. Look forward to seeing some better ways.
Bjorn Gustavsson
Bjorn Gustavsson 2020 年 10 月 6 日
Demonstrably it works - but there is something uncomfortable about counting on that level of cleverness. To me it feels way more robust to define pi as 4 (natural number, so no cleverness required there) times atan(1) (which by definition is the angle that is a quarter of pi)

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

その他の回答 (3 件)

Bjorn Gustavsson
Bjorn Gustavsson 2020 年 10 月 6 日
That is because your definition of pi as a symbolic variable that hides the built-in pi. Try:
which pi -all
or:
sym x
pi = 4*atan(x/x);
sin(pi)
HTH
  2 件のコメント
Jiefeng Sun
Jiefeng Sun 2020 年 10 月 6 日
編集済み: Jiefeng Sun 2020 年 10 月 6 日
That might be the be the problem.
But the same code works for Matlab 2017.
That is what I got from Matlab 2020 a, and it seems it is the build in pi
>> which pi -all
pi is a variable.
built-in (C:\Program Files\MATLAB\R2020a\toolbox\matlab\elmat\pi) % Shadowed
>> sin(pi)
ans =
sin(pi)
Bjorn Gustavsson
Bjorn Gustavsson 2020 年 10 月 6 日
No it is not the built-in pi you see - it clearly states that the built-in is shadowed.

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


Jiefeng Sun
Jiefeng Sun 2020 年 10 月 6 日
sym('pi') now creates a symbolic variable named pi instead of a symbolic number representing the mathematical constant π
Behavior changed in R2020a
So
syms pi
Will not work any more.
I use
clear all
pi = sym(pi)
It works.

Steven Lord
Steven Lord 2023 年 1 月 7 日
Another possible solution if you're just trying to avoid the roundoff error involved in approximating π as pi is to use the sinpi function.
d1 = sin(pi)
d1 = 1.2246e-16
d2 = sinpi(1)
d2 = 0
p = sym(pi);
d3 = sin(p)
d3 = 
0

カテゴリ

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

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by