# Create Symbolic Numbers, Variables, and Expressions

This example shows how to create symbolic numbers, variables, and expressions. To learn how to work with symbolic math, see Perform Symbolic Computations.

### Create Symbolic Numbers with Exact Representations

You can create symbolic numbers by using `sym`

. Symbolic numbers are exact representations, unlike floating-point numbers.

Create symbolic numbers by using `sym`

and compare them to the same floating-point numbers.

a1Sym = sym(1/3)

a1Sym =$$\frac{1}{3}$$

a1 = 1/3

a1 = 0.3333

a2Sym = sym(pi)

`a2Sym = $$\pi $$`

a2 = pi

a2 = 3.1416

The symbolic numbers are represented in exact rational form, while the floating-point numbers are decimal approximations.

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.

bSym = sin(sym(pi))

`bSym = $$0$$`

b = sin(pi)

b = 1.2246e-16

When you use `sym`

on a numeric input, the numeric expression is first evaluated to the MATLAB® default double-precision number that can be less accurate. Then, `sym`

is applied on that double-precision number. To represent an exact number without evaluating it to double precision, use a character vector with quotes. For example, create a symbolic number to represent a very large integer exactly.

inaccurateNum = sym(123456789012345678)

`inaccurateNum = $$123456789012345680$$`

`accurateNum = sym('123456789012345678')`

`accurateNum = $$123456789012345678$$`

You can also create symbolic complex numbers, by specifying the imaginary part of a number as `1i`

, `2i`

, and so on.

`sym('1234567 + 1i')`

`ans = $$1234567+\mathrm{i}$$`

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

### Create Symbolic Numbers with Variable Precision

You can create symbolic numbers with variable-precision floating-point arithmetic by using `vpa`

. By default, `vpa`

calculates values to 32 significant digits.

piVpa = vpa(pi)

`piVpa = $$3.1415926535897932384626433832795$$`

When you use `vpa`

on a numeric expression, such as `log(2)`

, the expression is first evaluated to the MATLAB default double-precision number that has less than 32 significant digits. Then, `vpa`

is applied on that double-precision number, which can be less accurate. For more accurate results, convert double-precision numbers in an expression to symbolic numbers with `sym`

and then use `vpa`

to evaluate the results with variable precision. For example, find `log(2)`

with 17- and 20- digit precision.

vpaOnDouble = vpa(log(2))

`vpaOnDouble = $$0.69314718055994528622676398299518$$`

vpaOnSym_17 = vpa(log(sym(2)),17)

`vpaOnSym_17 = $$0.69314718055994531$$`

vpaOnSym_20 = vpa(log(sym(2)),20)

`vpaOnSym_20 = $$0.69314718055994530942$$`

When you convert large numbers, use quotes to represent them exactly.

inaccurateNum = vpa(123456789012345678)

`inaccurateNum = $$123456789012345680.0$$`

`accurateNum = vpa('123456789012345678')`

`accurateNum = $$123456789012345678.0$$`

### Create Symbolic Variables

You can create symbolic variables using either `syms`

or `sym`

. Typical uses of these functions include:

`sym`

– Create numbered symbolic variables, symbolic variables in MATLAB functions, or symbolic numbers whose values differ from their names in the MATLAB workspace.`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. `syms`

clears the assumptions when creating variables. However, recreating a variable using `sym`

does not clear its assumptions. For more details about the differences of these two functions, see Choose syms or sym Function.

Create the symbolic variables x and y using `syms`

and `sym`

, respectively.

syms x y = sym('y')

`y = $$y$$`

The first command creates a symbolic variable `x`

in the MATLAB workspace with the value $\mathit{x}$ assigned to the variable `x`

. The second command creates a symbolic variable `y`

with the value $\mathit{y}$.

With `syms`

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

, `b`

, and `c`

.

syms a b c

#### Create Array of Symbolic Variables

If you want to create a MATLAB array of numbered symbolic variables, you can use the `sym`

or the `syms`

syntax.

Use `sym`

to create an array of many numbered symbolic variables. Clear the workspace. Create a row vector containing the symbolic variables ${\mathit{a}}_{1},\dots ,{\mathit{a}}_{10}$ and assign it to the MATLAB variable `A`

. Display the variable in the MATLAB workspace.

```
clear
A = sym('a',[1 10])
```

`A = $$\left(\begin{array}{cccccccccc}{a}_{1}& {a}_{2}& {a}_{3}& {a}_{4}& {a}_{5}& {a}_{6}& {a}_{7}& {a}_{8}& {a}_{9}& {a}_{10}\end{array}\right)$$`

whos

Name Size Bytes Class Attributes A 1x10 8 sym

`A`

is a 1-by-10 array of 10 automatically generated elements. These generated elements of `A`

do not appear in the MATLAB workspace.

Use `syms`

to create many fresh symbolic variables with corresponding variable names in the MATLAB workspace. Clear the workspace. Create the fresh symbolic variables `a1, ..., a10`

. Display the variables in the MATLAB workspace.

clear syms a [1 10] whos

Name Size Bytes Class Attributes a 1x10 8 sym 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 in the workspace 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(3)/11`

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

