To manipulate a symbolic mathematical
expression, you would need to define the symbolic variables first. If using the
native Matlab symbolic math toolbox, you can create a symbolic variable by
calling
sym('var')
(scalar) or sym('var',[2,3])
(array).
FROST uses the similar syntax to create a symbolic variable.Overview
SymVariable is an
inherited subclass
of SymExpression,
hence it can be used as any regular symbolic expression in the code. When a
SymVariable
object is created, it will be associated with one or a group of
symbolic variables in Mathematica. You can:
- create a scalar, 1-D or 2-D array variable
- reference sub-elements using indices
- assign special text labels for each elements and reference using labels
Create SymVariable objects
- Create a scalar variable
>> a = SymVariable('var'); % a is the matlab object that represents a symbolic variable 'var' in Mathematica
ans =
var
- Create a 1-D array variables:
>> a_vec = SymVariable('var',3)
ans =
{var$1, var$2, var$3}
- Create a 2-D array variables:
>> a_mat = SymVariable('var',[2,3])
ans =
{{var$1$1, var$1$2, var$1$3}, {var$2$1, var$2$2, var$2$3}}
Note: The underscore symbol
_
is considered as
a special character in Mathematica, hence we use the dollar sign $
to isolate indices in different dimensions. Use SymVariable objects
Once a SymVariable
object is created, you can use it as regular data to
perform symbolic math calculation. For example:
>> b = cos(a)
ans =
Cos[var]
>> b + a
ans =
var + Cos[var]
>> b_vec = cos(a_vec)
ans =
{Cos[var$1], Cos[var$2], Cos[var$3]}
>> b_mat = cos(a_mat)
ans =
{{Cos[var$1$1], Cos[var$1$2], Cos[var$1$3]}, {Cos[var$2$1], Cos[var$2$2], Cos[var$2$3]}}
Note: Any resulting variable from a symbolic calculation is
a
SymExpression
object. b
, b_vec
,
b_mat
are all SymExpression
objects not
SymVariable
objects. Reference by index
You can access a sub-entries of an array of SymVariable
using their indices
similar to other Matlab variables. For example:
>> a2 = a_vec(2)
ans =
{var$2}
>> a_1_3 = a_vec([1,3])
ans =
{var$1, var$3}
>> a2_1_3 = a_mat(2,[1,3])
ans =
{{var$2$1, var$2$3}}
Warning:
The returned sub-entries are given as
SymExpression
objects.
Reference by label
Labels can be assigned to each entry of a 2-D array of SymVariable
object at initialization.
>> a_vec = SymVariable('var',[3,1],{'alpha','beta','gamma'})
ans =
{{var$1$1}, {var$2$1}, {var$3$1}}
If labels are assigned, then it can be used to reference the individual element in the array. For example:
>> alpha = a_vec('alpha')
ans =
{var$1$1}