Visualize Expression Trees

    Note:   Use only in the MuPAD Notebook Interface. This functionality does not run in MATLAB.

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
Was this topic helpful?