List of execution outcomes

Posted 9 years ago
3656 Views
|
2 Replies
|
0 Total Likes
|
 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?
2 Replies
Sort By:
Posted 9 years ago
 How aboutf[{a_, b_, c_}] := Quiet[Check[NMinimize[a x^2 + b x + c, x], NMinimize::cvdiv]]l = {{1, 2, 3}, {-1, 2, 3}};r = f /@ lwhich now returns{{2., {x -> -1.}}, NMinimize::cvdiv}So you can simply discard all theNMinimize::cvdiv entries. You can also use your own error messagef[{a_, b_, c_}] := Quiet[Check[NMinimize[a x^2 + b x + c, x], "opps", NMinimize::cvdiv]]returns{{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 9 years ago
 Thank you! I really appreciate this.