### Create Symbolic Expressions

Suppose you want to use a symbolic variable to represent the golden ratio $\phi =\frac{\text{\hspace{0.17em}}1+\sqrt{\text{\hspace{0.17em}}5}}{2}$.

Use `sym`

to create the golden ratio.

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

Now you can perform various mathematical operations on `phi`

. For example:

f = phi^2 - phi - 1

f =$${\left(\frac{\sqrt{5}}{2}+\frac{1}{2}\right)}^{2}-\frac{\sqrt{5}}{2}-\frac{3}{2}$$

Next, suppose you want to study the quadratic function $\mathit{f}=\mathit{a}{\mathit{x}}^{2}+\mathit{bx}+\mathit{c}$. First, create the symbolic variables `a`

, `b`

, `c`

, and `x`

.

syms a b c x

Then, create a symbolic expression `f`

that represents the arithmetical expression $\mathit{a}{\mathit{x}}^{2}+\mathit{bx}+\mathit{c}$.

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

`f = $$a\hspace{0.17em}{x}^{2}+b\hspace{0.17em}x+c$$`

Solve the quadratic equation $\mathit{f}=0$ for $\mathit{x}$ by using `solve`

.

x_0 = solve(f == 0,x)

x_0 =$$\left(\begin{array}{c}-\frac{b+\sqrt{{b}^{2}-4\hspace{0.17em}a\hspace{0.17em}c}}{2\hspace{0.17em}a}\\ -\frac{b-\sqrt{{b}^{2}-4\hspace{0.17em}a\hspace{0.17em}c}}{2\hspace{0.17em}a}\end{array}\right)$$

You can also apply a mathematical function to an arithmetical expression. For example, apply the Bessel function of the first kind ${\mathit{J}}_{0}$ to the arithmetical expression $$f$$ and find its derivative with respect to $$x$$.

J_0 = besselj(0,f)

`J_0 = $${\mathrm{J}\text{besselj}}_{0}\left(a\hspace{0.17em}{x}^{2}+b\hspace{0.17em}x+c\right)$$`

DJ_0 = diff(J_0,x)

`DJ_0 = $$-{\mathrm{J}\text{besselj}}_{1}\left(a\hspace{0.17em}{x}^{2}+b\hspace{0.17em}x+c\right)\hspace{0.17em}\left(b+2\hspace{0.17em}a\hspace{0.17em}x\right)$$`

To create a symbolic number in a symbolic expression, use `sym`

. Do not use `syms`

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

, enter `f = sym(5)`

. The command `f = 5`

does not define `f`

as a symbolic expression.

You can also create symbolic expressions from strings by using `str2sym`

when reading expressions from text files or when specifying numbers exactly.

### Reuse Names of Symbolic Objects

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

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

For example, create a symbolic expression `f`

.

syms a b f = a + b

`f = $$a+b$$`

If you recreate f, then MATLAB removes the value $\mathit{a}+\mathit{b}$ from the expression `f`

.

```
syms f
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. These assumptions (which can be real, integer, rational, and positive) are stored separately from the symbolic object. However, recreating a variable using `sym`

does not clear its assumptions. For more information, see Use Assumptions on Symbolic Variables.

## See Also

## Related Topics

- Create Symbolic Functions
- Create Symbolic Matrices
- Create Symbolic Matrix Variables
- Use Symbolic Objects to Represent Mathematical Objects
- Choose syms or sym Function
- Perform Symbolic Computations
- Choose Numeric or Symbolic Arithmetic
- Add Subscripts, Superscripts, and Accents to Symbolic Variables in the Live Editor