Message Boards Message Boards

GROUPS:

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

Posted 5 months ago
646 Views
|
2 Replies
|
2 Total Likes
|

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]
2 Replies

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 5 months ago

Excellent explanation. Thank you also for the links.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract