Message Boards Message Boards

[✓] Find pressure corresponding to an entropy (s) and temperature (t)?

GROUPS:

This function is supposed to return the pressure corresponding to an entropy (s) and temperature (t).

Instead the function just returns the initial guess in FindRoot.

What's wrong?

Thanks.

pressure[s_Real, t_Real] := 
         Module[{p}, 
          sol = FindRoot[
            s == QuantityMagnitude[
              ThermodynamicData["CarbonDioxide", 
               "Entropy", {"Temperature" -> Quantity[t, "DegreesCelsius"], 
                "Pressure" -> Quantity[p, "Megapascals"]}]], {p, 13}]; 
          p /. sol]
POSTED BY: Glenn Carlson
Answer
17 days ago

You can get rid of the ThermodynamicData::quant error by keeping ThermodynamicData from evaluating when p is symbolic by wrapping it in a ?NumericQ-protected function. See "How do I use ?NumericQ to affect order of evaluation?"; more examples on Stackexchange here.

You can get rid of the FindRoot::jsing error by avoid methods that use derivatives, such as the Secant Method, which is automatically invoked if you use two starting points. This makes sense because ThermodynamicData does not return an expression that can be symbolically differentiated.

One issue that arose for me is that ThermodynamicData occasionally timed out, which caused FindRoot to throw an error.

pressure[s_Real, t_Real] := Module[{p, obj},
  obj[p_?NumericQ] := 
   QuantityMagnitude[
    ThermodynamicData["CarbonDioxide", 
     "Entropy", {"Temperature" -> Quantity[t, "DegreesCelsius"], 
      "Pressure" -> Quantity[p, "Megapascals"]}]]; 
  sol = FindRoot[s == obj[p], {p, 13, 12.5}];
  p /. sol]

pressure[2209., 2.]
(*  1.00249  *)
POSTED BY: Updating Name
Answer
17 days ago

Excellent explanation. Thank you also for the links.

POSTED BY: Glenn Carlson
Answer
16 days ago

Group Abstract Group Abstract