Main Content

Add Subscripts, Superscripts, and Accents to Symbolic Variables in the Live Editor

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

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 an 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˙

This accent list shows the supported accent suffixes and the corresponding display output. The dagger accent with symbol is available since R2022b and the degree accent with ° symbol is available since R2023a.

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

(astx*dagxdegx°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 complex conjugate transpose of the symbolic variables in accentList(:,2) using the ctranspose or ' function.

conjVar = accentList(:,2)'
conjVar = 

(x*xx°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*xx°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 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.

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 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

Add Signs to Subscripts and Superscripts

Starting in R2022b, you can also add signs as suffixes when creating symbolic variables. The supported signs are -, +, ±, and #. This sign list shows the supported sign suffixes and the corresponding display output.

suffix = ["minus"; "plus"; "plusmn"; "hash"];
signList = [suffix, sym("A_" + suffix)]
signList = 

(minusA-plusA+plusmnA±hashA#)

You can add these signs in combinations with other symbols when creating symbolic variables. To combine these signs with other subscripts or superscripts, you can use one underscore (_) or two underscores (__) with other suffixes, respectively. For example:

A1 = sym("A_minus_x_bar")
A1 = A-x
A2 = sym("A__plusmn__c")
A2 = A±c
A3 = sym("A_hash_123")
A3 = A#123
A4 = sym("A_minus_x_plus_y__r__theta")
A4 = A-x,+yr,θ

Related Topics