Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

`MAXLEVEL`

Prevent infinite recursion during evaluation

**MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.**

**MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.**

The environment variable `MAXLEVEL`

determines
the maximal substitution depth of identifiers. When this substitution
depth is reached, an error occurs.

Possible values: integer greater 2; the maximum value depends on the operationg system, see below.

When a MuPAD^{®} object is evaluated, identifiers occurring
in it are replaced by their values. This happens recursively, i.e.,
if the values themselves contain identifiers, then these are replaced
as well. `MAXLEVEL`

determines the maximal recursion
depth of this process. If the substitution depth `MAXLEVEL`

is
reached, then an error occurs.

The purpose of `MAXLEVEL`

is to provide a heuristic
for recognizing infinite recursion with respect to the replacement
of identifiers by their values, like in ```
delete a: a := a
+ 1; a
```

. If, in this example, the substitution depth would
not be limited, then `a + 1`

would be substituted
for `a`

infinitely often, and the system would “hang”.

Similarly, the environment variable `MAXDEPTH`

provides a
heuristic for recognizing infinite recursion with respect to function
calls; see the corresponding help page for details.

There is a close connection between `LEVEL`

and `MAXLEVEL`

.
If the substitution depth `LEVEL`

is reached during the evaluation
process, then the recursion stops and any remaining identifiers remain unevaluated, but no error
occurs.

Thus, if `MAXLEVEL > LEVEL`

, then `MAXLEVEL`

has
no effect. By default, `LEVEL`

and `MAXLEVEL`

have
the same value `100`

at interactive level. However,
the default value of `LEVEL`

within a procedure is `1`

,
and thus usually `MAXLEVEL`

has no effect within
procedures.

There are some notable differences between `LEVEL`

and `MAXLEVEL`

.
The value of `LEVEL`

depends
on the context, namely whether the evaluation happens at interactive
level or in a procedure. Moreover, some system functions, such as `context`

and `level`

,
do not respect the current value of `LEVEL`

. In contrast, `MAXLEVEL`

is
a global bound. It works as a last resort when the control of the
evaluation via `LEVEL`

fails.

The default value of `MAXLEVEL`

is `100`

; `MAXLEVEL`

has
this value after starting or resetting the system via `reset`

. Also the command ```
delete
MAXLEVEL
```

restores the default value.

`MAXLEVEL`

is a global variable. Use the statement `save MAXLEVEL`

in
a procedure to confine any changes to `MAXLEVEL`

to
this procedure.

The maximum value of `MAXLEVEL`

depends on
the operating system. Under Windows^{®} it is 2^{13} =
8192. Under UNIX^{®} operating systems the maximum
value depends on the maximum size of the C-stack. With a default stack
size of `8 MB`

the value is 8192,
too; with a bigger stack size it can be bigger (in a bash the stack
size can be set with `ulimit -s`

).

Evaluation of objects defined by an infinite recursion produces an error:

delete a: a := a + 1: a

Error: Recursive definition: Reached maximal evaluation level.

This also works for mutually recursive definitions:

delete a, b: a := b^2: b := a + 1: b

Error: Recursive definition: Reached maximal evaluation level.

If `MAXLEVEL`

is smaller or equal to `LEVEL`

,
as is the default at interactive level, then objects are evaluated
completely up to depth `MAXLEVEL-1`

, and an error
occurs if the substitution depth `MAXLEVEL`

is reached,
whether a recursive definition is involved or not:

delete a, b, c, d: a := b: b := c: c := 7: d := d + 1: MAXLEVEL := 2: LEVEL := 2: c

a

Error: Recursive definition: Reached maximal evaluation level.

d

Error: Recursive definition: Reached maximal evaluation level.

On the other hand, `MAXLEVEL`

has no effect
if it exceeds `LEVEL`

.
Then any object is evaluated up to depth at most `LEVEL`

, and the “recursive
definition” error does not occur:

MAXLEVEL := 3: a, b, c, d

In particular, `MAXLEVEL`

normally has no effect
within procedures, where by default `LEVEL`

has the value 1:

MAXLEVEL := 2: p := proc() begin a, d end_proc: p(); delete MAXLEVEL, LEVEL:

Was this topic helpful?