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

**URL:** <https://community.wolfram.com/t/how-do-i-prevent-findminimum-from-calling-my-function-for-non-numeric-args/6833>\
**Category:** Questions\
**Tags:** mathematica, numerical-computation\
**Created:** [October 3, 2015, 8:14am UTC](https://community.wolfram.com/t/how-do-i-prevent-findminimum-from-calling-my-function-for-non-numeric-args/6833 "2015-10-03T08:14:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tikhonov](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@tikhonov](https://community.wolfram.com/u/tikhonov)\
**Post date:** [October 3, 2015, 8:14am UTC](https://community.wolfram.com/t/how-do-i-prevent-findminimum-from-calling-my-function-for-non-numeric-args/6833/1 "2015-10-03T08:14:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tikhonov](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@tikhonov](https://community.wolfram.com/u/tikhonov)\
**Post date:** [October 3, 2015, 8:47am UTC](https://community.wolfram.com/t/how-do-i-prevent-findminimum-from-calling-my-function-for-non-numeric-args/6833/2 "2015-10-03T08:47:54Z")

</div>

Tried to post this on StackExchange and in the process of doing so, found a link to [Wolfram Support Quick Answers: How do I use ?NumericQ to affect order of evaluation?](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!
