2
$\begingroup$

I'm a new user and would like to find the minimum value of soln1[30] obtained after repeating the following evaluation (below) for 1000 cycles (for instance). I can obtain sequential values of soln[30] 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[30] 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][[1]];
b = IntegerDigits[w][[2]];
c = IntegerDigits[w][[3]];
d = IntegerDigits[w][[4]];
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[30]
$\endgroup$
2
  • $\begingroup$ 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[9]. No need for them to be defined outside soln1, if at all. $\endgroup$
    – DavidC
    Commented Dec 22, 2013 at 16:39
  • $\begingroup$ Have a look at Module, Do and CompoundExpression (;). $\endgroup$ Commented Dec 22, 2013 at 17:22

1 Answer 1

2
$\begingroup$

This is equivalent to your code, and iterates dynamically:

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

Mathematica graphics

Edit

Perhaps better:

r = {FromDigits@#, N[f1'[30]*(30 - Tr@# - 3 p) + k*f1[Tr@# + 2 p]]} & /@ 
                                                             IntegerDigits@Range[110, 999];
{ListLinePlot@r, Pick[r, Thread[#[[2]] == Min[r[[All, 2]]]] & /@ r]}  

Mathematica graphics

$\endgroup$
6
  • $\begingroup$ belisarius, rereading your comments regarding "Hat Dash" I had a good chuckle seeing as you are sporting a "hat" this year. XD $\endgroup$
    – Mr.Wizard
    Commented Dec 22, 2013 at 18:36
  • $\begingroup$ 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 $\endgroup$
    – SteveC
    Commented Dec 22, 2013 at 19:25
  • $\begingroup$ @user11287 Just change {ListLinePlot@r, Min@r} by {ListLinePlot[r[[All, 2]]], {FromDigits@#[[1]], #[[2]]} & /@ Select[r, #[[2]] == Min[r[[All, 2]]] &]} $\endgroup$ Commented Dec 22, 2013 at 20:03
  • $\begingroup$ @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. $\endgroup$ Commented Dec 22, 2013 at 20:04
  • $\begingroup$ @Mr.Wizard chat.stackexchange.com/transcript/message/12783059#12783059 $\endgroup$ Commented Dec 22, 2013 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.