Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to determine if a function f[x] can take negative values. f is expensive to compute and x may be high-dimensional.

I thought to use FindMinimum to minimize with respect to x. If the minimum is positive I have an answer.

But I do not want to proceed with the costly minimization if, en route, an evaluation of f produces a negative value. I want to return to the calling program with the information:

f can achieve a negative value.

How could I do this?

share|improve this question
up vote 6 down vote accepted

Here's a way:

f[x_] := x^2 - 15

Catch[FindMinimum[f[x], x, StepMonitor :> 
   Throw[Print@"func can achieve a negative value"]]]]

func can achieve a negative value

Otherwise, it evaluates:


Catch[FindMinimum[ff[x], x, StepMonitor :> 
   Throw[Print@"func can achieve a negative value"]]]]

{2., {x -> 0.}}

To wrap it in a function:

posFindMinimum[func_, args_?ListQ] :=
  Catch@FindMinimum[func @@ args, args, 
  StepMonitor :> 
  If[Negative@(func @@ args), 
  Throw[ToString@func <> " can achieve a negative value"]]]


f can achieve a negative value

share|improve this answer
This works for me. Thanks. – user7541 Jun 28 '13 at 15:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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