# Exiting FindMinimum when a condition is met

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?

-

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

-
This works for me. Thanks. – user7541 Jun 28 '13 at 15:35