Message Boards Message Boards

How do I repeat/loop multiple functions that are related?

Posted 9 years ago

Hello guys, I'm stuck with this question for some time. For my equation, I need to use WolframAlpha for my data. Firstly, I fix my initial water temperature as 500 Kelvin. Eg:

Twater = 300
Cwater = QuantityMagnitude[
  ThermodynamicData["Water", 
   "ThermalConductivity", {"Temperature" -> 
     Quantity[Twater, "Kelvins"]}]]

With Cwater determined, I can implement the next 2 functions

Dwater = (5 Cwater)/\[Pi]
Ewater = Dwater^3 + 2 Cwater

Then by using ParametricNDSolveValue, I can determine the value of Z

ClassicalRungeKuttaCoefficients[4, prec_] := 
  With[{amat = {{1/2}, {0, 1/2}, {0, 0, 1}}, 
    bvec = {1/6, 1/3, 1/3, 1/6}, cvec = {1/2, 1/2, 1}}, 
   N[{amat, bvec, cvec}, prec]];

f = ParametricNDSolveValue[{Derivative[1][y][x] == 
     Piecewise[{{(y[x] + x^3 + 3 z - 120*Ewater), 0 <= x <= 1},
       {(y[x] + x^2 + 2 z), 1 <= x <= 2},
       {(y[x] + x + z), 2 <= x <= 3}}],
    y[0] == 0},
   y[3.],
   {x, 0., 3.},
   z,
   Method -> {"ExplicitRungeKutta", "DifferenceOrder" -> 4, 
     "Coefficients" -> ClassicalRungeKuttaCoefficients}, 
   StartingStepSize -> 1/10];

point = {z /. FindRoot[f[z] == 100., {z, 1}, Evaluated -> False], 
   100.};

FindRoot[f[z] == 100., {z, 1}, Evaluated -> False]

Lastly, to find the new temperature of water

Tnew = 170 + 1.89*z /. 
  FindRoot[f[z] == 100., {z, 1}, Evaluated -> False]

I wanted to repeat Twater with Tnew until Twater=Tnew->True and the loop stop. I need to start Twater with a value, (only to replace Twater=Tnew after the first equation and keep press shift+enter repeatedly)

I've tried some Do Loop or For Loop and even FixedPoint but still don't know how to combine multiple functions/equations into 1 loop.... I apologize because I've been asking this question a few times, or maybe some of you have answered me in some way, but I still did not understand how to do this. Thank you very much for your time.

POSTED BY: Thai Kee Gan
12 Replies

Mathematica has a short tutorial entitled "Arbitrary-Precision Numbers." You can find it by searching for "tutorial/ArbitraryPrecisionNumbers" in Help/Wolfram Documentation or by typing SetPrecision, highlighting it, pressing F1, then find the link for the tutorial at the bottom of the page.

POSTED BY: Charles Elliott
Posted 9 years ago

Thanks again for your help and your advice, Dr. Charles Elliott (correct me if I'm wrong). I will try to digest everything. Hopefully.

POSTED BY: Thai Kee Gan
Attachments:
POSTED BY: Charles Elliott
Posted 9 years ago
POSTED BY: Thai Kee Gan
POSTED BY: Charles Elliott
Posted 9 years ago

Hello Charles, thank you for your advice. You are correct about my background, that i have not been exposed to numerical analysis as much as I supposed to have. In the future, I will definitely take up PhD program if there is a chance.

POSTED BY: Thai Kee Gan
Posted 9 years ago
POSTED BY: Thai Kee Gan
POSTED BY: Charles Elliott
Posted 9 years ago

Hold on..... I suppose when we replace Twater with Tnew, then

While[(Abs[Twater - Tnew] > tol) && (iters++ < iterLim),

will not hold True anymore, that is why the iteration only run once.... So, how do I change it?, or I can just use the conventional method, which is much more "messy"?

n = 5; While[0 < n - 1 < 9, Print[n]; n = n - 0.5]
POSTED BY: Thai Kee Gan
Posted 9 years ago
POSTED BY: Thai Kee Gan
POSTED BY: Charles Elliott
Posted 9 years ago

I was wondering if Module[] has something to do with it. It's just that the examples given in the documentation was just for simple tasks. I'm not in my Uni's lab right now (since the software was bought by Uni), but I'll get to it as soon as I can and let you know the result. Thank you very much.

POSTED BY: Thai Kee Gan
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