From: ValeX on
let's say that i have a numerical function f[x] which is defined in a
quite messy way.
For example, it could be defined as a Module that contains If, Whiles,
interpolation functions, Import, ..
This function works well without any error if I simply evaluate it,
for example if i write f[5] and hit the enter key.

The (my) problem is that often when i plug such a function in other
routines, like FindMinimum, i get all sorts of errors and it doesn't
work.
Is there a way to tell Mathematica to just compute f[x] and then use
that result for other things?

From: Sjoerd C. de Vries on
The problem is that many of Mathematica's functions first try to find
a derivative of your function symbolically. So all you need to do is
define the arguments of your functions to be numeric to prevent
Mathematica from doing this.

Cheers -- Sjoerd

On Mar 27, 12:10 pm, ValeX <rjov...(a)gmail.com> wrote:
> let's say that i have a numerical function f[x] which is defined in a
> quite messy way.
> For example, it could be defined as a Module that contains If, Whiles,
> interpolation functions, Import, ..
> This function works well without any error if I simply evaluate it,
> for example if i write f[5] and hit the enter key.
>
> The (my) problem is that often when i plug such a function in other
> routines, like FindMinimum, i get all sorts of errors and it doesn't
> work.
> Is there a way to tell Mathematica to just compute f[x] and then use
> that result for other things?


From: Bill Rowe on
On 3/27/10 at 5:10 AM, rjovale(a)gmail.com (ValeX) wrote:

>let's say that i have a numerical function f[x] which is defined in
>a quite messy way. For example, it could be defined as a Module that
>contains If, Whiles, interpolation functions, Import, .. This
>function works well without any error if I simply evaluate it, for
>example if i write f[5] and hit the enter key.

>The (my) problem is that often when i plug such a function in other
>routines, like FindMinimum, i get all sorts of errors and it doesn't
>work. Is there a way to tell Mathematica to just compute f[x] and
>then use that result for other things?

No, this simply cannot be done with an arbitrary f[x].
Mathematica does both numerical computations and symbolic
computations. Some built-in functions will handle one but not
the other. You simply cannot feed say NIntegrate a symbolic
result and expect an answer.

But this doesn't mean a specific problem cannot be dealt with.
For example, FindMinimum has more than one way to search for the
minimum. Rather than doing

FindMinimum[f[x], x]

I could do

FindMinimum[f[x], {x, x1, x2}]

Using this last method, FindMinimum avoids taking derivatives.
That might just be the difference between finding the minimum of
a "messy" f[x] or not.


From: ValeX on
As suggested by Murray (the topic got split in two), i changed the
definition to

f[x_?NumericQ]:= ....

and now it works!