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.

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 use two ways to create symbolic variables, `syms`

and `sym`

.
The `syms`

syntax is a shorthand for `sym`

.

Create 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 value `y`

.
Therefore, the commands are equivalent.

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 many variables, the `syms`

syntax
is inconvenient. Instead of using `syms`

, use `sym`

to
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]

The `syms`

command is a convenient shorthand
for the `sym`

syntax. Use the `sym`

syntax
when you create many variables, when the variable value differs from
the variable name, or when you create a symbolic number, such as `sym(5)`

.

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;

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.
However, `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.

Was this topic helpful?