Output Symbolic Trig Math in Degrees
9 ビュー (過去 30 日間)
古いコメントを表示
I am multiplying several matrices together. The inputs to these matrices are trig expressions mostly with symbols and an occasional number. I know that due to floating point math, when I do some trig function that should be zero such as cos(pi/2), it comes out as a fraction that is very close to zero. This makes it difficult to read what the output should be. But, when I use cosd or sind to get rid of this problem, the output has pi/180 in every trig expression. This also makes it difficult to read. Is there a way I can force it to not display the pi/180 or otherwise just make it more legible? The expressions I end up with are very long and this problem just adds to the length and complexity and it also comes with its own set of parentheses which makes it even more difficult to read.
Edit: As examples, this is what I get for one of the element in my matrix when I used cosd and sind:
cos((pi*(theta4 + 90))/180)*(cos((pi*theta1)/180)*cos((pi*theta3)/180)*cos((pi*(theta2 + 90))/180) - cos((pi*theta1)/180)*sin((pi*theta3)/180)*sin((pi*(theta2 + 90))/180)) + sin((pi*theta1)/180)*sin((pi*(theta4 + 90))/180)
And this is what I get when I use cos and sin:
cos(theta4 + 90)*(cos(theta3)*(cos(theta2 + 90)*cos(theta1) - (4967757600021511*sin(theta2 + 90)*sin(theta1))/81129638414606681695789005144064) - sin(theta3)*((4967757600021511*cos(theta2 + 90)*sin(theta1))/81129638414606681695789005144064 + sin(theta2 + 90)*cos(theta1))) - sin(theta4 + 90)*((4967757600021511*cos(theta3)*((4967757600021511*cos(theta2 + 90)*sin(theta1))/81129638414606681695789005144064 + sin(theta2 + 90)*cos(theta1)))/81129638414606681695789005144064 - sin(theta1) + (4967757600021511*sin(theta3)*(cos(theta2 + 90)*cos(theta1) - (4967757600021511*sin(theta2 + 90)*sin(theta1))/81129638414606681695789005144064))/81129638414606681695789005144064)
2 件のコメント
Torsten
2022 年 6 月 21 日
Why are you interested in these intermediate results ?
Just put them in a variable and you don't need to see them.
Stephen23
2022 年 6 月 21 日
編集済み: Stephen23
2022 年 6 月 21 日
" I know that due to floating point math, when I do some trig function that should be zero such as cos(pi/2), it comes out as a fraction that is very close to zero"
The symbolic toolbox is quite capable of giving exact results:
x = cos(sym(pi)/2)
double(x)==0
"But, when I use cosd or sind to get rid of this problem, the output has pi/180 in every trig expression."
How would using degrees "get rid of this problem" ?
回答 (1 件)
John D'Errico
2022 年 6 月 21 日
If you use cos and sin, terms like this are meaningless, if the 90 is a number in degrees. Well, let me just say you will get meaningless garbage out. That you want MATLAB to somehow know that you want cos to interpret the inputs as you wish is not going to happen. Cos and sin use radians, NOT degrees. It must do that, otherwise there are millions of codes that would immediately fail.
cos(theta4 + 90)
So you really have two main choices. Difficult to read but still mathematically correct, or meaningless garbage. Do you have a preference?
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!