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.

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 = ${F}_{a}+{F}_{b}$

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 = ${F}_{a}$

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 = ${F}^{a}+{F}^{b}$

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

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 $\mathit{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 = $k x-c \stackrel{˙}{x}+m \stackrel{¨}{x}=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 = $\stackrel{‾}{\stackrel{˙}{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 =

$\left(\begin{array}{cc}\mathrm{ast}& {x}^{*}\\ \mathrm{hat}& \stackrel{ˆ}{x}\\ \mathrm{tilde}& \stackrel{\sim }{x}\\ \mathrm{vec}& \stackrel{\to }{x}\\ \mathrm{bar}& \stackrel{‾}{x}\\ \mathrm{ubar}& \underset{‾}{x}\\ \mathrm{dot}& \stackrel{˙}{x}\\ \mathrm{ddot}& \stackrel{¨}{x}\\ \mathrm{tdot}& \stackrel{⃛}{x}\\ \mathrm{qdot}& \stackrel{⃜}{x}\\ \mathrm{prime}& {x}^{\prime }\\ \mathrm{dprime}& {x}^{\prime \prime }\\ \mathrm{tprime}& {x}^{\prime \prime \prime }\\ \mathrm{qprime}& {x}^{\prime \prime \prime \prime }\end{array}\right)$

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 =

$\left(\begin{array}{cccccccccccccc}\stackrel{‾}{{x}^{*}}& \stackrel{‾}{\stackrel{ˆ}{x}}& \stackrel{‾}{\stackrel{\sim }{x}}& \stackrel{‾}{\stackrel{\to }{x}}& \stackrel{‾}{\stackrel{‾}{x}}& \stackrel{‾}{\underset{‾}{x}}& \stackrel{‾}{\stackrel{˙}{x}}& \stackrel{‾}{\stackrel{¨}{x}}& \stackrel{‾}{\stackrel{⃛}{x}}& \stackrel{‾}{\stackrel{⃜}{x}}& \stackrel{‾}{{x}^{\prime }}& \stackrel{‾}{{x}^{\prime \prime }}& \stackrel{‾}{{x}^{\prime \prime \prime }}& \stackrel{‾}{{x}^{\prime \prime \prime \prime }}\end{array}\right)$

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 =

$\left(\begin{array}{cccccccccccccc}{x}^{*}& \stackrel{ˆ}{x}& \stackrel{\sim }{x}& \stackrel{\to }{x}& \stackrel{‾}{x}& \underset{‾}{x}& \stackrel{˙}{x}& \stackrel{¨}{x}& \stackrel{⃛}{x}& \stackrel{⃜}{x}& {x}^{\prime }& {x}^{\prime \prime }& {x}^{\prime \prime \prime }& {x}^{\prime \prime \prime \prime }\end{array}\right)$

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 = ${x}_{b,1}^{a,1}$
x2 = sym('x__b_1_a__1')
x2 = ${x}_{1,a}^{b,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 =

$\stackrel{\to }{{v}^{\prime }}$

v2 = sym('v_vec_prime')
v2 =

${\stackrel{\to }{v}}^{\prime }$

va = sym('v__a_bar_prime')
va = ${v}^{{\stackrel{‾}{a}}^{\prime }}$
vb = sym('v_bar__b_prime')
vb = ${\stackrel{‾}{v}}^{{b}^{\prime }}$

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 = ${F}_{t}^{a}$
F2 = sym('F_t__a')
F2 = ${F}_{t}^{a}$
F3 = sym('F__a_t')
F3 = ${F}_{t}^{a}$

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

Symbolic Math Toolbox Documentation

Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos