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.

Evaluation is one of the most common mathematical operations.
Therefore, it is important to understand how and when Symbolic Math Toolbox™ performs
evaluations. For example, create a symbolic variable, `x`

,
and then assign the expression `x^2`

to another variable, `y`

.

syms x y = x^2;

Now, assign a numeric value to `x`

.

x = 2;

This second assignment does not change the value of `y`

,
which is still `x^2`

. If later you change the value
of `x`

to some other number, variable, expression,
or matrix, the toolbox remembers that the value of `y`

is
defined as `x^2`

. When displaying results, Symbolic Math Toolbox does
not automatically evaluate the value of `x^2`

according
to the new value of `x`

.

y

y = x^2

To enforce evaluation of `y`

according to the
new value of `x`

, use the `subs`

function.

subs(y)

ans = 4

The displayed value (assigned to `ans`

) is
now `4`

. However, the value of `y`

does
not change. To replace the value of `y`

, assign the
result returned by `subs`

to `y`

.

y = subs(y)

y = 4

After this assignment, `y`

is independent of `x`

.

x = 5; subs(y)

ans = 4

Create a symbolic function and assign an expression to it.

syms f(x) f(x) = x^2;

Now, assign a numeric value to `x`

.

x = 2;

The function itself does not change: the body of the function
is still the symbolic expression `x^2`

.

f

f(x) = x^2

In case of symbolic expressions, the recommended approach is
to use `subs`

to evaluate the expression with the
most recent values of its parameters. This approach is not recommended
for symbolic functions. For example, if you evaluate `f`

using
the `subs`

function, the result is the expected
value `4`

, but it is assigned to a symbolic function, `fnew`

.
This new symbolic function formally depends on the variable `x`

.

fnew = subs(f)

fnew(x) = 4

The function call, `f(x)`

, returns the value
of `f`

for the current value of `x`

.
For example, if you assigned the value `2`

to the
variable `x`

, then calling `f(x)`

is
equivalent to calling `f(2)`

.

f2 = f(x)

f2 = 4

f2 = f(2)

f2 = 4

`f`

remains independent of the value assigned
to `x`

.

f [f(1),f(2),f(3)]

f(x) = x^2 ans = [ 1, 4, 9]

Was this topic helpful?