How to fix the large and mad fractions when dealing with symbolic vars
조회 수: 66 (최근 30일)
이전 댓글 표시
Hi guys,
I face this often, as you can see below that cos(phi) and cos(psi) are decent entities, but when they are used with a symbolic variable (kn here), it shows huge fractions. Is there an easy way to make the coefficients of k1 k2 and k3 in the short format or some decent fractions.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1128680/image.jpeg)
Even something as trivial as k1*cos(pi/2) is not evaluated to be 0 as shown below. I fear that this leads to truncation errors.
Thanks in advance.
cos(pi/2)
syms a
a*cos(pi/2)
댓글 수: 4
Torsten
2022년 9월 18일
I could only see your screenshots after I opened your contribution using "Edit".
채택된 답변
Walter Roberson
2022년 9월 19일
The answer you are looking for is
digits(4)
sympref('FloatingPointOutput',true);
However, I do not recommend this.
추가 답변 (2개)
Paul
2022년 9월 18일
In this expression
syms a
a * cos(pi/2)
the cos() is evaluated numerically and the result converted to a sym to multiply with a. The numerical cos(pi/2) is not zero.
Force the cos to be evaluated symbolically
a * cos(pi/sym(2))
or
a * cos(sym(pi)/2)
댓글 수: 3
Torsten
2022년 9월 19일
For phi = 30 and phi = 90, cos(phi) is correctly simplified.
What would you expect for cos(50) and cos(25) ? I don't see a simple representation.
Walter Roberson
2022년 9월 19일
You should never eval() a symbolic expression. eval() of a symbolic expression is not documented by Mathworks. In practice it is treated as eval(char()) of the expression. However, char() of a symbolic expression is written in a mix of MATLAB, English, and the internal MuPAD programming language, and is not generally something that can be computed.
참고 항목
카테고리
Help Center 및 File Exchange에서 Number Theory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!