Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 6 down vote accepted

Here's a way:

f[x_] := x^2 - 15


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

func can achieve a negative value

Otherwise, it evaluates:

ff[x_]:=x^2+2

Catch[FindMinimum[ff[x], x, StepMonitor :> 
   If[Negative@ff@x, 
   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"]]]

posFindMinimum[f,{x}]

f can achieve a negative value

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

Your Answer

 
discard

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.