Assign a unit to a variable in a function

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.

• 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. – eyorble Feb 10 '18 at 22:54
• yes, but is there a way to define it previously? so that I don't have to write everytime Quantity[]? – BobaJ Feb 10 '18 at 23:06
• For context (since the OP neglected to provide it): this is the van der Waals equation of state, relating pressure, volume and temperature. – J. M. will be back soon 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.

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_] :=
With[
{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] 