0
|
8446 Views
|
9 Replies
|
4 Total Likes
View groups...
Share
GROUPS:

# NestWhile Question

Posted 10 years ago
 How can I use NestWhile to apply a function repeatedly until the first element of the argument stops decreasing?
9 Replies
Sort By:
Posted 10 years ago
 Hello, I'm also a new user to Mathematica and I have the same problem to use NestWhile in my equations. Please help me in the link below. Much help is needed and appreciated. Thank you very much.http://community.wolfram.com/groups/-/m/t/434758
Posted 10 years ago
 Thanks again.
Posted 10 years ago
 In[178]:= Clear[fKampas] (* increases below 5 now *) fKampas[x_] := Which[x[[1]] >= 5, {x[[1]] - 1/10, x[[2]] + 1/10},    x[[1]] < 5, {x[[1]] + 1, x[[2]] + 1/10}] /; VectorQ[x, NumericQ] && Length[x] > 1  In[180]:= Clear[tKampas] tKampas[r1_, r2_] := First[r2] < First[r1]  In[182]:= NestWhile[fKampas, {7, 9}, tKampas, 2]Out[182]= {59/10, 56/5}In[183]:= NestWhile[fKampas, {7, 9}, tKampas, 2] == NestWhileList[fKampas, {7, 9}, tKampas, 2][[-1]]Out[183]= Truethe result of NestWhile - where the test tKampas fails - is not the local mininum; the local minimum is the value before:In[184]:= NestWhileList[fKampas, {7, 9}, tKampas, 2][[-2]]Out[184]= {49/10, 111/10}
Posted 10 years ago
 You want to get the absolute minimum in the first argument?Since the first element of the argument can decrease and then increase, to get the argument with the smallest first element,The documentation saysNestWhile[f,expr,test,m,max,n] applies f an additional n times after test fails, or max applications have already been performed. NestWhile[f,expr,test,m,max,-n] is equivalent to Part[NestWhileList[f,expr,test,m,max],-n-1].NestWhile stops when the test fails (for the first time), one can order some flavours when testing has to start, how much iterations have to be performed and how much extra iterations have to be performed. But the main thing is the test function: instead of #2[[1]]<#1[[1]]& you may define a test function tKampas having as much arguments as the argument of NestWhile behind the test function says. So as before In[77]:= Clear[fKampas] fKampas[x_] :=  If[x[[1]] > 5,      {x[[1]] - 1/10, x[[2]] + 1/10},      {x[[1]], x[[2]] + 1/10}] /; VectorQ[x, NumericQ] && Length[x] > 1  In[79]:= Clear[tKampas] tKampas[r1_, r2_] := First[r2] < First[r1]  In[81]:= NestWhile[fKampas, {7, 9}, tKampas, 2]Out[81]= {5, 111/10}to find a global minimum instead of a local in the first argument with NestWhile, the test function simply has to reflect that, and tKampas does not do it. tKampas finds a local minimum. The thing is, only the results in the result store can be tested: because one does not know, where the global minimum is, one does also not know how much steps (results) must  be compared within the test function. Possibly NestWhile is not the right choice to find a global minimum. There are functions like FindMinimum[], which could be taken into consideration.Just a correction: NestWhile[f,expr,test,All] supplies all results so far as arguments for test at each step.
Posted 10 years ago
 What I what to find is the first local minimum.
Posted 10 years ago
 Since the first element of the argument can decrease and then increase, to get the argument with the smallest first element,I guess I need to useNestWhile[fKampas, {3, 9}, #2[[1]] < #1[[1]] &, 2, nmax, -1]where nmax is the maximum number of function applications.  Is that correct?
Posted 10 years ago
 Have to admit I found the documentation confusing for NestWhile.Once a young man begged God: Please, make me a famous author! People should have deep feelings reading my texts, they should scream and laugh! God said: Okay, here you are. The young man awakened and found himself being a documentation writer.
Posted 10 years ago
 Thanks.  Have to admit I found the documentation confusing for NestWhile.
Posted 10 years ago
 In the following way:In[26]:= Clear[fKampas]fKampas[x_] := If[x[[1]] > 5,    {x[[1]] - 1/10, x[[2]] + 1/10},   {x[[1]], x[[2]] + 1/10}] /; VectorQ[x, NumericQ] && Length[x] > 1In[28]:= NestWhile[fKampas, {7, 9}, #2[[1]] < #1[[1]]&, 2]Out[28]= {5, 111/10}Slot 2 is the current result, Slot 1 is the penultimate result. The last "2" in NestWhile[] says  that two results are in the result store  for comparison. Mma stops correctly when the first argument of a result does no longer decrease, the second argument of a result increases all the time. If done in the constant area less than or equal to 5 for the first argument, what happens?In[29]:= NestWhile[fKampas, {3, 9}, #2[[1]] < #1[[1]] &, 2]Out[29]= {3, 91/10}Correct: {3,9} is the first (penultimate) result, {3, 91/10} the next (current) one, the test fails and the NestWhile stops.