Visualize Expression Trees

MuPAD® notebooks will be removed in a future release. 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.

You can create mathematical expressions using MuPAD® objects and operators. For example, the following equation presents a MuPAD expression:

x + y + 1 + 1/5 + sin(5) = z

Precedence levels determine the order in which MuPAD evaluates an expression. Operators in MuPAD expressions have precedences similar to the precedences of regular mathematical operators. For example, when you compute the expression a + b*c, you calculate b*c, and then add a.

To change the evaluation order, use parentheses:

1 + 2*3, (1 + 2)*3

A useful model for visualizing a MuPAD expression is the expression tree. It reflects the internal representation of an expression. The operators or their corresponding functions are the vertices, and the arguments are sub-trees. The lowest precedence operator is always at the root of an expression tree.

For example, you can represent the expression a + b * c + d * e *sin(f)^g using this expression tree.

The following expression tree represents the expression int(exp(x^4), x = 0..1).

MuPAD internally represents the difference a - b as a + b*(-1). Therefore, MuPAD represents the difference using this expression tree.

Similarly, a quotient a/b has the internal representation a * b^(-1).

To display expression trees in a MuPAD notebook, use the prog::exprtree function. It replaces operators with the names of the corresponding system functions:

prog::exprtree(a + b * c + d * e *sin(f)^g):
+-- a
+-- _mult
|   |
|   +-- b
|   |
|   `-- c
`-- _mult
    +-- d
    +-- e
    `-- _power
        +-- sin
        |   |
        |   `-- f
        `-- g