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)
a1 = 1/3
a1 = 0.3333
a2Sym = sym(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))
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)
accurateNum = sym('123456789012345678')
You can also create symbolic complex numbers, by specifying the imaginary part of a number as
2i, and so on.
sym('1234567 + 1i')
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)
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))
vpaOnSym_17 = vpa(log(sym(2)),17)
vpaOnSym_20 = vpa(log(sym(2)),20)
When you convert large numbers, use quotes to represent them exactly.
inaccurateNum = vpa(123456789012345678)
accurateNum = vpa('123456789012345678')
Create Symbolic Variables
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.
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.
Create the symbolic variables x and y using
syms x y = sym('y')
The first command creates a symbolic variable
x in the MATLAB workspace with the value assigned to the variable
x. The second command creates a symbolic variable
y with the value .
syms, you can create multiple variables in one command. Create the variables
syms a b c
Create Array of Symbolic Variables
If you want to create a MATLAB array of numbered symbolic variables, the
sym syntax is more convenient than the
syms syntax. Therefore, use
sym to create an array of many numbered symbolic variables.
Clear the workspace. Create a row vector containing the symbolic variables and assign it to the MATLAB variable
A. Display the variable in the MATLAB workspace.
clear A = sym('a',[1 20])
Name Size Bytes Class Attributes A 1x20 8 sym
A is a
20 array of 20 symbolic variables.
syms, you can create many fresh symbolic variables with corresponding variable names 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 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.
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
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 .
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
Next, suppose you want to study the quadratic function . First, create the symbolic variables
syms a b c x
Then, create a symbolic expression
f that represents the arithmetical expression .
f = a*x^2 + b*x + c
Solve the quadratic equation for by using
x_0 = solve(f == 0,x)
You can also apply a mathematical function to an arithmetical expression. For example, apply the Bessel function of the first kind to the arithmetical expression and find its derivative with respect to .
J_0 = besselj(0,f)
DJ_0 = diff(J_0,x)
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
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
syms a b f = a + b
If you recreate f, then MATLAB removes the value from the expression
syms 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 Delete Symbolic Objects and Their Assumptions.