I define a function called f[x], which gives a numerical result interacting with an external free software called FEMM, using NETLink.

This function works properly and the following codes give the expected result:


I try to do some optimization using this function. In order to do that I use


But an error appears as below:

"error: attempt to perform arithmetic on global `x' (a nil value)".

What could be the cause of this problem?

Thanks for your help!

  • 1
    $\begingroup$ I'm not sure without the full definition of f, but try defining ff[x_?NumericQ]:=f[x] and optimizing ff instead. $\endgroup$
    – xzczd
    Mar 21, 2017 at 6:56
  • $\begingroup$ Thanks for your help! When added this one, it works and gets the right result. Thanks a lot. I also feel much sorry for express my gratitude late. $\endgroup$
    – Wang Q
    Mar 28, 2017 at 3:40


Browse other questions tagged or ask your own question.