I have a particularly difficult loop, in that it consists of two variables looping and sowing the result if a criteria is met. Overall it works for me, but some certain values for my variables take extremely long to compute, whilst most others go through instantly.

I would thus like to apply a time constraint for every calculation in my loop, but instead of aborting, i'd like for the loop to just break and continue to the next iteration.

How would i do this if my code looks like so:

Reap[Do[Do[(g = i;
o = 1;
Kbt = 1;
a = j;
p := Solve[(s^2) + g*(s^a) + (o^2) == 0, s];
b = Abs[Re[s /. p[[1]]]];
ot = Abs[Im[s /. p[[1]]]];
fi = Pi + ArcTan[(-1)*(ot/b)];
v = 2 ot - ((g*a*Sin[(1 - a)*fi])/((b^2) + (ot^2))^((1 - a)/2));
u = -2 b + ((g*a*Cos[(1 - a)*fi])/((b^2) + (ot^2))^((1 - a)/2));
Om = ArcTan[u/v];
B[r_] := (a big function);
H[t_] := (a big function);
fm = FindMinValue[H[t], {t, 1, 10}]);
  If[fm >= 0, Sow[{a , g}]; Break[]], {i, 0, 5, 0.01}], {j, 0.44, 
   0.98, 0.01}]]

This code gives me a load of points which i want to graph (the criteria for each point is that it is positive in the interval 1-10, which i determine with FindMinValue on said interval). However having it take days because of some points that could easily be replaced with the next iteration or previous one, is a tad tedious.

Thanks in advance

  • 2
    $\begingroup$ Have you profiled which step exactly is taking that long? Blindly, you could try using NSolve or FindRoot instead of Solve. Also, loops may not be the best method to go about your problem, if expressed in Mathematica. Could you rewrite your algorithm as a Table and parallelize it, or use other vector (e.g. Map) approaches? $\endgroup$ – MarcoB Nov 14 '16 at 21:56
  • $\begingroup$ Table didnt work for me well. It didnt want to let my function through.. might try again though. About the step, its usually on computing H[t_] and can happen on seemingly random values, where the i and j happen to form particularly difficult expressions. The way i do it now is evaluate only the center of my graphs points (the graph im expecting to get) and find the edges by hand, viewing graphs. EDIT: Scrach that... NSolve for the fractional polynome helps ALOT... ill look into it $\endgroup$ – Castle Nov 14 '16 at 22:04
  • 6
    $\begingroup$ How about TimeConstrained with CheckAbort? Example: Do[ CheckAbort[TimeConstrained[ Pause[Mod[i, 3]]; Print[i], 1.5], Null ], {i, 10} ] $\endgroup$ – Szabolcs Nov 14 '16 at 22:13
  • $\begingroup$ EDIT2: WOW NSolve was the key... i somehow missed the impact of Solve... THANKS! And ill check out the loop criteria too. It is still of interest to me on a larger scale $\endgroup$ – Castle Nov 14 '16 at 22:15
  • $\begingroup$ @Szabolcs That's a construct worthy to be an answer. $\endgroup$ – corey979 Nov 14 '16 at 23:09

How about TimeConstrained with CheckAbort? Example: Do[ CheckAbort[TimeConstrained[ Pause[Mod[i, 3]]; Print[i], 1.5], Null ], {i, 10} ] – Szabolcs Nov 14 at 22:13

Thanks loads!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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