Output Symbolic Trig Math in Degrees

9 visualizaciones (últimos 30 días)
Michael Boyte
Michael Boyte el 21 de Jun. de 2022
Comentada: Michael Boyte el 21 de Jun. de 2022
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 comentarios
Torsten
Torsten el 21 de Jun. de 2022
Why are you interested in these intermediate results ?
Just put them in a variable and you don't need to see them.
Stephen23
Stephen23 el 21 de Jun. de 2022
Editada: Stephen23 el 21 de Jun. de 2022
" 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" ?

Iniciar sesión para comentar.

Respuestas (1)

John D'Errico
John D'Errico el 21 de Jun. de 2022
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 comentario
Michael Boyte
Michael Boyte el 21 de Jun. de 2022
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by