Documentation Center |
On this page… |
---|
Create Symbolic Objects with Identical Names Create 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 15 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
phi = sym('(1 + sqrt(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. One approach is to enter the command
f = sym('a*x^2 + b*x + c');
which assigns the symbolic expression ax^{2} + 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;
Tip To create a symbolic expression that is a constant, you must 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. |
You also can use sym and syms to create symbolic functions. For example, you can create an arbitrary function f(x, y) where x and y are function variables. The simplest way to create an arbitrary symbolic function is to use syms:
syms f(x, y)
This syntax creates the symbolic function f and symbolic variables x and y.
Alternatively, you can use sym to create a symbolic function. Note that sym only creates the function. It does not create symbolic variables that represent its arguments. You must create these variables before creating a function:
syms x y; f(x, y) = sym('f(x, y)');
If instead of an arbitrary symbolic function you want to create a function defined by a particular mathematical expression, use this two-step approach. First create symbolic variables representing the arguments of the function:
syms x y
Then assign a mathematical expression to the function. In this case, the assignment operation also creates the new symbolic function:
f(x, y) = x^3*y^3
f(x, y) = x^3*y^3
After creating a symbolic function, you can differentiate, integrate, or simplify it, substitute its arguments with values, and perform other mathematical operations. For example, find the second derivative on f(x, y) with respect to variable y. The result d2fy is also a symbolic function.
d2fy = diff(f, y, 2)
d2fy(x, y) = 6*x^3*y
Now evaluate f(x, y) for x = y + 1:
f(y + 1, y)
ans = y^3*(y + 1)^3
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 Delete 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. For example, create the symbolic circulant matrix 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 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
To check if the sum of the elements of the first row equals the sum of the elements of the second column, use the logical function:
logical(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.
The sym function also lets you define a symbolic matrix or vector without having to define its elements in advance. In this case, the sym function generates the elements of a symbolic matrix at the same time that it creates a matrix. The function presents all generated elements using the same form: the base (which must be a valid variable name), a row index, and a column index. Use the first argument of sym to specify the base for the names of generated elements. You can use any valid variable name as a base. To check whether the name is a valid variable name, use the isvarname function. By default, sym separates a row index and a column index by underscore. For example, create the 2-by-4 matrix A with the elements A1_1, ..., A2_4:
A = sym('A', [2 4])
A = [ A1_1, A1_2, A1_3, A1_4] [ A2_1, A2_2, A2_3, A2_4]
To control the format of the generated names of matrix elements, use %d in the first argument:
A = sym('A%d%d', [2 4])
A = [ A11, A12, A13, A14] [ A21, A22, A23, A24]
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 Estimate Precision of Numeric to Symbolic Conversions.
To find symbolic variables in an expression, function, or matrix, use symvar. For example, find all symbolic variables in symbolic expressions f and g:
syms a b n t x f = x^n; g = sin(a*t + b); symvar(f)
ans = [ n, x]
Here, symvar sorts all returned variables alphabetically. Similarly, you can find the symbolic variables in g by entering:
symvar(g)
ans = [ a, b, t]
symvar also can return the first n symbolic variables found in a symbolic expression, matrix, or function. To specify the number of symbolic variables that you want symvar to return, use the second parameter of symvar. For example, return the first two variables found in symbolic expression g:
symvar(g, 2)
ans = [ t, b]
Notice that the first two variables in this case are not a and b. When you call symvar with two arguments, it sorts symbolic variables by their proximity to x.
You also can find symbolic variables in a function:
syms x y w z f(w, z) = x*w + y*z; symvar(f)
ans = [ w, x, y, z]
When you call symvar with two arguments, it returns the function inputs in front of other variables:
symvar(f, 2)
ans = [ w, z]
If you do not specify an independent variable when performing substitution, differentiation, or integration, MATLAB uses a default variable. The default variable is typically the one closest alphabetically to x or, for symbolic functions, the first input argument of a function. To find which variable is chosen as a default variable, use the symvar(f, 1) command. For example:
syms s t f = s + t; symvar(f, 1)
ans = t
syms sx tx f = sx + tx; symvar(f, 1)
ans = tx
For more information on choosing the default symbolic variable, see symvar.