Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'd like to write a function that would perform a loop, I guess Do or While should be helpful here. I've simplified the problem, so that it is clear what I'd like to achieve. First, I have a simple function:

f[{x_, y_}] := {x + 1, y + 1};

Then I'd like to have another function to perform Nest operation on a given list.

f1[{a_, b_}] := Block[{c, d},
 c = Sqrt[a];
 d = Nest[f, {c, b}, 10];

Now I'd like to develop f1 in the following manner - if d[[1]] does not lie within a certain interval, let's say {49.5,50.5}, I'd like the function to perform Nest operation again, this time with starting value of c=c+1 or c=c-1 depending on whether d[[1]] was below or above the given interval. This should go on until d[[1]] lies within this interval, and the function should return list {c,b} with that value of c that enabled d[[1]] to lie within the interval. The algorithm is pretty simple, but since I'm not a programmer, it is quite difficult for me to execute in Mathematica.

If anybody is interested, it's about counter current heat exchanger - I know the inlet temperature value, but I perform calculations backwards, c=Sqrt[a] is my initial guess on the temperature at the outlet. Thanks for any advice!


So far I developed something like this, which actually works:

f1[{a_, b_}] := Module[{c, d, abc},
  c = Sqrt[a];
  d = Nest[f, {N[c], b}, 10];
  abc = Interval[{49.5, 50.5}];
  While[Not[IntervalMemberQ[abc, d[[1]]]], If[d[[1]] < Min[abc], c = c + 1, c = c - 1]; 
  d = Nest[f, {N[c], b}, 10]];

I'm not sure if it's the best way to do that. Thanks for any comments.

share|improve this question
Your function f1 is not going to return anything with the ; after the Nest. There is no need for the local symbol d either. Why are you using Block? I'd suggest Module, Block is a specialized construct which does some unusual/unexpected things. –  Ymareth Dec 16 '13 at 10:06
There is a syntax error; interval should be Interval[{49.5,50.5}]. This means loop will neve exit. I cannot see how your code deals with coming from above, e.g a>50.5^2. If starts above, code increases value of d[[1]] and condition will never be met. Aplogies if I have misinterpreted. –  ubpdqn Dec 16 '13 at 11:48
Thanks, simple mistake. As it is right now, it does not deal with coming from above, so that's what I'm asking. –  Wojciech Dec 16 '13 at 12:28
Why is d = Nest[f, {N[c], b}, 10]; performed above the While block (as well as below)? Presumably if c is too high this will just push it higher. –  Ymareth Dec 16 '13 at 15:46
@Ymareth I'm not sure whether I understand You, if c does not lie within the interval and it is not lower than Min value of the interval that means that it is higher than Max value of the interval, so c will decrease in the next steps. –  Wojciech Dec 16 '13 at 16:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.