# [✓] Use NestWhile using a function with 2 arguments one sets parameters?

GROUPS:
 I want to use gf[x_, y_] := Module[{x1, x2, xtst, fnw, f1, xnw}, xtst = Total[x]/2;fnw = LAcr[xtst, 1/12., LAI, LBI, AF, a, b, c, d, 400000.,a, y]; f1 = LAcr[x[[1]], 1/12., LAI, LBI, AF, a, b, c, d, 400000., a, y]]; Return[If[Sign[fnw] == Sign[f1], {xtst, x[[2]]}, {x[[1]], xtst}]];] as the input to NestWhile[ gf, {500., 50000.}, #[[2]] - #[[1]] > 0.0001 &] where the return parameters are {xlowerfinal,xupperfinal} are the final values of { #[[1]],#[[2]]} and the value of y is to be supplied before entering NestWhile by using the Table function to introduce a list of y values. Can one convert a a 2 variable (here x, y) function into a 1 variable function of x by automatically introducing the y values using Table? Or is there some way of making ListWhile know which variable (x) to use?
26 days ago
4 Replies
 Bill Simpson 1 Vote A common trick to distinguish names when you have more than one # or more than one x is to introduce a named function.So try changing your NestWhile[ gf, {500., 50000.}, #[[2]] - #[[1]] > 0.0001 &] to test[p_] := p[[2]] - p[[1]] > 0.0001; NestWhile[f, {500., 50000.}, test] and now you could use a separate # inside your NestWhile without confusion.Next the trick to get NestWhile to work with two arguments is to give it a list of two items instead of two individual items. Thus gf[{x_, y_}]:= instead of gf[x_, y_] and then modify your NestWhile arguments to match.If you think about this a bit then it should give you enough of a hint that you can make this work.
 I apologize for not following the rules. The code for my actual problem using the hints by Bill Simpson follows. Unfortunately I seem to not be able to think of a simple example. This is the solution to the problem I posted earlier today using the code for the actual problem. The functions and input paramaters are defined in another notebook. gf\[Beta][{x_, \[Beta]_}] := Module[{x1, x2, xtst, fnw, f1, xnw}, xtst = Total[x]/2; fnw = LAcr[xtst, 1/12., LAI, LBI, AF, a, b, c, d, 400000., \[Alpha]0, \[Beta]]; f1 = LAcr[x[[1]], 1/12., LAI, LBI, AF, a, b, c, d, 400000., \[Alpha]0, \[Beta]]; Return[If[Sign[fnw] == Sign[f1], {xtst, x[[2]]}, {x[[1]], xtst}]];] f1 = LAcr[x[[1]], 1/12., LAI, LBI, AF, a, b, c, d, 400000., \[Alpha]0, \[Beta]]; Return[If[Sign[fnw] == Sign[f1], {xtst, x[[2]]}, {x[[1]], xtst}]];] f[x_] := gf\[Beta][{x, .6}] t works slightly differently from previously. What used to work, doesn't. In[27]:= NestWhile[f, {500., 50000.}, test &] Out[27]= {500., 50000.} However, if I add a large integer it does. In[28]:= NestWhile[f, {500., 50000.}, test &, 100] Out[28]= {35964.7, 35964.7} The integer seems to be the number of iterations.enter code here In[29]:= NestWhile[f, {500., 50000.}, test &, 3] Out[29]= {25250., 37625.} `