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!


closed as off-topic by xzczd, happy fish, gwr, MarcoB, m_goldberg Mar 22 '17 at 18:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – xzczd, happy fish, gwr, MarcoB, m_goldberg
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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 '17 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 '17 at 3:40