# How to stop the result?

My program allows me to solve the equation Ht. The aim is to show the probability for each value of d value in my dlist table (dmin and dmax). But, for certain values ​​of d, I have a first negative probability then two other positive ones. What i want, is that my program show no others p (prabability) results when the first probability is negative. that's means, if for a d value the first probability is negative, i need the program show only this negatif probability value and no others probability values.

Clear["Global*"]

c = 0.058;
rc = -0.06;
th = 0.80;

g1[ε_] = 2*(ε + 0.5)*(ε + 0.5);

g2[ε_] = 1 - 2*(-ε + 0.5)*(-ε + 0.5);

dmin = 1;
dmax = 3;
pas = 0.01;
dlist = Table[d, {d, dmin, dmax, pas}]
Length[dlist]

f[d_] = Min[{-0.02 + 0.0231397 d, 0.038524}];

rt = Simplify[((1 + rc) (1 - p th ))/(1 - p) - 1]

Ht = (rt - c) d - f[d]

k[d_, p_] = g1[Ht] - p

For[i = 1, i < Length[dlist] + 1,
i++, {Print[i "ème d fixé = ", dlist[[i]]],
Print[NSolve[k[dlist[[i]], p] == 0, p, Reals]]}]

k[d_, p_] = g2 [Ht] - p

For[i = 1, i < Length[dlist] + 1,
i++, {Print[i "ème d fixé = ", dlist[[i]]],
Print[NSolve[k[dlist[[i]], p] == 0, p, Reals]]}]

• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey May 21 at 16:13

This can be accomplished by replacing

NSolve[k[dlist[[i]], p] == 0, p, Reals]


by

If[t = NSolve[k[dlist[[i]], p] == 0, p, Reals]; (p /. First[t]) < 0, First[t], Flatten[t]]


in the Print statements. This can be tested by

NSolve[k[dlist[], p] == 0, p, Reals]
If[t = NSolve[k[dlist[], p] == 0, p, Reals]; (p /. First[t]) < 0, First[t], Flatten[t]]
(* {{p -> 0.0274673}, {p -> 0.170184}, {p -> 0.775767}} *)
(* {p -> 0.0274673, p -> 0.170184, p -> 0.775767} *)

NSolve[k[dlist[], p] == 0, p, Reals]
If[t = NSolve[k[dlist[], p] == 0, p, Reals]; (p /. First[t]) < 0, First[t], Flatten[t]]
(* {{p -> -0.00249263}, {p -> 0.187624}, {p -> 0.775012}} *)
(* {p -> -0.00249263} *)


Note that I have used Flatten to eliminate an unnecessary set of curly brackets. Tor keep them, simply remove Flatten. I hope this helps.

• Hi Frey! Sorry for my late answer, iam a little bit ill this days. I've tried your code but could not found the same result. how did you do from the loop? did you keep the same structure as mine? – EmilioDas May 23 at 15:29
• I used For[i = 1, i < Length[dlist] + 1, i++, {Print[i "ème d fixé = ", dlist[[i]]], Print[If[t = NSolve[k[dlist[[i]], p] == 0, p, Reals]; (p /. First[t]) < 0, First[t], Flatten[t]]]}]. The values returned for i = 61 and i = 62 of the g2` computation are those in my answer. – bbgodfrey May 23 at 18:55
• Okay Frey, Thnaks a lot for the hand. Have a nice Day! – EmilioDas May 24 at 8:45