I have the following input:

PVW[V_, T_] := (R*T/(V - b)) - (a/(V^2))

Where I have already defined R as the molecular gas constant with its corresponding units and a and b are constants with their proper units.

My question is, if it is posible to define, that V and T also have units, so that when I put, for example, PVW[1.5,300] the results comes with the correct units.

  • $\begingroup$ Input the pressure and temperature with units as well? PVW[Quantity[1.5,"Liters"],Quantity[300,"Kelvin"], if the other units are set up properly, should suffice. $\endgroup$ – eyorble Feb 10 '18 at 22:54
  • $\begingroup$ yes, but is there a way to define it previously? so that I don't have to write everytime Quantity[]? $\endgroup$ – BobaJ Feb 10 '18 at 23:06
  • $\begingroup$ For context (since the OP neglected to provide it): this is the van der Waals equation of state, relating pressure, volume and temperature. $\endgroup$ – J. M.'s technical difficulties Mar 18 '18 at 14:15

You may use With.

PVW[V_, T_] :=
 With[{qV = Quantity[V, "Liters"], qT = Quantity[T, "Kelvins"]},
  (R*qT/(qV - b)) - (a/(qV^2))

Hope this helps.

| improve this answer | |

I think you can just include the units of the parameters and variables in the the function body. I demonstrate this with a Galilean model of a falling body (I use the contrived example because you didn't supply the necessary constants and units for you equation).

s[so_, vo_, t_] :=
   {g =
     UnitConvert[Quantity[None, "StandardAccelerationOfGravity"], "m/s/s"] // N},
   Quantity[so, "Meters"] + 
   Quantity[vo, "Meters"/"Seconds"] Quantity[t, "Seconds"] - 
   g Quantity[t, "Seconds"]^2/2]

Then a table of the altitude of a two second fall can be made as follows:

With[{v0 = v0 /. Solve[s[10, v0, 2] == 0, v0]}, 
  Table[{Quantity[t, "Seconds"], s[10, v0, t]}, {t, 0, 2, .1}] // Chop // TableForm]


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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