Message Boards Message Boards

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

What type of command do I use to repeat a function until it stop?

Posted 10 years ago

Hello community.

As I remember for other types of software, I can do a loop to repeat the equation until it satisfy a current condition.

So now I have an equation that I need repeat the function until it satisfy the condition. I have check Mathematica tutorials and I can't seem to find a best method (maybe I still don't understand the functions completely). Here is what I want to do.

    Tinitial = (*any value*)
f[t_] = (*something*)
(*Obtain Tnew*)
Tnew=Tinitial -> False

Replace Tinitial with Tnew

Repeat Loop until Tnew=Tinitial -> True
Stop

Or Stop when Looping of function reached N-loops So any suggestions?

POSTED BY: Thai Kee Gan
3 Replies
Posted 10 years ago

Well, I tried FixedPoint[], but it only gives what was in the function only

Below is an example of my equation, which is also from another thread http://community.wolfram.com/groups/-/m/t/421465

(*Temperature of water*)
Twater = 500
Cwater = QuantityMagnitude[
  ThermodynamicData["Water", 
   "ThermalConductivity", {"Temperature" -> 
     Quantity[Twater, "Kelvins"]}]]

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 - 18000*Cwater), 
        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.};
Show[ListPlot[Evaluate[{#, f[#]} & /@ Range[60., 100., 0.05]], 
  Joined -> True, Frame -> True, FrameLabel -> {"Z", "Y[3]"}], 
 Graphics[{PointSize[Large], Red, Point[point]}], ImageSize -> 400]

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

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

If i use FixedPoint, how do I get Tnew to replace Twater and repeat again until it stop when Tnew=Twater?

POSTED BY: Thai Kee Gan

As I remember for other types of software, I can do a loop to repeat the equation until it satisfy a current condition.

Why can't you use a While ?

tStop = 10;
t = 0;
keepLooping = True;

While[ keepLooping,
 t++;
 If[t >= tStop,
  keepLooping = False,
  Print["I am still looping !"]
  ]
 ]
POSTED BY: Nasser M. Abbasi
Posted 10 years ago

Thank you for your suggestions, I will try that. I have not thought of While, been occupied with For, Do, Nest and FixedPoint. How do While command replace Tnew into Twater?

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