3
$\begingroup$

I have problems with unwanted outputs of NMaximize. I tried to use the command Quiet[] and Off[] in order to turn off the warnings and unwanted outputs but no avail.

You can see the NMaximize part of my code here:

enter image description here

Parallelize[Do[Print[NMaximize[{Abs[
   eg[1.5032*10^-4, a, b, m, s] - e1g[1.5032*10^-4, a, b, m, s]]/
  eg[1.5032*10^-4, a, b, m, s], {constraint[a, b, m, s] < 
   1*10^-3, -20 <= a <= 20, 1 <= b <= 10, 
  1*10^-9 <= m <= 500*10^-9, 1*10^-9 <= s <= 500*10^-9, s < m, 
  0.01*m < s}}, {a, b, m, s}, MaxIterations -> 1000, 
"RandomSeed" -> i]], {i, 1000}]]

Most of the time it produces reasonable outputs such as

{0.321066,{a->-11.9257,b->9.97032,m->3.96839*10^-7,s->3.50421*10^-7}}

and sometimes just the copy of the NMaximize code with chosen numbers as (see the figure above):

(kernel 1) NMaximize[{Abs[NIntegrate[myfun1[y,...

My problem is that the output that I have in another code is so huge. And I cannot run my code properly due to this unwanted output (it takes thousand lines in editor per unwanted output).

The problem is even more significant because I have many such outputs and in the end the editor is not reachable anymore and mathematica disappears.

Is there any way to tell mathematica NOT to output such unwanted outputs? Or if it must output, then to delete this output immediately right after outputing it?

$\endgroup$
6
  • $\begingroup$ Why not add something like If[ListQ[res = NMaximize[(* stuff *)]], Print[res]]? $\endgroup$ Commented Dec 23, 2021 at 13:43
  • $\begingroup$ @J.M. you mean stuff as the unwanted output? There stuff is chaning. In fact only the RandomSeed->i here i is changing. I need to adjust it to the variable i. Where should I add this code part? I posted the related code part. I can also post all the code but it takes time to reach this unwanted part. $\endgroup$ Commented Dec 23, 2021 at 13:48
  • $\begingroup$ (* stuff *) is whatever complicated thing you had in NMaximize[] that I didn't want to bother writing out in full; I was assuming you'd know to try modifying the inner part of your loop to something like it. $\endgroup$ Commented Dec 23, 2021 at 13:51
  • $\begingroup$ @J.M. ok. I think this is a very good idea and I guess I can do it. So basically Mathematica can freely output it and one must be prepared of this thing. I think this should not appear at all. It is not helpful at all. All I know is that kernel 1 failed to give any output at iteration xyz. $\endgroup$ Commented Dec 23, 2021 at 14:00
  • $\begingroup$ It's not like you have advance knowledge of which kernel and which iteration would give a dud result. That's what conditional structures and predicate checks are for, among other things. $\endgroup$ Commented Dec 23, 2021 at 14:01

1 Answer 1

1
$\begingroup$

I couldn't find a duplicate along the lines of "How to check if a function worked?" It's a basic question, but not one that is easy to look up in the docs, imo. Many have figured this out for themselves, but I think the question deserves an answer on the site.

There are a couple of standard(?) approaches. You can test if the result has the expected form, or you can test if the result is free of the function (assuming it should be).

@J.M. in a comment suggest the former:

res = function[args...];
If[!ListQ[res],  (* if a successful function[] call returns a List *)
 res = $Failed]; (* use whatever signal for failure you wish *)

For functions that do not return lists, you would have to use a different check other than !ListQ[res].

The latter is typically what I do:

res = function[args...];
If[!FreeQ[res, function],
 res = $Failed];

Most functions when they evaluate successfully return an expression that does not contain the function in it. Some functions may return unevaluated or return $Failed or Failure[] etc. (NDSolve`ProcessEquations comes to mind.) In that case, I use something like !FreeQ[res, function | $Failed] as the test for success. There is also FailureQ[] if a function always returns an expression indicating failure.

$\endgroup$
1
  • $\begingroup$ Thank you very much for the answer. I agree that I also havent found a similar question. $\endgroup$ Commented Dec 29, 2021 at 16:26

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.