Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.5K Views
|
8 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Use NMinimize calling an own defined function?

Posted 7 years ago

I want to call an own defined function through NMinimize. For the sake of simplicity, let us define the problem as follow:

radpatt[x1_] := (Print[x1]; x1 )

NMinimize[{radpatt[xx], 0.1 <= xx <= 1.1}, {xx}]

If you run the above-listed instructions, you will notice that x1 is not number as soon as the function radpatt is called by NMinimize, but it is equal to xx. This causes me an issue because in my original problem x1 needs to be a number just at the beginning of my own function. Any ideas?

Many many thanks in advance.

8 Replies
Posted 1 year ago
POSTED BY: Updating Name

Thanks a lot! (But I am sorry to say I cannot understand what "context" really means, and I suppose I shall sometimes crash even after understanding it. Sweat ^^;)

POSTED BY: Unknown Unknown
POSTED BY: Michael Rogers

For protecting an objective which is a function of a list of variables use:

obj[vars_?(AllTrue[#, NumberQ] &)]

as suggested to me by Wolfram support

POSTED BY: Frank Kampas

A user-supplied objective function for NMinimize or FindMinimum needs to be "protected" from premature evaluation when given symbolic input. Therefore, use a definition like this

radpatt[x1_?NumberQ] := (Print[x1]; x1 )
POSTED BY: Robert Nachbar

I had the same problem as the original question posted above, so thank you for your answer. However, I would appreciate it if you could answer my question as to why Mathematica does not provide an answer to such a frequently encountered problem. I also wonder if there are too many random, no-regularity, cop-out patch options like "?NumberQ". Since the contents of Mathametica are a black box to users, I would like to know about the critical language design policies and syntax of this useful software.

POSTED BY: Unknown Unknown
Posted 1 year ago

Dear Shinji,

There is an example in the current documentation for NMinimize[] (here) that shows the use f2[a_?NumericQ] := .... Note NumberQ[Sqrt[2]] is False but NumericQ[Sqrt[2]] is True. Therefore NumberQ[] is a more restrictive test; per the docs, "NumberQ[expr] returns False unless expr is manifestly a number (i.e. has head Complex, Integer, Rational, or Real)." The expression Sqrt[2] has head Power (another somewhat frequently encountered problem). NumberQ[] may be slightly faster than NumericQ[], but RepeatedTiming[] shows no discernible difference in my tests.

This example had been part of the "knowledge base" tech notes, but I believe it was incorporated into the documentation some years ago. The knowledge base note is still available here: https://support.wolfram.com/12502.

The issue is also discussed in this StackExchange answer. Other answers in that Q&A discuss things someone thinks are helpful for Mathematica users to know, some of which you may find helpful.

As for "the critical language design policies" in general, I'm not sure I can really help. I think I "grok" them. When I started, I read The Mathematica Book by Stephen Wolfram back around 1990. It is (or was for the early versions) a good, intermediate-level guide, with a good introductory Part 1. I came away with a pretty good understanding of "everything-is-an-expression"; that "evaluation" meant rewriting expressions according to pattern-rule operations, and it was not strictly limited to number and string crunching as in C but was more like Lisp, two languages I knew; and that there was "standard evaluation" and "non-standard evaluation." Coming to grips with standard and non-standard evaluation, the significance of Hold* attributes, Hold[], Unevaluated[], Defer[], and up-/down-/sub-values associated to symbols took a bit longer. And now we have Inactive[]. It's complicated. That means it is easy to become confused. Some of it I gave up on and didn't understand for maybe fifteen years or more. Most work does not require an understanding of all the arcane aspects. Much of The Mathematica Book is available in the documentation, tutorial/VirtualBookOverview. It has necessarily been restructured and updated. "The Evaluation of Expressions" may be found under "Core Language."

I mention several aspects of evaluation, because that is at the heart of the problem in the OP, and an appropriate pattern (_?NumericQ) is needed to control the order of evaluation.

I might also mention that at the time I started learning Mathematica, I knew Postscript, which greatly helped with understanding graphics concepts in early Mathematica; and Macaulay and Maple, which greatly helped with understanding symbolic algebra concepts and symbolic computation in general.

POSTED BY: Updating Name
POSTED BY: Unknown Unknown
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard