Borrar filtros
Borrar filtros

Trouble using atan2 for coordinate transform, maybe recommend other tools.

20 visualizaciones (últimos 30 días)
I am trying to use atan2 on a 2D plane (X,Z) centered at (0,0) to define an angle phi.
I know that using atan(Z./X) or atan2(Z,X) will yield different discontinuities at either pi/2 or pi respectively. I need this angle to define a coordinate transform matrix for a vector V(X,Y,Z) into V(t,y,r).
This basically what I am trying to do, and I'm not sure
v1 = V(1,i,j,k); % [i,j,k] represent indices within the X,Y,Z position and V(1,i,j,k) is same size.
v2 = V(2,i,j,k); % second component
v3 = V(3,i,j,k); % third component
The transformation matrix is defined as follows...
phi = atan2(Z(i,j,k),X(i,j,k)); % or phi = atan(Z(i,j,k)./X(i,j,k));
J = [sin(phi) 0 -cos(phi);
0 1 0;
cos(phi) 0 sin(phi)];
v_prime = J*[v1,v2,v3]';
... but does not take into account the discontinuity in phi. As a result, I get a similar discontinuity in v_prime, but this should not be there realistically.
Is there a way that I can avoid the discontinuity from phi showing up in v_prime? Maybe I can use a different tool that will give phi from 0 to 2*pi instead? Technically phi should be increasing infinitely but I only care about the continuous range from 0 to 2*pi. I'm not aware of any particular functions that will do what I'm looking for.
Even though I am using R2021a for these calculations, I need any possible solution to also be compatible with R2016b since that is what we use in our lab.
  6 comentarios
Nathaniel H Werner
Nathaniel H Werner el 23 de Nov. de 2021
Hi David,
Thanks I think that makes sense.
X and Z that do cross over zero. X ranges from -30 to 40, and Z ranges from -60 to 60. So I guess that could be resulting in the discontinuities in v_prime. I wonder if it could also be due to calculating the angle ahead of time. Maybe I could try to do it in the same for loop as the multiplication of J*[v1,v2,v3].
David Goodmanson
David Goodmanson el 23 de Nov. de 2021
Hi Nathanial,
For the type of discontinuity I was talking about both Z and X have to cross the origin 'at the same time'. As long as the appropriate index is kept track of correctly, I doubt that the issue comes from calculating beforehand, but it's worth a try.

Iniciar sesión para comentar.

Respuestas (1)

John D'Errico
John D'Errico el 21 de Nov. de 2021
Editada: John D'Errico el 21 de Nov. de 2021
The transformation you are using is a simple tranformation into polar coordinates. When you have y in there also, this becomes what is commonly known as cylindrical coordinates. So we will have a transformation of your coordinate system from (X,Y,Z) into (THETA,Y,R). Totally common in mathematics, and often very useful.
A useful tool in this is to just use cart2pol. It will convert the pair (X,Z) into (theta,R). And since you know that Y is unchanged, that will make things almost trivial, simpler even than using atan2 and then computing R.
A problem is, you seem to think there is a problem of a discontinuity. In reality, yes, we will expect to see a discontinuity, in the sense the angle theta will always lie in the (half open) interval [-pi,pi). Or [-180,+180), if you prefer to think in degrees.
That is, you can imagine the rotation being done smoothly in a circle. At some point, the rotation angle starts to approach 180 degrees. Just at the point where it tips over, past 180 degrees say to 181 degrees, the angle reported will revert to -179 degrees. The rotation is the same. Everything is happy. But because the angles as reported only live in the interval [-180,180), you must see what you are describing as a discontinuity, because at that point, you are visualizing this as an angle in a polar coordinate system. You can still have angles in that coordinate system that are larger than 180, or less than -180. But when you compute that rotation angle from X and Z, it will be contained in that limited interval. And that is what you are thinking of as a discontinuity.
(Another sometimes useful tool here is the unwrap function. You really don't need it in this context though.)
But, really, is this a discontinuity in the real world? NO! The rotation matrix you have does not truly convert things into cylindrical coordinates at all. You can think of it as an implicit transformation. All it does is to rotate the X and Z axes around the Y axis, so leaving the Y axis unchanged. If you think of the result after rotation, there is no discontinuity. If you spin a top around, there is no huge bump as it spins. You do not hear a thump, thump, thump,... as it spins. Just a faint whir as it disturbs the air. The discontinuity lies only in your mind as you try to visualize the cylindrical coordinate system.
Hmm. Maybe I need to expain this as an animation. That may take some time to draw pictures. So first, I'll try to draw a mental picture:
Consider a point that rotates around a circle. So imagine this as the point (1,Y,0). Think of that point rotating around the y axis. At a rotation of 90 degrees, it will become (0,Y,1), etc. Eventiually, it gets to the point at 180 degrees, where the coordinates after rotation are now (-1,Y,0). If we rotate just a bit more, just 1 degree more, things are still moving smoothly. There is no huge bump in the night. As I have written it here, as a function of the rotation angle phi, that original point will have rotated to: (cos(phi),Y,sin(phi)). Would you agree with that?
In fact, for ANY smoothly incrementing angle phi, that rotation will be smooth and continuous. Again, no bumps will ever be seen, none felt. As you smoothly change phi, the transformation is smooth and continuous. The only discontinuity you will ever see is when you visualize the transformation in terms of that polar coordinte system. And this is because the rotation angle will always be reported in a finite range like [-pi,pi). So the discontinuity lies only in the mind, not in reality.
I'll hope for now this is sufficient, because it is Sunday morning here and I am feeling far, far too lazy to draw anything more than mental pictures and thought experiments. :)
  2 comentarios
Nathaniel H Werner
Nathaniel H Werner el 21 de Nov. de 2021
Editada: Nathaniel H Werner el 21 de Nov. de 2021
It is not cylindrical because each of the components retains the same units of length. J has similarities to rotation matrices, but since it's not acting to rotate anything, this semantic argument is rather fruitless. So no, I don't agree with your illustration at all because each particle at any specific grid point is not undergoing any rotation. It remains stationary and the components effectively become aligned with a cylindrical coordinate system. That is the primary similarity between the two. You can do the derivation yourself of the Jacobian and see that this matrix is not identical to cylindrical, but I really don't see the need to go into that much detail here.
I have no problem with expecting the discontinuity mathematically, however my data is a continuous spatially varying vector valued function. Any discontinuity is an indication of non-physicality and is therefore not permitted.
As I have mentioned in a previous comment, the discontinuities are not in my mind. MatLab is actually plotting the transformed data in 3d space with a discontinuity. Hence, my calculation of the angle needed to perform the transformation is causing the problem. So I need a new method.
Anyway, since I am primarily interested in the radial coordinate (sorry if I didn't mention that in the question, I was try to be brief), maybe I can use the built in tools for a cylindrical transformation. It had not occurred to me, but that's probably a good idea. I'll try that.
When I get back to my computer, I'll update my question with some figures showing the discontinuity in my attempt at calculating the radial component.
Nathaniel H Werner
Nathaniel H Werner el 21 de Nov. de 2021
Apparently I left the drive with my data in the lab yesterday, so I can't update the question to show the figures. Sorry about that.

Iniciar sesión para comentar.


Más información sobre Motion Modeling and Coordinate Systems en Help Center y File Exchange.





Community Treasure Hunt

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

Start Hunting!

Translated by