2
$\begingroup$

I am trying to convert some quantities with units from rectangular to polar coordinates (to get the magnitude and angle). It seems that ToPolarCoordinates command does not calculate the angle when units are present. Does anyone have a solution to this (other than removing the units)?

N1 = Quantity[1.0, "Newtons"];
N2 = Quantity[2.0, "Newtons"];
ToPolarCoordinates[{N1, N2}]

The output is:

enter image description here

M1 = 1.0;
M2 = 2.0;
ToPolarCoordinates[{M1, M2}]

The output is:

enter image description here

$\endgroup$

2 Answers 2

2
$\begingroup$

I am not sure whether ArcTan of two quantities with compatible units should work by default, but you can certainly make it work yourself.

Unprotect[ArcTan];
ArcTan[q1_Quantity, q2_Quantity] := 
 ArcTan[QuantityMagnitude[q1], QuantityMagnitude[UnitConvert[q2, QuantityUnit[q1]]]]
Protect[ArcTan];

N1 = Quantity[1.0, "Newtons"];
N2 = Quantity[2.0, "Newtons"];
ToPolarCoordinates[{N1, N2}]

(* {Quantity[2.23607, "Newtons"], 1.10715} *)
$\endgroup$
1
$\begingroup$

Your issue is Mathematica cannot handle the units directly, so you need to extract the magnitudes, convert those magnitudes to polar, and reattach the units like so:

N1 = Quantity[1.0, "Newtons"];
N2 = Quantity[2.0, "Newtons"];

N1Value = QuantityMagnitude[N1];
N2Value = QuantityMagnitude[N2];

polarCoordinates = ToPolarCoordinates[{N1Value, N2Value}];

magnitude = Quantity[polarCoordinates[[1]], "Newtons"]
angle = Quantity[polarCoordinates[[2]], "Degrees"]*180/Pi

Out[10]= 2.23607 N
Out[11]= 63.4349 deg

You can change angle if you want rad.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.