Re: Help with NMinimize

*To*: mathgroup at smc.vnet.net*Subject*: [mg82432] Re: Help with NMinimize*From*: Flavio <flavio.cimolin at gmail.com>*Date*: Sat, 20 Oct 2007 05:49:43 -0400 (EDT)*References*: <ff1qmk$9eq$1@smc.vnet.net><ff78qi$o1t$1@smc.vnet.net>

Thank you very much for your detailed explanation, now it's perfectly clear to me which was the problem with my code. Just one last "philosophical" question: I know that Mathematica deals with simbolic calculation, but I though that at least the Nxxx commands (NSolve, NDSolve, NMinimize, ...) would treat the functions just numerically. So must I keep in mind that these commands have to be considered "simbolic" too? Regards, Flavio On 19 Ott, 11:01, Szabolcs Horv=E1t <szhor... at gmail.com> wrote: > 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_] := > > > and your kind > > > 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[2]:= fun[a] > During evaluation of In[2]:= a > Out[2]= 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[3]:= > Clear[fun] > fun[x_?NumericQ]:=(Print[x];x^2) > > In[5]:= fun[a] > Out[5]= fun[a] > > In[6]:= fun[1] > During evaluation of In[6]:= 1 > Out[6]= 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-Patter...h= ttp://reference.wolfram.com/mathematica/tutorial/PatternsAndTransfor... > > 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[0] = 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[0] 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

**Re: Ver 6.0 Subsets and Packed Array Question**

**Re: Re: Integrate question**

**Re: Help with NMinimize**

**Re: Help with NMinimize**