In Symbolic Math Toolbox™, you can create symbolic objects by using either
sym. These two functions are conceptually different.
symsfunction creates a symbolic object that is automatically assigned to a MATLAB® variable with the same name.
symfunction refers to a symbolic object that can be assigned to a MATLAB variable with the same name or a different name.
The following examples discuss the differences between the
sym functions. For more examples on the use cases of each function, see
Assign Symbolic Variables to MATLAB Variables
syms function creates a variable dynamically. For example, the command
syms x creates the symbolic variable
x and automatically assigns it to a MATLAB variable with the same name.
syms x x
You can then use the variable
x in the MATLAB workspace for symbolic workflow, such as finding the roots of a polynomial.
f = x^2 + x - 6
x0 = solve(f)
sym function refers to a symbolic variable, which you can then assign to a MATLAB variable with a different name. For example, the command
f1 = sym('x') refers to the symbolic variable
x and assigns it to the MATLAB variable
clear f1 = sym('x')
You can then use the variable
f1 in the MATLAB workspace for symbolic workflow, such as finding the zeros of a sine function.
f2 = sin(f1)
[solx,parameters,conditions] = solve(f2,f1,'ReturnConditions',true)
Create Symbolic Number
syms function to create a symbolic variable
x and automatically assign it to a MATLAB variable
x. When you assign a number to the MATLAB variable
x, the number is represented in double-precision and this assignment overwrites the previous assignment to a symbolic variable. The class of
syms x x = 1/33
x = 0.0303
ans = 'double'
sym function to refer to an exact symbolic number without floating-point approximation. You can then assign this number to the MATLAB variable
x. The class of
x = sym('1/33')
ans = 'sym'
Create Symbolic Variable with Assumptions
When you create a symbolic variable with an assumption, MATLAB stores the symbolic variable and its assumption separately.
syms to create a symbolic variable that is assigned to a MATLAB variable with the same name. You get a fresh symbolic variable with no assumptions. If you declare a variable using
syms, existing assumptions are cleared.
syms x positive syms x assumptions
ans = Empty sym: 1-by-0
sym to refer to an existing symbolic variable. If this symbolic variable was used in your MATLAB session before, then
sym refers to it and its current assumption. If it was not used before, then
sym creates it with no assumptions.
syms x positive x = sym('x'); assumptions
Create Many Symbolic Variables
To create many symbolic variables simultaneously, using the
syms function is more convenient. You can create multiple variables in one line of code.
syms a b c
When you use
sym, you have to declare MATLAB variables one by one and refer them to the corresponding symbolic variables.
a = sym('a'); b = sym('b'); c = sym('c');
Create Array of Symbolic Variables
To declare a symbolic array that contains symbolic variables as its elements, you can use either
syms a [1 3] creates a 1-by-3 symbolic array
a and the symbolic variables
a3 in the workspace. The symbolic variables
a3 are automatically assigned to the symbolic array
clear syms a [1 3] a
Name Size Bytes Class Attributes a 1x3 8 sym a1 1x1 8 sym a2 1x1 8 sym a3 1x1 8 sym
a = sym('a',[1 3]) refers to the symbolic variables
a3, which are assigned to the symbolic array
a in the workspace. The elements
a3 are not created in the workspace.
clear a = sym('a',[1 3])
Name Size Bytes Class Attributes a 1x3 8 sym
Create Symbolic Variable in Parallel For Loop
To create a symbolic variable in each worker for parallel computation, use
sym. For example, you can create a symbolic variable
k in a
parfor loop that performs sums of series using
S = zeros(1,10); parfor i = 1:10 k = sym('k'); S(i) = symsum(1/k^i,k,1,Inf); end
Starting parallel pool (parpool) using the 'Processes' profile ... Connected to the parallel pool (number of workers: 6).
You cannot use
syms to create a symbolic variable in a
parfor loop because it modifies the global state of the workspace.
Create Symbolic Variable in Function
To declare a symbolic variable within a function, use
sym. For example, you can explicitly define a MATLAB variable
x in the parent function workspace and refer
x to a symbolic variable with the same name.
function primaryFx x = sym('x') function nestedFx ... end end
Functions make the workspace static, so you cannot dynamically add variables using