| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Symbolic Math Toolbox |
| Contents | Index |
| Learn more about Symbolic Math Toolbox |
| On this page… |
|---|
Creating Symbolic Objects with Identical Names Creating a Matrix of Symbolic Variables |
The sym command creates symbolic variables and expressions. For example, the commands
x = sym('x');
a = sym('alpha');create a symbolic variable x with the value x assigned to it in the MATLAB workspace and a symbolic variable a with the value alpha assigned to it. An alternate way to create a symbolic object is to use the syms command:
syms x;
a = sym('alpha');You can use sym or syms to create symbolic variables. The syms command:
Does not use parentheses and quotation marks: syms x
Can create multiple objects with one call
Serves best for creating individual single and multiple symbolic variables
The sym command:
Requires parentheses and quotation marks: x = sym('x'). When creating a symbolic number with 10 or fewer decimal digits, you can skip the quotation marks: f = sym(5).
Creates one symbolic object with each call.
Serves best for creating symbolic numbers and symbolic expressions.
Serves best for creating symbolic objects in functions and scripts.
Suppose you want to use a symbolic variable to represent the golden ratio
![]()
The command
rho = sym('(1 + sqrt(5))/2');achieves this goal. Now you can perform various mathematical operations on rho. For example,
f = rho^2 - rho - 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 = ax2 + bx + c. One approach is to enter the command
f = sym('a*x^2 + b*x + c');which assigns the symbolic expression ax2 + bx + c to the variable f. However, in this case, Symbolic Math Toolbox software does not create variables corresponding to the terms of the expression: a, b, c, and x. To perform symbolic math operations on f, you need to create the variables explicitly. A better alternative is to enter the commands
a = sym('a');
b = sym('b');
c = sym('c');
x = sym('x');or simply
syms a b c x
Then, enter
f = a*x^2 + b*x + c;
Note To create a symbolic expression that is a constant, you must use the sym command. Do not use syms command 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, and positive. These assumptions are stored separately from the symbolic object. See Deleting Symbolic Objects and Their Assumptions for more information.
A circulant matrix has the property that each row is obtained from the previous one by cyclically permuting the entries one step forward. You can create the symbolic circulant matrix A whose elements are a, b, and c, using the commands:
syms a b c; A = [a b c; c a b; b c a]
A = [ a, b, c] [ c, a, b] [ b, c, a]
Since the matrix A is circulant, the sum of elements over each row and each column is the same. Find the sum of all the elements of the first row:
sum(A(1,:))
ans = a + b + c
Check if the sum of the elements of the first row equals the sum of the elements of the second column:
sum(A(1,:)) == sum(A(:,2))
The sums are equal:
ans =
1From this example, you can see that using symbolic objects is very similar to using regular MATLAB numeric objects.
A particularly effective use of sym is to convert a matrix from numeric to symbolic form. The command
A = hilb(3)
generates the 3-by-3 Hilbert matrix:
A =
1.0000 0.5000 0.3333
0.5000 0.3333 0.2500
0.3333 0.2500 0.2000By applying sym to A
A = sym(A)
you can obtain the precise symbolic form of the 3-by-3 Hilbert matrix:
A = [ 1, 1/2, 1/3] [ 1/2, 1/3, 1/4] [ 1/3, 1/4, 1/5]
For more information on numeric to symbolic conversions see Estimating the Precision of Numeric to Symbolic Conversions.
To determine what symbolic variables are present in an expression, use the symvar command. For example, given the symbolic expressions f and g defined by
syms a b n t x z; f = x^n; g = sin(a*t + b);
you can find the symbolic variables in f by entering:
symvar(f)
ans = [ n, x]
Similarly, you can find the symbolic variables in g by entering:
symvar(g)
ans = [ a, b, t]
![]() | Symbolic Objects | Performing Symbolic Computations | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |