Queries on the use of "double"

3 ビュー (過去 30 日間)
Doc Ed
Doc Ed 2020 年 9 月 17 日
コメント済み: Ameer Hamza 2020 年 9 月 30 日
Hi MATLAB, during our laboratory simulation, when we evaluate sin(x) when x=2, and use the "subs" command, some students reported the answer as sin(2) instead of 0.9093. How come? But when they use the "double" command, they arrived 0.9093. But other computers don;t need double command.
Case 1:
Script:
y=input('Input f(x): ');
x=input('Input x for f(x): ');
val=subs(y,x)
Result:
Input f(x): sin(x)
Input x for f(x): 2
val=
sin(2)
Case 2:
Script:
y=input('Input f(x): ');
x=input('Input x for f(x): ');
val=subs(y,x)
Result:
Input f(x): sin(x)
Input x for f(x): 2
val=
0.9093
Why did some computers reported sin(2) instead of 0.9093 with the same script? Why do some programs need double command but other computeres don't? Thank you

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 9 月 17 日
This is probably caused by symbolic preferences on your machines. I guess you have defined x as a symbolic variable before this code.
syms x
By default, in the case of symbolic variables, MATLAB does not convert to a floating-point value; therefore, sin(2) is the default answer. If you are getting a value of 0.9093, then that means that somehow the preference has been changed by running
sympref("FloatingPointOutput", 1)
If you want always to get floating-point output, run the above line on all machines. To get the default preferences, run
sympref("default")
  2 件のコメント
Doc Ed
Doc Ed 2020 年 9 月 30 日
thank you
Ameer Hamza
Ameer Hamza 2020 年 9 月 30 日
I am glad to be of help!

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

その他の回答 (1 件)

BOB MATHEW SYJI
BOB MATHEW SYJI 2020 年 9 月 17 日
I think the first one uses syms command anywhere in the script while the second one does not. While you use syms command the entire expression is given as output. So you have to manually give double(output) to get the value.

カテゴリ

Help Center および File ExchangeNumbers and Precision についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by