10
$\begingroup$

I was playing with some of the new features, and when I type e.g.

Quantity[10, "ml"] + Quantity[250 - 100, "ml"]

I get

160 mL

as expected.

But when I want to solve the following equation

Solve[Quantity[x, "ml"] Quantity[5, "mol"] + Quantity[250 - x, "ml"] Quantity[7, "mol"] ==
Quantity[250, "ml"] Quantity[6, "mol"], x]

I get

{{x -> 125}}

without any unit. But x actually is in mL. So I'm wondering what I am getting wrong, i.e. why don't I get mL here?

thanks!

$\endgroup$

1 Answer 1

17
$\begingroup$

x is not in mL..it is just a pure number. Quantity[x, "ml"] is the 'thing' that is in mL.

To get what you want, you need to recast your Solve command as

Solve[x Quantity[5, "mol"] + (Quantity[250, "ml"] - x) Quantity[7, 
     "mol"] == Quantity[250, "ml"] Quantity[6, "mol"], x]

However, the result is a bit strange looking.

{{x -> Quantity[1/8000, ("Meters")^3]}} 

It is, however, correct. Let's convert it to numerical form

Solve[
  x Quantity[5, "mol"] + (Quantity[250, "ml"] - x) Quantity[7, 
      "mol"] == Quantity[250, "ml"] Quantity[6, "mol"], x] // N

gives

{{x -> Quantity[0.000125, ("Meters")^3]}}

Which looks right since 1 mL= 0.000001 m^3

How to force Mathematica to stick to mL, however, is another question.

Of course you could get back to mL by using UnitConvert. For example

UnitConvert[
 x /. Solve[
    x Quantity[5, "mol"] + (Quantity[250, "ml"] - x) Quantity[7, 
        "mol"] == Quantity[250, "ml"] Quantity[6, "mol"], 
    x] [[1]], "Milliliters"]

gives

Quantity[125, "Milliliters"]
$\endgroup$
3
  • 2
    $\begingroup$ Not strange at all! UnitConvert[Quantity[1/8000, ("Meters")^3], "Milliliters"] = 125 mL. $\endgroup$
    – P. Fonseca
    Dec 6, 2012 at 21:48
  • $\begingroup$ agreed. Perhaps I should have said 'unexpected' because one might like like to remain in Milliliters. $\endgroup$ Dec 6, 2012 at 21:54
  • $\begingroup$ Thanks! I think I have to get used to Quantity a little more. $\endgroup$ Dec 7, 2012 at 9:34

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.