Message Boards Message Boards

0
|
6711 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

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

Posted 6 years ago

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
2 Replies
Posted 6 years 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
Posted 6 years ago

Excellent explanation. Thank you also for the links.

POSTED BY: Glenn Carlson
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