Displaying (small number)*(syms variable) as 0

6 ビュー (過去 30 日間)
Mike
Mike 2021 年 2 月 15 日
回答済み: Steven Lord 2021 年 2 月 16 日
This is an arbitrary example representing my problem
syms x;
a=0;
b=pi/2;
c=x;
R=[cos(a)*cos(b)*cos(c) sin(a)*sin(b)*sin(c)];
vpa(R)
This outputs:
ans =
[ 6.1e-17*cos(x), 0]
Because pi isn't exact in Matlab, then cos(pi/2) isn't exactly zero as sin(0) is, which leads to the first element in ans to be written as such, and the second element to be written exactly 0;
I've tried "digits" before vpa, "format long", changed format in preferences, but none works.
How can I edit my code such that the first element is displayed as exactly 0?

採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 15 日
syms x;
a=0;
b=sym(pi)/2;
c=x;
R=[cos(a)*cos(b)*cos(c) sin(a)*sin(b)*sin(c)];
vpa(R)
ans = 
  1 件のコメント
Mike
Mike 2021 年 2 月 15 日
Can't believe it was that simple, thanks.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2021 年 2 月 16 日
Another solution, if you know one of the quantities involved is a multiple of π:
syms x;
a=0;
b=1/2;
c=x;
R=[cos(a)*cospi(b)*cos(c) sin(a)*sinpi(b)*sin(c)];
vpa(R)
ans = 
cospi computes the cosine of pi times its input without actually computing pi times its input.

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by