# Can ThermodynamicData be used with NSolve?

Let's say I define a function giving the enthalpy of water:

f[t_]:=QuantityMagnitude@ThermodynamicData["Water","Enthalpy",{"Pressure"->Quantity[1,"Bars"], "Temperature"->Quantity[t,"DegreesCelsius"]}]


I'd like to use that to find out what temperature corresponds to a given enthalpy:

NSolve[f[t]==300000,t]


But this gives me no result. Is there a way to do it?

Restrict the argument of f to numeric values:

Clear[f]
f[t_?NumericQ] := QuantityMagnitude @ ThermodynamicData[
"Water",
"Enthalpy",
{"Pressure"->Quantity[1,"Bars"], "Temperature"->Quantity[t,"DegreesCelsius"]}
]


Then, you can just use FindRoot:

FindRoot[f[t] == 300000, {t, 50}]


{t -> 71.6414}

• Interesting. I had tried that with NSolve but it didn't work, so I assumed it wouldn't work with FindRoot either. – Whelp May 16 at 15:23

How about generating an interpolation over the expected range and use FindRoot like so?

f = Interpolation@
Table[{t,
QuantityMagnitude@
ThermodynamicData["Water",
"Enthalpy", {"Pressure" -> Quantity[1, "Bars"],
"Temperature" -> Quantity[t, "DegreesCelsius"]}]}, {t, 1,
99}];
FindRoot[f[t] - 300000, {t, 50}]
(* {t -> 71.6414} *)


Also, if you are just interested in having temperature as a function of enthalpy, you could transpose the table and avoid FindRoot all together.

tofh = Interpolation@
Table[{QuantityMagnitude@
ThermodynamicData["Water",
"Enthalpy", {"Pressure" -> Quantity[1, "Bars"],
"Temperature" -> Quantity[t, "DegreesCelsius"]}], t}, {t, 1,
99, 10}];
tofh[300000]
(* 71.6414 *)

• That's a method I've been considering. I was worried it might be very slow. – Whelp May 16 at 14:45
• The database call is the slowest part. Once the table is populated, the interpolations are very fast. – Tim Laska May 16 at 14:47
• I'll accept and give it a try. Given that behind the hood it's just calling a database, I doubt it can be made much more efficient than that. – Whelp May 16 at 14:50
• If you are only interested in the liquid phase region, you need very few points. If you are near the boiling point, you may need a lot of points to capture the transition. I got the same answer when I stepped by 10 degrees. – Tim Laska May 16 at 14:53