Message Boards Message Boards

0
|
11236 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Mathematica evaluates numerical functions symbolically. How can I stop it?

Posted 10 years ago
The attached file demo.nb:

Defines two Bezier curves cur1 and cur2. It graphs them, and then tries to use NMaximize to find the distance between them.

As the results show

(1) Plot first evaluates the plotted function with the symbolic argument "t", even though the documentation of plot says " ...evaluates the fi and gi only after assigning specific numerical values to variables." Plot seems to be smart enough to produce a correct plot, even though the call with "t" returns Null.

(2) Then NMaximize also tries to evaluate the target function with the symbolic argument "t". However, NMaximize isn't smart enough to recover when the target function returns nonsense. (And I have other examples in which the symbolic call hopelessly messes up the state of the target function, so that _no_ future call with any kind of argument would return a meaningful result.)

This seems like a general behavior in Mathematica ... I've seen it in other places.

In the case of NMaximize, it seems particularly inappropriate. What does the "N" mean, if NMaximize tries to evaluate the target symbolically?

I'm sure that the Wolfram people have a reason for this. But it seems to me that (1) the documentation should describe this behavior, and (2) there should be an option to turn it off.

And, of course, Plot should work as documented.

My apolgies for not putting the code in here directly, and making you look at the attached file.  The machine I have Mathematica on is not connected to the net, so I have to carry home on a thumb drive anything I post here. So it's hard to post code.

Wolfram should be aware that it takes an hour to put togther this clean demo. And _days_ wasted because of this kind of non-intuitive and non-documented behavior of Mathematica.

Yours
David Golber
POSTED BY: David Golber
4 Replies
Posted 2 years ago
POSTED BY: John G
When you define a numerical function that can't be evaluated symbolically, you should define in the form  f[ x_?NumberQ] := ...
Many of Mathematica's numerical algorithms do some symbolic evaluation to improve their performance.  _?NumberQ prevents that from happening.

http://reference.wolfram.com/mathematica/ref/NumberQ.html
POSTED BY: Frank Kampas
I couldn't see any file attached, so I'll comment on NMaximize evaluation in general.

Where necessary, for many numerical functions in Mathematica, there is a statement in the documentation, for example "FindMinimum has attribute HoldAll ... FindMinimum first localizes the values of all variables, then evaluates f with the variables being symbolic, and then repeatedly evaluates the result numerically."

Such a statement is not needed for NMaximize, because it does not have any Hold* attributes. By the general evaluation principles of Mathematica, see the documentation, this means that any and all arguments will be evaluated before NMaximize even sees them.

As already pointed out in this thread, there is a simple way to define the objective function by restricting it so it evaluates only for numerical arguments, using the _?NumericQ pattern test on the variables. You may also find this Technical support article useful.

It might be a good idea to organize the computations so that your target function will never "return nonsense" or get in a hopelessly messed-up state so that "_no_ future call with any kind of argument would return a meaningful result". Doing that, however, doesn't seem to be a task for NMaximize.
POSTED BY: Ilian Gachevski
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract