Message Boards Message Boards

2 Replies
0 Total Likes
View groups...
Share this post:

List of execution outcomes

Posted 10 years ago
I am applying a function (NMinimize) to each element of a list. The function converges for some parameters (elements of the list) but not for others. However, the function always gives me a solution even if it does not converge. I get some error messages when the function doesn't converge, but they don't tell me which elements of the list failed. See the sample code below:
f[{a_, b_, c_}] := NMinimize[a x^2 + b x + c, x];
l = {{1, 2, 3}, {-1, 2, 3}};
f /@ l
{{2., {x -> -1.}}, {-1.70354*10^211, {x -> -4.1274*10^105}}}
Is there a way to generate a list which tells me whether or not each element in the input list converged (executed successfully) or not?
POSTED BY: Cuneyt Eroglu
2 Replies
How about
f[{a_, b_, c_}] := Quiet[Check[NMinimize[a x^2 + b x + c, x], NMinimize::cvdiv]]
l = {{1, 2, 3}, {-1, 2, 3}};
r = f /@ l
which now returns
{{2., {x -> -1.}}, NMinimize::cvdiv}
So you can simply discard all theNMinimize::cvdiv entries. You can also use your own error message
f[{a_, b_, c_}] := Quiet[Check[NMinimize[a x^2 + b x + c, x], "opps", NMinimize::cvdiv]]
{{2., {x -> -1.}}, "opps"}
Now Cases[] can be used to select the good results if needed. The position of each the "opps"  or NMinimize::cvdiv is the same as the data position. This way you can also know which data failed entry failed. You can use `Position` to determine this.
POSTED BY: Nasser M. Abbasi
Posted 10 years ago
Thank you! I really appreciate this.
POSTED BY: Cuneyt Eroglu
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract