This page shows how to create symbolic numbers, variables, and expressions. To learn how to work with symbolic math, see Perform Symbolic Computations.
You can create symbolic numbers by using
sym. Symbolic numbers are exact representations,
unlike floating-point numbers.
Create a symbolic number by using
compare it to the same floating-point number.
ans = 1/3 ans = 0.3333
The symbolic number is represented in exact rational form, while the floating-point number is a decimal approximation. The symbolic result is not indented, while the standard MATLAB® result is indented.
Calculations on symbolic numbers are exact. Demonstrate this
exactness by finding
sin(pi) symbolically and numerically.
The symbolic result is exact, while the numeric result is an approximation.
ans = 0 ans = 1.2246e-16
To learn more about symbolic representation of numbers, see Numeric to Symbolic Conversion.
Create symbolic variables
syms x y = sym('y')
The first command creates a symbolic variable
the MATLAB workspace with the value
to the variable
x. The second command creates a
y with value
Therefore, the commands are equivalent.
syms, you can create multiple variables
in one command. Create the variables
syms a b c
If you want to create many variables, the
is inconvenient. Instead of using
create many numbered variables.
Create the variables
a1, ..., a20.
A = sym('a', [1 20])
A = [ a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,... a11, a12, a13, a14, a15, a16, a17, a18, a19, a20]
syms command is a convenient shorthand
sym syntax. Use the
when you create many variables, when the variable value differs from
the variable name, or when you create a symbolic number, such as
Suppose you want to use a symbolic variable to represent the golden ratio
phi = (1 + sqrt(sym(5)))/2;
achieves this goal. Now you can perform various mathematical
phi. For example,
f = phi^2 - phi - 1
f = (5^(1/2)/2 + 1/2)^2 - 5^(1/2)/2 - 3/2
Now suppose you want to study the quadratic function
c. First, create the symbolic variables
syms a b c x
Then, assign the expression to
f = a*x^2 + b*x + c;
To create a symbolic number, use the
Do not use the
syms function to create a symbolic
expression that is a constant. For example, to create the expression
whose value is
f = sym(5).
f = 5 does not define
a symbolic expression.
If you set a variable equal to a symbolic expression, and then
syms command to the variable, MATLAB software
removes the previously defined expression from the variable. For example,
syms a b f = a + b
f = a + b
If later you enter
syms f f
then MATLAB removes the value
a + b from
f = f
You can use the
syms command to clear variables
of definitions that you previously assigned to them in your MATLAB session.
syms does not clear the following assumptions
of the variables: complex, real, integer, and positive. These assumptions
are stored separately from the symbolic object. For more information,
see Delete Symbolic Objects and Their Assumptions.