Main Content

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 `sym`

and compare it to the
same floating-point number.

sym(1/3) 1/3

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.

sin(sym(pi)) sin(pi)

ans = 0 ans = 1.2246e-16

To learn more about symbolic representation of numbers, see Numeric to Symbolic Conversion.

You can create symbolic variables using either `syms`

or `sym`

. Typical uses of these functions include:

`sym`

– Create numbered symbolic variables or create symbolic variables in MATLAB functions.`syms`

– Create*fresh*symbolic variables for interactive symbolic workflows, that is, for symbolic variable creation at the MATLAB command line or in MATLAB live scripts. A*fresh*symbolic variable does not have any assumptions.

The `syms`

command is shorthand for the
`sym`

syntax, but the two functions handle assumptions
differently. For more details, see Reuse Names of Symbolic Objects.

Create the symbolic variables `x`

and `y`

using
`syms`

and `sym`

, respectively.

syms x y = sym('y')

The first command creates a symbolic variable `x`

in the
MATLAB workspace with the value `x`

assigned to the variable
`x`

. The second command creates a symbolic variable
`y`

with the value `y`

.

With `syms`

, you can create multiple variables in one command.
Create the variables `a`

, `b`

, and
`c`

.

syms a b c

If you want to create a MATLAB array of numbered symbolic variables, the `syms`

syntax is inconvenient. Therefore, use `sym`

instead to create an
array of many numbered symbolic variables.

Clear the workspace. Create a row vector containing the symbolic variables
`a1, ..., a20`

and assign it to the MATLAB variable `A`

. Display the variable in the MATLAB workspace.

clear all A = sym('a', [1 20]) whos

A = [ a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,... a11, a12, a13, a14, a15, a16, a17, a18, a19, a20] Name Size Bytes Class Attributes A 1x20 8 sym

`A`

is a `1`

-by-`20`

array of
20 symbolic variables.

By combining `sym`

and `syms`

, you can
create many fresh symbolic variables with corresponding variables name in the
MATLAB workspace.

Clear the workspace. Create the fresh symbolic variables ```
a1, ...,
a10
```

and assign them the MATLAB variable names `a1, ..., a10`

, respectively. Display
the variables in the MATLAB workspace.

clear all syms(sym('a', [1 10])) whos

Name Size Bytes Class Attributes a1 1x1 8 sym a10 1x1 8 sym a2 1x1 8 sym a3 1x1 8 sym a4 1x1 8 sym a5 1x1 8 sym a6 1x1 8 sym a7 1x1 8 sym a8 1x1 8 sym a9 1x1 8 sym

The MATLAB workspace contains 10 MATLAB variables that are symbolic variables.

The `syms`

command is a convenient shorthand for the
`sym`

syntax, and its typical use is to create fresh symbolic
variables for interactive symbolic workflows. Use the `sym`

syntax to create the following:

Symbolic variables in MATLAB functions

Many numbered symbolic variables

Symbolic variable whose value differs from its name in the MATLAB workspace

Symbolic number, such as

`sym(5)`

Symbolic variable that inherits the assumptions from a previously used symbolic variable having the same name

Suppose you want to use a symbolic variable to represent the golden ratio

$$\phi =\frac{1+\sqrt{5}}{2}$$

The command

phi = (1 + sqrt(sym(5)))/2;

achieves this goal. Now you can perform various mathematical operations on
`phi`

. For example,

f = phi^2 - phi - 1

returns

f = (5^(1/2)/2 + 1/2)^2 - 5^(1/2)/2 - 3/2

Now suppose you want to study the quadratic function `f`

=
`ax`

^{2} + `bx`

+
`c`

. First, create the symbolic variables `a`

,
`b`

, `c`

, and `x`

:

syms a b c x

Then, assign the expression to `f`

:

f = a*x^2 + b*x + c;

**Tip**

To create a symbolic number, use the `sym`

command. Do not
use the `syms`

function to create a symbolic expression that is
a constant. For example, to create the expression whose value is
`5`

, enter `f = sym(5)`

. The command
`f = 5`

does *not* define
`f`

as a symbolic expression.

If you set a variable equal to a symbolic expression, and then apply the
`syms`

command to the variable, MATLAB software removes the previously defined expression from the variable.
For example,

syms a b f = a + b

returns

f = a + b

If later you enter

syms f f

then MATLAB removes the value `a + b`

from the expression
`f`

:

f = f

You can use the `syms`

command to clear variables of definitions
that you previously assigned to them in your MATLAB session. `syms`

clears the assumptions of the
variables: complex, real, integer, and positive. These assumptions are stored
separately from the symbolic object. However, recreating a variable using
`sym`

does not clear its assumptions. For more information, see
Delete Symbolic Objects and Their Assumptions.