0
$\begingroup$

(THIS QUESTION HAD A MAJOR SIMPLIFICATION EDIT)

Lets suppose I want to convert the falling body equation, from one set of units to another.

enter image description here

I start with:

eqn = Quantity[d, "Feet"] == 1/2*Quantity[32.2, "Feet"/"Seconds"^2]*Quantity[t, "Seconds"]^2

(*Quantity[d, "Feet"] == Quantity[16.1 t^2, "Feet"]*)

And I apply the Map shortcut from suggestion Stephen Powell answer:

Solve[Map[UnitConvert, eqn], d]

(*{{d -> 16.1 t^2}}*)

Obviously, something went wrong, since, being this SI basis units, my weight has tripled!

Question, how can I easily convert equations or other type of expressions, between units?

I mean, imagine that I want to translate a book from US market to French. In the book, there are equations based on inch, feet, cfs, etc. And many times, all mixed together in the same equation, since applied engineering books have a lot of empirical thumb rule expressions (and everything gets homogenized by the constants, that not always show the units...). For these same reasons, I may want to express, in the translated book, a pipe diameter in mm, together with a flow rate in m3/h and a length in m (notice the mm instead of m, and the h instead of s...).

Question Is there a simple way I can do something like:

ExpressionUnitConvert[eqn, {d -> "mm", t -> "h"}]

Obviously, I can do it "by hand", starting by converting only the constant, and then, replacing the units (without converting them). But how to have Mathematica automatically return:

(* Quantity[d, "Meters"] == 1/2*Quantity[9.81, "Meters"/"Seconds"^2]*Quantity[t, "Seconds"]^2 *)

or...

(* Quantity[d, "Meters"] == Quantity[4.905, "Meters"/"Seconds"^2]*Quantity[t, "Seconds"]^2 *)

or...

(* Quantity[d, "Meters"] == Quantity[4.905 t^2, "Meters"] *)

(obviously, it will return something closer to 4.90728...)

$\endgroup$
3
  • $\begingroup$ If you use Solve on your very first equation you get the same answer. Solve[Quantity[d, "Feet"] == Quantity[h, "Feet"]^(1/2)*Quantity[dc, "Feet"]^(1/3)/ Quantity[2, "Feet"^(-1/6)], d] results in {{d -> 1/2 dc^(1/3) Sqrt[h]}}. I don't think that meters is the problem, I think that d is not a quantity is the reason you are getting that result. $\endgroup$ Commented Aug 20, 2015 at 12:36
  • $\begingroup$ I may be misunderstanding your question, but I think Mathematica is giving the correct answer. An equation of the form x=y, where x and y have the same dimensions, is independent of the choice of units. $\endgroup$ Commented Aug 20, 2015 at 13:16
  • $\begingroup$ @JackLaVigne The Solve of the feet expression is normal that it returns itself. But once you convert it to meters, the constant should change... (the constant has units... for the equation to be balanced). Please look at my edit (added example). $\endgroup$
    – P. Fonseca
    Commented Aug 20, 2015 at 14:15

2 Answers 2

2
$\begingroup$

The result that you're getting is perfectly correct, given what you have entered into Mathematica.

In your equation $d=\frac{1}{2}gt^2$, $d$ is a length, but when you translate that to

Quantity[d, "Feet"] == 1/2*Quantity[32.2, "Feet"/"Seconds"^2]*Quantity[t, "Seconds"]^2

you have defined d to be a unitless number. Specifically, d is defined as the numerical coefficient that when multiplied by a length of one foot gives the distance fallen. So of course changing the units has no effect on d, because d is dimensionless.

Another way to look at it is that d is defined as the distance fallen divided by one foot. If you change the units to meters, you would now say that d is the distance fallen divided by 0.3048 meters, i.e., one foot. So d doesn't change.

What you seem to want is to redefine d to be the distance fallen divided by one meter. You can do that with

eqn /. {d -> Quantity[d, "Meters"/"Feet"]}

(* Quantity[d, "Meters"] == Quantity[16.1 t^2, "Feet"] *)

Then UnitConvert gives the result you expect:

Map[UnitConvert, %]

(* Quantity[d, "Meters"] == Quantity[4.90728 t^2, "Meters"] *)

A more straightforward method is to leave the variables defined as dimensionful quantities. Then you can directly convert:

d == 1/2*Quantity[32.2, "Feet"/"Seconds"^2] t^2;
% /. q_Quantity :> UnitConvert[q]

(*  d == t^2 (Quantity[4.90728, ("Meters")/("Seconds")^2]) *)
$\endgroup$
1
  • $\begingroup$ Perfect clarification of the subject. Thank you. +1 $\endgroup$
    – P. Fonseca
    Commented Aug 21, 2015 at 12:52
3
$\begingroup$

I'm not sure if this is exactly what you want, but UnitConvert[x] converts any quantity x to SI units, so in this case you can just use Map:

eqn = Quantity[d, "Feet"] == Quantity[h, "Feet"]^(1/2)*Quantity[dc, "Feet"]^(1/3)/Quantity[2, "Feet"^(-1/6)];
Map[UnitConvert, eqn]

(* Quantity[(381 d)/1250, "Meters"] == Quantity[(381 dc^(1/3) Sqrt[h])/2500, "Meters"] *)

More generally, you can use

Replace[eqn, q_Quantity :> UnitConvert[q], Infinity]
$\endgroup$
1
  • $\begingroup$ I added an example in my question. If you apply my example to the answer you obtained, by substituting the variable with their values, in meters: Solve[Map[UnitConvert, eqn] /. {dc -> 0.3048, h -> 3.048}, d] ; you get the wrong answer : 0.587466 $\endgroup$
    – P. Fonseca
    Commented Aug 20, 2015 at 14:22

Your Answer

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

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