Re: Mathematica in conjunction with outside program; NMinimize fails.

*To*: mathgroup at smc.vnet.net*Subject*: [mg98938] Re: Mathematica in conjunction with outside program; NMinimize fails.*From*: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>*Date*: Wed, 22 Apr 2009 05:11:59 -0400 (EDT)*References*: <gsivfv$9dj$1@smc.vnet.net>

Hi ANdreas, NMinimize as a first step tries to evaluate the function symbolicaly. You can see this by using a function like In[39]:= f[x_] := Module[{}, Print[x]; x] In[40]:= NMinimize[f[y^2], y] y^2 Out[40]= {0., {y -> 0.}} You can prevent this behaviour by explicitely defining the function as numeric: g[x_?NumberQ] := Module[{}, Print[x]; x] NMinimize[g[y^2], y] 0.0929857 0.0708309 0.133682 ... So, in your case you should write your function as: SingleModelRun[ parametervalue_?NumberQ ] := etc. Cheers -- Sjoerd On Apr 21, 1:11 am, Andreas Pape <ap... at binghamton.edu> wrote: > Hello mathgroup. I am using Mathematica in conjunction with an > outside program (Netlogo is the program.) In a nutshell, my error is = > the following: I can call the outside program with a numeric value > successfully, and Plot can call the outside program as one would > expect. However, NMinimize calls the outside program with the > variable name, not a value, so it fails. In short, I need to force > NMinimize to pass a numeric value to the objective function, not a > variable name. > > More details: > > I have the following functions defined in Mathematica: > > SingleModelRun[ parametervalue_ ] := ....(definition ir= relevant) > This function (successfully) runs the out= side program, setting a > parameter equal to "parametervalue" (which is some number when I call > the function). It returns a value from the model. > > Plot[ SingleModelRun[ x ], {x , 0, 1} ] > Does what you expect: runs SingleModelRun= for parameter values 0 > through 1, and plots the resulting return value each time. > > NMinimize[ - SingleModelRun[ myvalue ], {myvalue} ] > Does not work. Returns the following e= rror: > > NetLogo`Private`NetLogo::"compilerExcepti= on" : "Nothing named > MYVALUE has been defined" > > (Note: NetLogo is the name of the outside program.) > > Which means that, oddly, NMinimize passes the *variable name* > (myvalue) instead of the *variable value* to the function > SingleModelRun; otherwise, there is no way the outside program would > be aware of the variable name. > > Is there a way to force Mathematica to evaluate the funct= ion with a > numeric value, not the variable name? (I thought that's what "N" in = > "NMinimize" meant, but apparently not.) > > In case you were wondering, this fails in an identical wa= y with: > NMaximize, FindMaximum, FindMimimum, etc. > > Thank you. > > Sincerely, > Andreas Duus Pape