Main Content

Evaluate Symbolic Expressions Using subs

When you assign a value to a symbolic variable, expressions containing the variable are not automatically evaluated. Instead, evaluate expressions by using subs.

Define the expression y = x^2.

syms x
y = x^2;

Assign 2 to x. The value of y is still x^2 instead of 4.

x = 2;
y
y =
x^2

If you change the value of x again, the value of y stays x^2. Instead, evaluate y with the new value of x by using subs.

subs(y)
ans =
4

The evaluated result is 4. However, y has not changed. Change the value of y by assigning the result to y.

y = subs(y)
y =
4

Show that y is independent of x after this assignment.

x = 5;
subs(y)
ans =
4