[Mathematica] : Mathlink/Evaluation of a function using NMinValue/NMin

In summary, the function will not return a numeric value for any input except for constant numeric inputs.
  • #1
Hepth
Gold Member
464
40
I have a function that is defined using a variety of things, some matrix algebra, some mathlink functions, etc. But they are all defined to evaluate when called with the parameters :

chi2[mb, vmc, v1, v2, v3, v4, 0.1066, 0.219]

gives me a number back. I can even do a table over some range of parameters and it always returns a value, no matter how fine my grid becomes.

But when I run something like :

Code:
NMinValue[chi2[mb, 0.9882, 0.4536, 0.1485, 0.2981, -0.142279, 0.1066, 0.219], {mb, 4.6 - 0.2, 4.6 + 0.2}, AccuracyGoal -> 5]

Mathematica gives me :
Code:
NMinValue::nnum: The function value 0.584316+<<49>>+<<1>> is not a number at {mb} = {4.66099}. >>

though

Code:
chi2[4.660987123189611`, 0.9882000000000001`, 0.4536`, \
0.14850000000000002`, 0.29810000000000003`, -0.14227941068376537`, \
0.1066`, 0.219`]

gives me a value, 120.067.

So I don't know why it is complaining about that point.

I believe it has to do with NMinValue trying to evaluate the expression before it runs its minimization, and due to Mathlink it can't return a value or functional form unless its passed the parameters first. Like how you can specifiy SymbolicPreprocessing in NIntegrate.


I tried Evaluate[] over the chi2, but that doesn't work.

I can't really give a sample code, its much too difficult. But does anyone know if you can force a function to be evaluated at each given point?

I can also plot the function over the range of mb, it is just a simple parabola with a minimum right where I expected it. Plot has no trouble evaluating it.

Does NMinimize use algebraic derivatives? Does it try to preprocess functions before passing it a test argument?

This is all in Mathematica 7 for the moment. I can try 8 and 9 later, but not for a bit.
 
Physics news on Phys.org
  • #2
Yes, Mathematica does all sorts of things in the evaluation process that can break things which expect constant numeric values and get symbols.

Try changing your definition to use m_?NumericQ for each of your parameters m and see if the problem goes away.

Doing that means your function is in some ways undefined for anything but constant numeric arguments.
 
  • #3
Yes, due to the mathlink to some external Fortran code it is undefined unless given numerical inputs. It'll just return Failed otherwise.

I already set a property of the mathlink function as numerical, but maybe I must do tht to each input as well. I'll check when I get back to my computer.
 

FAQ: [Mathematica] : Mathlink/Evaluation of a function using NMinValue/NMin

What is Mathlink?

Mathlink is a technology developed by Wolfram Research that allows for communication between Mathematica and other programming languages, such as C, Java, and Python.

What is the purpose of using Mathlink in Mathematica?

The purpose of using Mathlink in Mathematica is to extend the functionality of Mathematica by allowing it to communicate with external programs and perform tasks that may not be possible within Mathematica alone.

What is NMinValue in Mathematica?

NMinValue is a built-in function in Mathematica that is used to find the minimum value of a function subject to certain constraints. It is often used in optimization problems.

How do you use NMinValue in conjunction with Mathlink?

To use NMinValue with Mathlink, you can first create a link between Mathematica and your external program using Mathlink functions. Then, you can pass the necessary data and constraints to your external program and use NMinValue to evaluate the function and return the minimum value back to Mathematica.

What is the difference between using NMinValue with Mathlink and using it within Mathematica alone?

Using NMinValue with Mathlink allows for more flexibility in terms of the types of functions and constraints that can be evaluated. It also allows for faster computation by utilizing the capabilities of the external program. However, using NMinValue within Mathematica alone may be more convenient for simple or quick calculations.

Similar threads

Replies
6
Views
3K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
2
Views
3K
Replies
4
Views
9K
Replies
4
Views
3K
Replies
4
Views
7K
Back
Top