# Using NestList?

Here is the code that I have so far:

fun[{data_, p_, n_, min_, max_}] :=
Module[{}, lstplt = LinearModelFit[data, Table[x^i, {i, n}], x];
Print[lstplt[x]]
Print["Fit plotted from min to max temperature in data set range"]
Print[Plot[lstplt[x], {x, min, max}]];
reslist =
Inner[List, {data}[[1, All, 1]], lstplt["FitResiduals"], List];
Print["Fit and data curve"]
Print[Show[ListPlot[data], Plot[lstplt[x], {x, min, max}]]];
Print["Residual Plot"]
Print[ListPlot[reslist]];
bres = Select[reslist, Abs[#[[2]]] > p &];
gres = DeleteCases[reslist, Alternatives @@ bres];
gpoints = gres[[All, 1]] \[Intersection] reslist[[All, 1]];
Print["New data curve excluding points that had poor residual \
values"]
Print[ListPlot[
dataset13 = Select[data, gpoints~MemberQ~First[#] &]]];
Print["New data set"];
Print["Number of points"];
Print[Length[dataset13]];
Return[dataset13];]
data = Table[{x, RandomReal[{-.1, .1}] + x^2}, {x, 0, 15}];


I want to be able to do the following line of code:

fun[fun[data, .04, 2, 0, 15], 1, 3, 0, 15]


without having to type in the argument over and over again. Would NestList be the best way to tackle this? It looks like a nest function problem to me, however I want to be able to change the n value by one after each iteration. How could I simplify this last line that I want to be able to do so that I can type it in one line and say continue to apply the function 'fun' until n has reached some value N?

• do you want p changing in each iteration or is it a typo in the last line?
– kglr
Jul 19, 2019 at 20:36

If p is fixed (at, say, p0) in each iteration and only n is changing, you can use Nest:

k = 3;
i = 1;
Nest[fun[#, p0, ++i, 0, 15] &, data, k]


fun[fun[fun[data, p0, 2, 0, 15], p0, 3, 0, 15], p0, 4, 0, 15]

Alternatively,

First @ Nest[{fun[#[[1]], p0, #[[2]], 0, 15], #[[2]] + 1} &, {data, 2},  k]


fun[fun[fun[data, p0, 2, 0, 15], p0, 3, 0, 15], p0, 4, 0, 15]

If p varies in each iteration taking values in a specified list:

plist = Array[p, 3];
nlist = Range[Length @ plist] + 1;

Fold[fun[#1, plist[[#2 - 1]], #2, 0, 15] &, data, nlist]


fun[fun[fun[data, p[1], 2, 0, 15], p[2], 3, 0, 15], p[3], 4, 0, 15]

Also

i = 1; Nest[fun[#, plist[[i]], ++i, 0, 15] &, data, 3]
Fold[fun[#1, #2[[1]], #2[[2]], 0, 15]&, data, Transpose[{nlist, plist}]]
Fold[fun[#1, ## & @@ #2, 0, 15] &, data, Transpose[{nlist, plist}]]


same result

• p will vary with each iteration, however the value will not be known until the previous iteration is done. How could I manipulate the function to represent this? I am trying to use Manipulate now and it is not giving a correct output. Jul 30, 2019 at 16:16
• how is the value of p determined given previous iteration?
– kglr
Jul 30, 2019 at 16:21
• There is no true criteria, I want to exclude outliers while keeping a majority of the points. It is really done from looking at the previous iteration residual graph and making a determination that way, which is why I wanted some dynamic variable to change the p value. Jul 30, 2019 at 19:22
m = 2;
Fold[fun[#1, #2, m++, 0, 15] &, data, {0.04, 1}]
(*    fun[fun[data, 0.04, 2, 0, 15], 1, 3, 0, 15]    *)


This Fold takes as the last argument the list of p values to run through, and it stops when this list is used up.

• Not exactly what I want the program to do. This only takes the output of the first iteration and takes it to a degree fit that is one value higher than before. I want to take the result of the first iteration, plug that into the function 'fun' again (for a second iteration) and have the third argument of 'fun' be increased by one for the second iteration. Then I want to continue this process until it has reached say 6 iterations for example. How could this be done? Jul 19, 2019 at 20:26
• So what to do with the second argument p? In your example there is a different value for p in every iteration. Jul 19, 2019 at 21:02