Add Subscripts, Superscripts, and Accents to Symbolic Variables

This example shows how to add subscripts, superscripts, and accents to symbolic variables. MATLAB® Live Editor displays symbolic variables with subscripts, superscripts, and accents in standard mathematical notation.

Add Subscripts and Superscripts

To add subscripts to symbolic variables in live scripts, append the corresponding index to the variable using one underscore (_). For example, create two symbolic variables with subscripts using syms. Use these variables in an expression.

syms F_a F_b
Ftot = F_a + F_b
Ftot = Fa+Fb

You can also use sym to create a symbolic variable with a subscript and assign the variable to a symbolic expression.

Fa = sym('F_a')
Fa = Fa

To add superscripts to symbolic variables, append the corresponding index to the variable using two underscores (__). For example, create two symbolic variables with superscripts.

syms F__a F__b
Ftot = F__a + F__b
Ftot = Fa+Fb

When you assign symbolic variables to an expression, the symbolic expression is displayed in ASCII format.

Add Accents

To add accents to symbolic variables in live scripts, append the corresponding suffix to the variable using the underscore (_). For example, create symbolic variables with one dot and two dots over the symbol x. Use these variables in an equation.

syms x x_dot x_ddot c m k
eq1 = m*x_ddot - c*x_dot + k*x == 0
eq1 = kx-cx˙+mx¨=0

When you compute the complex conjugate of a symbolic variable with an accent, a bar notation is added above the variable. For example, find the complex conjugate of x_dot using the conj function.

xConj = conj(x_dot)
xConj = x˙

The supported accent suffixes for symbolic variables follow.

suffix = ["ast"; "hat"; "tilde"; "vec"; "bar"; ...
   "ubar"; "dot"; "ddot"; "tdot"; "qdot"; ...
   "prime"; "dprime"; "tprime"; "qprime"];
accentList = [suffix, sym("x_" + suffix)]
accentList = 

(astx*hatxˆtildexvecxbarxubarxdotx˙ddotx¨tdotxqdotxprimexdprimextprimexqprimex)

When you compute the complex conjugate transpose of a matrix containing symbolic variables, a bar notation is also added above each variable. For example, find the conjugate transpose of the symbolic variables in accentList(:,2) using the ctranspose or ' function.

conjVar = accentList(:,2)'
conjVar = 

(x*xˆxxxxx˙x¨xxxxxx)

When you compute the nonconjugate transpose of a matrix containing symbolic variables, the display output is unchanged. For example, find the nonconjugate transpose of the symbolic variables in accentList(:,2) using the transpose or .' function.

nonconjVar = accentList(:,2).'
nonconjVar = 

(x*xˆxxxxx˙x¨xxxxxx)

Add Multiple Subscripts, Superscripts, and Accents

You can create symbolic variables with multiple subscripts, superscripts, and accents. The multiple suffixes are assigned to the symbolic variables from left to right.

Create symbolic variables with multiple subscripts and superscripts. If you add multiple subscripts and superscripts, then the input indices are separated with a comma sign and displayed from left to right.

x1 = sym('x_b_1__a__1')
x1 = xb,1a,1
x2 = sym('x__b_1_a__1')
x2 = x1,ab,1

Now create symbolic variables with multiple accents. If you add multiple accents, then the input accents are assigned from left to right to the closest preceding variable or index. Some examples follow.

v1 = sym('v_prime_vec')
v1 = 

v

v2 = sym('v_vec_prime')
v2 = 

v

va = sym('v__a_bar_prime')
va = va
vb = sym('v_bar__b_prime')
vb = vb

Adding suffixes to the symbolic variables can produce similar output. However, the variables are equal only if their suffixes are also in the same order. For example, create three symbolic variables that produce similar output.

syms F_t__a
F1 = F_t__a
F1 = Fta
F2 = sym('F_t__a')
F2 = Fta
F3 = sym('F__a_t')
F3 = Fta

Determine if the symbolic variables are equal to each other using the isequal function.

TF_12 = isequal(F1,F2)
TF_12 = logical
   1

TF_23 = isequal(F2,F3)
TF_23 = logical
   0

Related Topics