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.
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
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
f1 = sym('x')
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
Symbolic Variable in Nested Function
To declare a symbolic variable within a nested 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
Nested functions make the workspace static, so you cannot dynamically add variables using