I'm trying to take a formula, take it's integral which yields a formula with logarithms, and then directly plug in values with units and get an answer. However if the units go into any of the logarithms I get conditional expressions. Is it possible to set up the equations so I can just plug in units?
Here's my code:
Pideal[n_, T_, V_] := (n*R*T)/V
PvdW[n_, T_, V_, a_: a, b_: b] := (n*R*T)/(V - n*b) - (n^2*a)/V^2
W[V1_, V2_, P_] := Integrate[P, {V, V1, V2}]
Wideal[V1_, V2_, n_: n, T_: T] = W[V1, V2, Pideal[n, T, V]]
Here's the problem spot:
Wideal[5 Liters, 10 Liters, 1 Mols, 298 Kelvins]
I can work around this fairly easily, just leave out the Liters because they cancel out anyway. (Liters = Quantity["Liters"]
, etc)
However when I get to a more complicated equation that doesn't work so easily without making sure to put back in what I leave out.
WvdW[V1_, V2_, n_: n, T_: T, a_: a, b_: b] =
W[V1, V2, PvdW[n, T, V, a, b]]
WvdW[5*Liters, 10*Liters, 1 Mols, 298 Kelvins, 0, 0]
This doesn't work for the same reason, even though the Liters cancel out Mathematica doesn't assume that. However if I leave them out, I still have problems with the Mols, which appear in and outside of Logarithms, even though they're multiplied by 0.
WvdW[5, 10, 1 Mols, 298 Kelvins, 0, 0]
Finally, I haven't even gotten to using b which also appears in the logarithm, and even though the volumes add up and cancel out, Mathematica doesn't assume so.
WvdW[V1*Liters, V2*Liters, 1 Mols, 298 Kelvins, 0, 0 Mols Liters^(-1)]
SetDelayed
(:=
) instead ofSet
(=
) you don't appear to have a problem whatsoever. $\endgroup$R = Quantity["MolarGasConstant"]
andUnitConvert[ Wideal[Quantity [5, "Liters"], Quantity [10, "Liters"], Quantity [1, "Moles"], Quantity[ 298, "Kelvins"]], "Joules"]
I getQuantity[1.71742, "Joules"]
. $\endgroup$