       Re: Help with NMinimize

• To: mathgroup at smc.vnet.net
• Subject: [mg82381] Re: Help with NMinimize
• From: Szabolcs Horvát <szhorvat at gmail.com>
• Date: Fri, 19 Oct 2007 04:52:28 -0400 (EDT)
• References: <ff1qmk\$9eq\$1@smc.vnet.net><ff4fse\$dre\$1@smc.vnet.net> <ff78qi\$o1t\$1@smc.vnet.net>

```Flavio wrote:
> Thank you for your answer, I'm learning Mathematica and I think I will
> never stop doing it...
>
> I realized just after sending the message to have written (to the
> group) f[x,y,...] instead of f[x_,y_,...] , I'm sorry. Can you please
> tell me where in the help I can find the difference between the
> function declaration
>
> f[x_,y_] :=
>
>
> f[{x_,y_}]:=
>
> What's wrong with the first kind of declaration, which is used many
> times in the examples?

There is nothing wrong with it.

Note that if you have a function definition

fun[x_] := (Print[x]; x^2)

, then fun[a] evaluates to a^2 :

In:= fun[a]
During evaluation of In:= a
Out= a^2

NMinimize evaluates the function passed to it with symbolic arguments
(and if possible, compiles it).  Therefore NMinimize[fun[x], x] will see
x^2, and not (Print[x]; x^2).

One solution is to define the function in such a way that it does not
evaluates only with numeric (not symbolic) arguments:

In:=
Clear[fun]
fun[x_?NumericQ]:=(Print[x];x^2)

In:= fun[a]
Out= fun[a]

In:= fun
During evaluation of In:= 1
Out= 1

Try NMinimize[fun[x], x] with this second version and you'll notice that
the Print[x] statement is evaluated for each number NMinimize
subsittues for x in fun[x].

I could have written simply

g[x1_?NumericQ, x2_?NumericQ, x3_?NumericQ,
x5_?NumericQ, x5_?NumericQ] := (...)

, but this is too tedious, so I used a more compact version, with a
named pattern (lst):

g[lst:{x1_, x2_, x3_, x4_, x5} /; And@@NumericQ/@lst] := (...)

The sole reason for putting {x1_, x2_, x3_, x4_, x5_} into a list was
that I wanted to avoid typing, and test that all of them are numerical
in one go.  Probably the following solution is a better alternative:

g[x1_, x2_, x3_, x4_, x5_] :=
(...) /; VectorQ[{x1, x2, x3, x4, x5}, NumericQ]

Take a look at the following tutorials to learn about patterns:

http://reference.wolfram.com/mathematica/tutorial/Introduction-Patterns.html
http://reference.wolfram.com/mathematica/tutorial/PatternsAndTransformationRules.html

http://reference.wolfram.com/mathematica/tutorial/PatternsOverview.html

In Mathematica, f[x_] := x^2 isn't really a function definition.  'f' is
not a function in the mathematical sense, and it is not a subroutine
either (a function in the C sense).  This expression is the definition
of a transformation rule, attached to the symbol 'f'.  Whenever
Mathematica sees the symbol 'f', it checks whether it matches the
pattern f[_].  If it does, it replaces f[(argument)] with (argument)^2.

Many definitions can be attached to the same symbol, e.g.

factorial = 1
factorial[n_Integer?Positive] := factorial[n-1]*n

(* this will be used if the previous two don't match: *)
factorial[_] = "error"

When several definitions are attached to the same symbol, Mathematica
always checks the "more specific" ones first, i.e. factorial before
factoial[n_Integer?Positive], and factorial[n_Integer?Positive] before
factorial[_].  "More specific" is not defined clearly, and sometimes
Mathematica may not be able to decide which pattern is more specific and
which is more general.  In this case they are tried in the order they
were defined.

Before you redefine a "function" with many attached rules, Clear[] it,
so old definitions are removed.

--
Szabolcs

```

• Prev by Date: Re: Integrate question
• Next by Date: A riddle: Functions that return unevaluated when they cannot solve
• Previous by thread: Re: Help with NMinimize
• Next by thread: Re: Help with NMinimize