# Need help looping a notebook evaluation

I'm a new user and would like to find the minimum value of soln1 obtained after repeating the following evaluation (below) for 1000 cycles (for instance). I can obtain sequential values of soln if I reevaluate the entire notebook manually each time, but I want to be able to automate this process until a minimum value of soln1 is reached. The difficulty that I am encountering is that the entire notebook requires evaluation in order to generate a different random integer each time- I am having trouble figuring out how to force the evaluation to include the randominteger step using commands like Table, etc. I'm probably missing something simple- any help would be appreciated. Thanks in advance...

Clear[w, t, q, soln1]

f1[t_] := 5*t;

w = RandomInteger[{1111, 9999}];
a = IntegerDigits[w][];
b = IntegerDigits[w][];
c = IntegerDigits[w][];
d = IntegerDigits[w][];
p = 0.1;
k = 0.6;

end1 = a;
p1 = end1 + p;
end2 = p1 + b;
p2 = end2 + p;
end3 = p2 + c;
p3 = end3 + p;
end4 = p3 + d;

soln1[t_] := Piecewise[{{f1[t], t < end1}, {k*f1[end1],
end1 <= t < p1}, {f1'[t]*(t - p1) + k*f1[end1],
p1 <= t < epnd2}, {k*f1[end2],
end2 <= t < p2}, {f1'[t]*(t - p2) + k*f1[end2],
p2 <= t < end3}, {k*f1[end3],
end3 <= t < p3}, {f1'[t]*(t - p3) + k*f1[end3], p3 <= t <= 30}}];

soln1

• Welcome to SE. It would help if you would explain in simple terms what soln1 is intended to do. What do you want to vary and what do you want to keep invariant as you iterate? You can and should eliminate several variables. a, b, c, and d are all produced by RandomInteger. No need for them to be defined outside soln1, if at all. – DavidC Dec 22 '13 at 16:39
• Have a look at Module, Do and CompoundExpression (;). – Sjoerd C. de Vries Dec 22 '13 at 17:22

This is equivalent to your code, and iterates dynamically:

f1[t_] := 5*t
p = 1/10;   k = 6/10; r = {};
SeedRandom;
Dynamic[ Refresh[
a = (IntegerDigits@RandomInteger[{1111, 9999}])[[;; 3]];
AppendTo[r, N[f1'*(30 - Tr@a - 3 p) + k*f1[Tr@a + 2 p]]];
{ListLinePlot@r, Min@r}]] Edit

Perhaps better:

r = {FromDigits@#, N[f1'*(30 - Tr@# - 3 p) + k*f1[Tr@# + 2 p]]} & /@
IntegerDigits@Range[110, 999];
{ListLinePlot@r, Pick[r, Thread[#[] == Min[r[[All, 2]]]] & /@ r]} • belisarius, rereading your comments regarding "Hat Dash" I had a good chuckle seeing as you are sporting a "hat" this year. XD – Mr.Wizard Dec 22 '13 at 18:36
• Thanks to all for the comments. Belisarius, your code is exactly what I needed- I learned a lot by examining it. Is there any way that I could get a read out of the actual random integer value that is associated with the min@r value? Thanks again. Steve – SteveC Dec 22 '13 at 19:25
• @user11287 Just change {ListLinePlot@r, Min@r} by {ListLinePlot[r[[All, 2]]], {FromDigits@#[], #[]} & /@ Select[r, #[] == Min[r[[All, 2]]] &]} – Dr. belisarius Dec 22 '13 at 20:03
• @Mr.Wizard I'm NOT supporting it. The overall effects on the site don't look good. I'm just trying it to understand it better. – Dr. belisarius Dec 22 '13 at 20:04
• – Dr. belisarius Dec 22 '13 at 20:20