# Use NMinimize calling an own defined function?

**URL:** <https://community.wolfram.com/t/use-nminimize-calling-an-own-defined-function/13415>\
**Category:** Questions\
**Tags:** optimization, numerical-computation\
**Created:** [December 8, 2018, 6:23am UTC](https://community.wolfram.com/t/use-nminimize-calling-an-own-defined-function/13415 "2018-12-08T06:23:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mencagli](https://avatars.discourse-cdn.com/v4/letter/m/7993a0/32.png) [@mencagli](https://community.wolfram.com/u/mencagli)\
**Post date:** [December 8, 2018, 6:23am UTC](https://community.wolfram.com/t/use-nminimize-calling-an-own-defined-function/13415/1 "2018-12-08T06:23:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rnachbar](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/rnachbar/32/10766_2.png) [@rnachbar](https://community.wolfram.com/u/rnachbar)\
**Post date:** [December 8, 2018, 2:05pm UTC](https://community.wolfram.com/t/use-nminimize-calling-an-own-defined-function/13415/2 "2018-12-08T14:05:51Z")

</div>

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 )

```

---

<div class="post-metadata">

**Author:** ![gc46579](https://avatars.discourse-cdn.com/v4/letter/g/d9b06d/32.png) [@gc46579](https://community.wolfram.com/u/gc46579)\
**Post date:** [July 3, 2024, 7:24am UTC](https://community.wolfram.com/t/use-nminimize-calling-an-own-defined-function/13415/3 "2024-07-03T07:24:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fkampas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/fkampas/32/41217_2.png) [@fkampas](https://community.wolfram.com/u/fkampas)\
**Post date:** [July 3, 2024, 1:45pm UTC](https://community.wolfram.com/t/use-nminimize-calling-an-own-defined-function/13415/4 "2024-07-03T13:45:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon21378091](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@anon21378091](https://community.wolfram.com/u/anon21378091)\
**Post date:** [July 3, 2024, 4:24pm UTC](https://community.wolfram.com/t/use-nminimize-calling-an-own-defined-function/13415/5 "2024-07-03T16:24:32Z")

</div>

Dear Shinji,

There is an example in the current documentation for `NMinimize[]` ([here](https://reference.wolfram.com/language/ref/NMinimize.html#1878322261)) 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: [Wolfram Support Quick Answers: How do I use ?NumericQ to affect order of evaluation?](https://support.wolfram.com/12502).

The issue is also discussed in [this StackExchange answer](https://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users/26037#26037). 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](https://reference.wolfram.com/language/tutorial/VirtualBookOverview.html). 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.

---

<div class="post-metadata">

**Author:** ![mroge02](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mroge02/32/28775_2.png) [@mroge02](https://community.wolfram.com/u/mroge02)\
**Post date:** [July 4, 2024, 7:45pm UTC](https://community.wolfram.com/t/use-nminimize-calling-an-own-defined-function/13415/6 "2024-07-04T19:45:26Z")

</div>

Hi,

Numerical solvers in Mathematica usually analyze the symbolic form of the objective function. To do this analysis, `NMinimize[]` evaluates `radpatt[xx]` (in the OP at the top). This analysis may be used for method selection, singularity handling, and perhaps other adjustments. Mathematica is designed to do this automatically and to allow for users to override its automatic choices. It uses heuristics that are not always perfect. Symbolic processing takes time. So user-overrides sometimes are needed and can speed things up when the user knows what they are doing.

Using `_?NumericQ` in defining the objective function prevents symbolic analysis, and the automatic choice of method is more likely to be less than optimal. Symbolic analysis mainly done on formulas, as far as I know. So if your objective functions calls a numerical routine, such as `NIntegrate[]` or `FindRoot[]`, then you do not lose much by using `_?NumericQ`. And if you need to specify singularities or method, you can do that through options. (One irritating-to-me exception: `Plot[f[x], {x, 0, 1}, WorkingPrecision -> 32]` evaluates `f[x]` on a machine-precision number at the beginning, and sometimes this causes problems.)

Below are variations on an example. The method, speed, and quality of the result are altered by using `_?NumericQ` in `obj[]`. Also the last variant shows that the superior method may not be used on `obj[]`.

```
(* Uses Couenne NLP *)
NMinimize[{Cos[x] - Exp[(x - 0.5) y], x^2 + y^2 < 1}, {x, y}] // AbsoluteTiming
(* {0.03809, {-1.60047, {x -> -0.657103, y -> -0.753805}}} *)

(* Ends up using diff. evol. after rejecting Couenne *)
obj // ClearAll; (* I always start function definitions this way (or ClearAll[obj] *)
obj[x_?NumericQ, y_?NumericQ] := Cos[x] - Exp[(x - 0.5) y];
NMinimize[{obj[x, y], x^2 + y^2 < 1}, {x, y}] // AbsoluteTiming
(* {0.154981, {-1.60046, {x -> -0.657221, y -> -0.753698}}} *)

NMinimize[{obj[x, y], x^2 + y^2 < 1}, {x, y}, Method -> "Couenne"]
(* NMinimize::unproc: Could not process the objective and constraints in a form suitable for Couenne. *)
(* NMinimize[{obj[x, y], x^2 + y^2 < 1}, {x, y}, Method -> "Couenne"] *)

```

* * *

If you want to know how I know what is going, here are some undocumented ways to peak under the hood of `NMinimize`/`NMaximize`:

```
Optimization`Debug`SetPrintLevel[2]; 
Block[{Optimization`NMinimizeDump`$DiagnosticLevel = 2},
 (* Uses Couenne NLP *)
 NMinimize[{Cos[x] - Exp[(x - 0.5) y], x^2 + y^2 < 1}, {x, y}]
 ]
Optimization`Debug`SetPrintLevel[0];

Optimization`Debug`SetPrintLevel[2];
Block[{Optimization`NMinimizeDump`$DiagnosticLevel = 2},
 (* Ends up using diff. evol. after rejecting Couenne *)
 NMinimize[{obj[x, y], x^2 + y^2 < 1}, {x, y}]
 ]
Optimization`Debug`SetPrintLevel[0];

```

---

<div class="post-metadata">

**Author:** ![gc46579](https://avatars.discourse-cdn.com/v4/letter/g/d9b06d/32.png) [@gc46579](https://community.wolfram.com/u/gc46579)\
**Post date:** [July 4, 2024, 10:24pm UTC](https://community.wolfram.com/t/use-nminimize-calling-an-own-defined-function/13415/7 "2024-07-04T22:24:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon21378091](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@anon21378091](https://community.wolfram.com/u/anon21378091)\
**Post date:** [July 5, 2024, 7:20pm UTC](https://community.wolfram.com/t/use-nminimize-calling-an-own-defined-function/13415/8 "2024-07-05T19:20:53Z")

</div>

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](https://library.wolfram.com/infocenter/MathSource/4718/),  
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.

---

<div class="post-metadata">

**Author:** ![gc46579](https://avatars.discourse-cdn.com/v4/letter/g/d9b06d/32.png) [@gc46579](https://community.wolfram.com/u/gc46579)\
**Post date:** [July 9, 2024, 2:48am UTC](https://community.wolfram.com/t/use-nminimize-calling-an-own-defined-function/13415/9 "2024-07-09T02:48:52Z")

</div>

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 ^^😉
