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.