3
$\begingroup$

I have a function (T[p_]) that calculates a temperature from a given pressure.

I have a table with a column of pressures and an associated temperature (PTFeO).

I would like to apply my temperature (T[p_]) function to the column of pressures and compare the calculated temperatures to each associated temperature in the table.

If the calculated temperature is greater than its associated temperature, then I would like to omit that row from the table.

Do you guys have any suggestions? I know I will need to do a for-loop, but I'm having trouble beginning.

Temperature function:

T[P_] := -5.104*(P*0.1)^2 + 132.899*(P*0.1) + 1120.661;

PTFeO = Transpose[{p1, t1}]

{{39, 1640}, {39, 1650}, {39, 1660}, {39, 1670}, {39, 1680}, {39, 1690}, {39, 1700}, {39, 1710}, {39, 1720}, {39, 1730}, {39, 1740}, {39, 1750}, {41, 1610}, {41, 1620}, {41, 1630}, {41, 1780}, {41, 1790}, {41, 1800}, {43, 1600}, {43, 1610}, {43, 1830}, {43, 1840}, {43, 1850}, {45, 1590}, {45, 1600}, {45, 1870}, {45, 1880}, {47, 1580}, {47, 1590}, {47, 1910}, {47, 1920}, {49, 1580}, {49, 1940}, {49, 1950}, {51, 1570}, {51, 1580}, {51, 1980}, {53, 1570}, {55, 1570}, {57, 1570}, {59, 1570}, {61, 1570}, {63, 1570}, {65, 1570}, {67, 1570}, {69, 1570}}

Thanks!

$\endgroup$
3
  • $\begingroup$ A for-loop is the last thing you need in Mathematica :) Please post a small, copyable pressure-temperature table in the format you are using. Then we can help. $\endgroup$ Apr 29 '18 at 15:58
  • $\begingroup$ @MariusLadegårdMeyer, I've just added it. Thanks again! $\endgroup$ Apr 29 '18 at 16:10
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Apr 29 '18 at 20:25
7
$\begingroup$

With:

data = {
    {39,1640},{39,1650},{39,1660},{39,1670},{39,1680},{39,1690},{39,1700},
    {39,1710},{39,1720},{39,1730},{39,1740},{39,1750},{41,1610},{41,1620},
    {41,1630},{41,1780},{41,1790},{41,1800},{43,1600},{43,1610},{43,1830},
    {43,1840},{43,1850},{45,1590},{45,1600},{45,1870},{45,1880},{47,1580},
    {47,1590},{47,1910},{47,1920},{49,1580},{49,1940},{49,1950},{51,1570},
    {51,1580},{51,1980},{53,1570},{55,1570},{57,1570},{59,1570},{61,1570},
    {63,1570},{65,1570},{67,1570},{69,1570}
};

You can use DeleteCases:

DeleteCases[data, {p_, t_} /; T[p] > t]

{{39, 1640}, {39, 1650}, {39, 1660}, {39, 1670}, {39, 1680}, {39, 1690}, {39, 1700}, {39, 1710}, {39, 1720}, {39, 1730}, {39, 1740}, {39, 1750}, {41, 1610}, {41, 1620}, {41, 1630}, {41, 1780}, {41, 1790}, {41, 1800}, {43, 1600}, {43, 1610}, {43, 1830}, {43, 1840}, {43, 1850}, {45, 1870}, {45, 1880}, {47, 1910}, {47, 1920}, {49, 1940}, {49, 1950}, {51, 1980}}

$\endgroup$
2
  • $\begingroup$ Great, thank you so much!! Would you mind clarifying the /; in the DeleteCases code? $\endgroup$ Apr 29 '18 at 16:37
  • $\begingroup$ Look up Condition $\endgroup$
    – Carl Woll
    Apr 29 '18 at 16:40
5
$\begingroup$

You can also use UnitStep along with Pick.

p1 =Developer`ToPackedArray@{39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 41, 41, 41, 41, 41, 41, 43, 43, 43, 43, 43, 45, 45, 45, 45, 47, 47, 47, 47, 49, 49, 49, 51, 51, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69};

t1 = Developer`ToPackedArray@{1640, 1650, 1660, 1670, 1680, 1690, 1700, 1710, 1720, 1730, 1740, 1750, 1610, 1620, 1630, 1780, 1790, 1800, 1600, 1610, 1830, 1840, 1850, 1590, 1600, 1870, 1880, 1580, 1590, 1910, 1920, 1580, 1940, 1950, 1570, 1580, 1980, 1570, 1570, 1570, 1570, 1570, 1570, 1570, 1570, 1570};

pickers = UnitStep[t1 - T[p1]];
Transpose[{Pick[p1, pickers, 1], Pick[t1, pickers, 1]}]

{{39, 1640}, {39, 1650}, {39, 1660}, {39, 1670}, {39, 1680}, {39, 1690}, {39, 1700}, {39, 1710}, {39, 1720}, {39, 1730}, {39, 1740}, {39, 1750}, {41, 1610}, {41, 1620}, {41, 1630}, {41, 1780}, {41, 1790}, {41, 1800}, {43, 1600}, {43, 1610}, {43, 1830}, {43, 1840}, {43, 1850}, {45, 1870}, {45, 1880}, {47, 1910}, {47, 1920}, {49, 1940}, {49, 1950}, {51, 1980}}

picker contains 1 at all positions i where T[p1[[i]]] >= t1[[i]] and 0 else. Pick[p1, pickers, 1] picks only those elements of p1 where pickers has 1 as entry.

I prefer this method since it does not unpack arrays. This should probably not bother you but here is an illustration of the performance difference for longer list:

p1 = RandomInteger[{39, 69}, 100000];
t1 = RandomInteger[{1570, 1980}, 100000];

a = With[{pickers = UnitStep[T[p1] - t1]},
    Transpose[{Pick[p1, pickers, 0], Pick[t1, pickers, 0]}]
    ]; // RepeatedTiming // First

b = DeleteCases[Transpose[{p1, t1}], {p_, t_} /; T[p] > t]; // RepeatedTiming // First

a == b

0.00396

0.299

True

$\endgroup$
1
  • $\begingroup$ If you have lots of data this is definitely the way to go. +1 $\endgroup$ Apr 29 '18 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.