Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.2K 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 BY: Michael Rogers

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
Posted 1 year ago

The name of the global context is "Global`". Context names always end in a backtick. The * is a "wildcard" that matches any characters in a symbol name. It is similar to the Unix shell wildcard * — and wildcards on other systems, I'm sure, but I learned computers on a multi-user Unix system before PCs were available. It is explained in the details section near the top of the documentation for Remove. Thus "Global`*" matches all symbol names in the global context. And "Global`q*" would match only symbol names that begin with q.

I use Quit[], which quits and restarts the kernel, instead of Remove["Global`*"], with an empty kernel init.m. Quit[] is usually slower (a few seconds) than Remove[], I think. How slow seems to change from version to version. Restarting the kernel seems closest to what you get when you run a compiled program (just system definitions, no prior user-defined values). As an interpreted environment with persistent definitions from one shift-enter to another (and a shared kernel for all notebooks), it's never exactly the same, though.

There is also the CleanSlate package, which some people use to do a bit more cleanup than Remove["Global`*"].

So much for dealing with clean runs of programs. As for the complexity of the programming environment, as I said in my first post, I don't have a quick answer.

POSTED BY: Updating Name
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

Dear Updating Name,

Thank you very much for your kind advice, including your valuable experience. I am probably much older than you and have no experience (other than a little Mathematica) with symbolic computation, etc. I have a lot of experience with FORTRAN programming and a little with assembler.

These older programs are written to execute in the order of the code statements, and the code looks spaghetti-complex because of the "go to" statements or jump statements, but the order of the operations is easy to understand, and debugging can be successful if done carefully, even if it takes time.

On the other hand, Mathematica's language is convenient for simple examples, but nasty pitfalls appear when I try to do anything even remotely complex. For example, is there an example of a function that needs to perform a symbolic computation in NMaximize[ ]? If numerical computation is inevitably assumed, shouldn't the argument be a number? I am also still stumped by the difference between a list and a number: when I have multiple answers in a list to a function defined using Module[ ], NMaximize[ ] does not work without a message even when I use Part[ ] to extract one answer as a number.

Ever since I started using Mathematica, I have wondered about the initialization of Mathematica programs at the start of execution. I always put the line

Remove["Global`*"];

at the beginning of each program, which is a spell without understanding what ` and * mean.

In conclusion, Mathematica is very useful for computing simple problems such as those given as samples, but when users try to solve their own customized problems, they often end up in a swamp-like pit requiring unknown spells. Wolfram seems to have no intention at this time of removing such difficulties in its product specifications. I am like a pilot who is confused by the autopilot specifications of an airliner and crashes.

POSTED BY: Unknown Unknown

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

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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard