Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was hoping to be able to use MMA's minimization power to work with an external process. I want to minimize the function 'TryThisNumber[x_]' which sends the value of x to something external and gets back a result from that external object. I want to find the number which minimizes the returned result. But ... I can only talk to the external process using strings. So, assume I have a function SendNumberToProcess[m_String] which does the actual communication with the process, I try:

TryThisNumber[x_]:= ToExpression[SendNumberToProcess[ToString[x]]];

and then,


The problem is that ToString[x] in the above is just evaluating to the symbol, a, not a numerical value, and thus I'm not actually able to send a number to the external process. Any ideas?

Update. Presumably the problem is that FindMinimum has the HoldAll attribute, but this attempt doesn't work either:

TryThisNumber[x_]:= ToExpression[SendNumberToProcess[ToString[Evaluate[x]]]];
share|improve this question
Have you tried TryThisNumber[x_?NumericQ] := (* stuff *)? – J. M. Sep 21 '12 at 16:26
Thanks, tried that but makes no difference. It's presumably because FindMinimum etc have the attribute HoldAll. I thought maybe this would work, but it doesn't: TryThisNumber[x_]:= ToExpression[SendNumberToProcess[ToString[Evaluate[x]]]]; – Cuboid Sep 21 '12 at 16:33
It seems you forgot the NumericQ[] in your new definition... – J. M. Sep 21 '12 at 16:37
Dont forget when you define the function with the NumericQ pattern, the old generic argument definition is still there. Be sure to clear it.. – george2079 Sep 21 '12 at 16:44
"I also wanted to ask what exactly is going on with the ?NumericQ construct, or rather, why specifically does it solve this problem." - See this. In short, the routines will first attempt a symbolic analysis of your objective function. If the function you want to optimize cannot be subjected to such analysis, then you need the _?NumericQ in the definition. – J. M. Sep 22 '12 at 8:41

Thanks to J.M. and george2079, this works:

TryThisNumber[x_?NumericQ] := ToExpression[SendNumberToProcess[ToString[x]]];
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.