Output Symbolic Trig Math in Degrees

조회 수: 15 (최근 30일)
Michael Boyte
Michael Boyte 2022년 6월 21일
댓글: Michael Boyte 2022년 6월 21일
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
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
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)
x = 
0
double(x)==0
ans = logical
1
"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
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?
  댓글 수: 1
Michael Boyte
Michael Boyte 2022년 6월 21일
In this example, theta4 is a symbolic term. So when I use cos(theta4 + 90), the output shows up as cos(theta4 + 90) and when I use cos(theta4 + pi/2) the output shows up as cos(theta4 + pi/2). In either case, it's not showing as a numeric value because theta4 is symbolic.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Mobile Fundamentals에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by