# Using FindMinimum of a function imported via DefineDLLFunction

Mathematica newbie here, just used it today for the first time.

So I've successfully "imported" a function from a dll I've built (in C++ with VS), using .NET/LINK and DefineDLLFunction and I've assigned it the name MyFunction. The function evaluates just fine when I call it directly (i.e. passing a double as an argument). But when I try to find a local minimum using say

FindMinimum[{MyFunction[x], 1 < x < 2}, {x, 1.5}]


I get

NET::methodargs : Improper arguments supplied for method named...


Am I trying something that cannot be done, or am I doing it wrong? Can functions imported like that be used by FindMinimum?

• Does it work if you put FindMinimum[{f[x], 1 < x < 2}, {x, 1.5}] where f[x_?NumericQ]:=MyFunction[x]? – Kuba Feb 27 '17 at 21:46
• Wow, you do know your stuff! Thank you so much for this and for taking the time to make my post more readable. Could you please explain what exactly the "?NumericQ"? specifier does? I had already done what you said (my function actually takes 16 arguments and I only want to change one, so I had mapped it to f[x_] := MyFunction[x, c1, c2, c3...], where c1, c2, c3 are the constant values for the other arguments), but without the "?NumericQ" magic part. – Yian Pap Feb 27 '17 at 22:11
• Take a look at this answer, it should explain it: 26037 – Kuba Feb 27 '17 at 22:15
• Again, thanks for the link with the pitfalls for new users, I'll be having a look at it I guess. But this was definitely an "obscure" solution for a newbie, so I don't feel that bad for not finding it myself with my 2-hour of googling (should I?!) – Yian Pap Feb 27 '17 at 22:31
• Another related question (please let me know if it's worth stating it as a separate question): So this worked and it takes about 5 secs for FindMinimum to find the minimum. An individual function evaluation takes about 70ms. But when I set EvaluationMonitor, it tells me there was only one evaluation. What can be the issue here? Obviously the function was evaluated many times. – Yian Pap Feb 28 '17 at 0:46