Products & Services Solutions Academia Support User Community Company

Learn more about Symbolic Math Toolbox   

Creating Symbolic Variables and Expressions

Creating 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:

The sym command:

Creating Symbolic Expressions

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;

Creating Symbolic Objects with Identical Names

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.

Creating a Matrix of Symbolic Variables

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 =
     1

From this example, you can see that using symbolic objects is very similar to using regular MATLAB numeric objects.

Creating a Matrix of Symbolic Numbers

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.2000

By 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.

Finding Symbolic Variables in Expressions and Matrices

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]
  


Recommended Products

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