Why does Matlab substitute a number in a formula instead of solving it?

21 ビュー (過去 30 日間)
tommytutone
tommytutone 2019 年 7 月 18 日
コメント済み: Walter Roberson 2019 年 7 月 18 日
Using the Symbolic toolbox, Matlab seems to arbitrarily determine whether or not to return a solved value when a concrete value is substituted in a formula.
>> syms x
>> f(x) = 1/(5+4*cos(x))
>> f(0)
ans = 1/9
>> f(1)
ans = 1/(4*cos(1) + 5)
Why isn't it solving for f(1) ? I'm guessing its because I'm working symbolically, so I can substitute a second equation as x, but how I can force Matlab to return a concrete value?
EDIT: I tried double(f(1)) and that seemed to work. Would that be considered best practice here?

採用された回答

Star Strider
Star Strider 2019 年 7 月 18 日
The Symbolic Math Toolbox outputs its results as symbolic expressions, unless you ask it to do otherwise. (It assumes you want a symbolic result.) The double function is not always appropriate for symbolic results, since if the symbolic result contains a symbolic variable, double will throw an error.
If you want a numeric result, use the vpa() function:
syms x
f(x) = 1/(5+4*cos(x))
Out1 = f(0)
Out2 = f(1)
Out3 = vpa(f(1))
producing:
Out1 =
1/9
Out2 =
1/(4*cos(1) + 5)
Out3 =
0.1396412210276252254724967860432
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 7 月 18 日
The Symbolic Toolbox tries to create closed form expressions of indefinitely precise algebraic numbers when practical. The Symbolic Toolbox works with mathematical results instead of approximations whenever it can.
If 1/(4*cos(1) + 5) "is" 0.139641221027625 then it follows that mod(1e17 * 1/(4*cos(1) + 5),1) should be 0 -- that multiplying by 10^17 should get you an exact integer. But that is clearly not true mathematically: cos(1) is mathematically an infinitely long decimal value.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by