Message Boards Message Boards

0
|
4645 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

How do I prevent FindMinimum from calling my function for non-numeric args?

I'm trying to numerically minimize a user-defined function F = Function [{x,y}, (...)] using simple gradient descent or something similar.

F performs a complex computation that cannot be done symbolically. Thus, F[1,1] returns a number, but F[x,y] takes forever and gives lots of errors. Unfortunately, it seems that FindMinimum starts by calling my function for symbolic arguments, even though I provide a numeric starting point.

Simple example:

A = Function[{x}, Print["Called for x = ", x]; a0 = a./FindRoot[x-a,{a, 2 x}]; a0^2];  (* Basically x -> x^2 *)

A[2] returns 4, but A[x] returns errors (FindRoot: "Value 2 x in search specification is not a number")

And if I call

FindMinimum[A[x], {x,1}, Method->"PrincipalAxis", StepMonitor:>Print["Step to x=",x]]

I get

Called for x = x
(errors)
Step to x = 1.16573 10^-14
Step to x = 1.16573 10^-14
Step to x = 0.
Step to x = 0.
Out[1]: {0., {x->0.}}

How can I make sure the function is never called for "x = x", but were instead evaluated at every step, and only for numeric values of the arguments?

I tried telling my function to return Infinity for non-numeric arguments:

A = Function[{x}, 
        Print["Called for x = ", x]; 
        If[Not[NumericQ[x]], Infinity, 
        (*else*) a0 = a./FindRoot[x-a,{a, 2 x}]; a0^2]
    ];

But then here is what hapens:

A[1]
Called for x = 1
1.

A[x]
Called for x = x
Infinity

FindMinimum[A[x], {x,1}]
Error: FindMinimum::nrnum  The function value Infinity is not a real number at {x} = {1.}

I've been struggling with this forever and Googling turned up no answers. I suspect I don't understand the origin of the problem well enough to search for the right keywords... Sorry if a similar question was answered elsewhere.

POSTED BY: Mikhail Tikhonov

Tried to post this on StackExchange and in the process of doing so, found a link to http://support.wolfram.com/kb/12502.

Looks like for functions defined as

f[x_] := x^2

one can tell Mathematica to only evaluate the function for numeric arguments as follows:

f[x_?NumericQ] := x^2

So in my case, the following seems to work:

NewA [x_?NumericQ] := A[x];

In: NewA[2]
Out: 4

In: NewA[x]
Out: NewA[x]

FindMinimum[NewA[x],x]
Called for x = 1.
Called for x = 1.
Called for x = 1.
Called for x = 0.5
Called for x = 0.5
Called for x = 0.5
Called for x = -7.45058 10^-9
Called for x = -7.45058 10^-9
Called for x = 7.45058 10^-9

So the problem is solved! But if anyone has any suggestions on making this faster or more elegant, please share!

POSTED BY: Mikhail Tikhonov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract