Main Content

Next Step Suggestions for Symbolic Workflows in Live Editor

When you run code that generates a symbolic output in the Live Editor, Live Editor provides a context menu with next step suggestions that are specific to the output. To open the suggestion menu, you can point to the symbolic output and click the three-dot icon , or you can right-click the symbolic output.

Using these suggestion menus, you can insert and execute function calls or Live Editor tasks into live scripts. Typical uses of these suggestion menus include:

  • Evaluate and convert symbolic expressions to numeric values.

  • Simplify and manipulate, or solve symbolic math equations.

  • Plot symbolic expressions.

  • Perform matrix and vector operations, such as finding the inverse and determinant of a matrix, and finding the Jacobian and curl of a vector.

  • Perform calculus functions, such as differentiation, integration, transforms, and solving differential equations.

  • Convert between units of measurement and verify unit dimensions.

For example, in a new live script, create a symbolic expression.

expr = (pi + sym(2))^2
expr = π+22

Click Run to see the result, which is a symbolic output. When you first run code that generates a symbolic output, Live Editor shows the three-dot icon for suggested next steps with a pop-up notification. You can also point to the symbolic output to bring up the three-dot icon. To suppress the pop-up notification in the rest of your workflow, select Don't show again.

Click the three-dot icon located to the right of the output to bring up the suggestion menu. When you point to a menu item, Live Editor gives you a preview of what happens when you select the menu item. For instance, if you point to Approximate numerically, you see the new line of code it suggests.

Select Approximate numerically to add the suggested new line of code. Live Editor inserts the vpa function into the code region and automatically runs the current section to evaluate the expression numerically.

var = vpa(expr)
var = 26.435975015448531572685064532994

As another example, create a symbolic equation. Run a live script to generate the symbolic output. Solve the equation numerically using symbolic suggestions for next steps.

syms x a
eqn = (2*x^2 + a)/(x + 1) == 3
eqn = 

2x2+ax+1=3

To open the suggestion menu, you can also right-click the symbolic output. Select Solving equations > Solve equation numerically.

When you click the Solve equation numerically suggestion, Live Editor inserts the vpasolve function into the code region. Live Editor then automatically runs the current section to solve the equation numerically.

var2 = vpasolve(eqn,x)
var2 = 

(0.75-0.2533.0-8.0a0.2533.0-8.0a+0.75)

The following sections provide more examples showing how to use the interactive suggestion menus in symbolic workflows.

Simplify Symbolic Expression

Create a symbolic expression that contains exponential functions and imaginary numbers. Run the following code to generate the symbolic output.

syms x
expr = 1i*(exp(-1i*x) - exp(1i*x))/(exp(-1i*x) + exp(1i*x))
expr = 

e-xii-exiie-xi+exi

To simplify the expression, right-click the symbolic output and select Rewriting and simplifying expressions > Simplify expression.

Live Editor inserts and applies the Simplify Symbolic Expression live task to interactively simplify or rearrange symbolic expressions. Change the computational effort to Medium to get a simpler result.

Substitute Coefficients and Solve Quadratic Equation

Create a quadratic equation with coefficients a, b, and c. Run the following code to generate the symbolic output.

syms a b c x
eqn = a*x^2 + b*x + c == 0
eqn = ax2+bx+c=0

To substitute the coefficients in the equation, right-click the output and select Substitute variables.

Live Editor inserts the subs function to substitute the coefficients and variables in the equations. For the subs function, Live Editor does not run the function automatically. To substitute a=3, b=2, and c=0, change the second and third arguments of the subs function to [a,b,c] and [3,2,0]. Run the Live Editor section afterwards to apply the subs function.

var3 = subs(eqn,[a,b,c],[3,2,0])
var3 = 3x2+2x=0

To solve the quadratic equation, right-click the output and select Solve equation analytically.

Live Editor inserts and applies the Solve Symbolic Equation live task to interactively find analytic solutions of symbolic equations.

Plot Explicit and Implicit Functions

Create three symbolic variables x, y, and z and a sinusoidal function. Run the following code to generate the symbolic output.

syms x y z
f = sin(2*x)
f = sin(2x)

To plot the sinusoidal function, right-click the output and select Plotting functions > Plot function.

Live Editor plots the sinusoidal function using fplot.

fplot(f)

Figure contains an axes object. The axes object contains an object of type functionline.

Next, create an equation that represents a hyperbola. Run the following code to generate the symbolic output.

eqn = x^2 - y^2 == 1
eqn = x2-y2=1

To plot the sinusoidal function, right-click the output and select Plotting functions > Plot implicit equation in 2-D.

Live Editor plots the hyperbola using fimplicit.

fimplicit(eqn)

Figure contains an axes object. The axes object contains an object of type implicitfunctionline.

Next, create a symbolic function that represents a torus. Run the following code to generate the symbolic output.

f(x,y,z) = (x^2 + y^2 + z^2 + 3^2 - 2^2)^2 - 4*3^2*(x^2+y^2)
f(x, y, z) = x2+y2+z2+52-36x2-36y2

To plot the torus, right-click the output and select Plot implicit equation in 3-D.

Live Editor plots the torus using fimplicit3.

fimplicit3(f)

Figure contains an axes object. The axes object contains an object of type implicitfunctionsurface.

Apply Matrix Functions

Create three symbolic variables x, y, and z and a symbolic matrix. Run the following code to generate the symbolic output.

syms x y z
M1 = sym([x^2 + a, x; y + 2, 3*y^2])
M1 = 

(x2+axy+23y2)

To invert the matrix, right-click the output and select Applying matrix functions > Invert matrix. Note that the Applying matrix functions > Invert matrix suggestion is available only if the symbolic output is a symbolic matrix.

Live Editor inserts and applies the inv function to invert the matrix.

var4 = inv(M1)
var4 = 

(-3y2σ1xσ1y+2σ1-x2+aσ1)where  σ1=-3x2y2+xy+2x-3ay2

Next, create a 1-by-3 symbolic vector. Run the following code to generate the symbolic output.

M2 = sym([x*y*z, y^2, x + z])
M2 = (xyzy2x+z)

To compute the Jacobian of the vector, right-click the output and select Applying matrix functions > Compute Jacobian matrix of vector. Note that the Applying matrix functions > Compute Jacobian matrix of vector suggestion is available only if the symbolic output is a symbolic vector.

Live Editor inserts and applies the jacobian function to compute the Jacobian of the vector.

var5 = jacobian(M2)
var5 = 

(yzxzxy02y0101)

Next, create another 1-by-3 symbolic vector. Run the following code to generate the symbolic output.

M3 = sym([x^2*y, 2*x, z])
M3 = (x2y2xz)

To compute the curl of the vector, right-click the output and select Applying matrix functions > Compute curl of vector field.

Live Editor inserts and applies the curl function to compute the curl of the vector.

var6 = curl(M3)
var6 = 

(002-x2)

Solve Differential Equation, Compute Integral Transform, and Find Poles

Create a second-order differential equation. Run the following code to generate the symbolic output.

syms f(x) s
eqn = diff(f,x,x) == -9*f
eqn(x) = 

2x2 f(x)=-9f(x)

To solve the differential equation, right-click the output and select Solve differential equation. Note that the Solve differential equation suggestion is available only if the symbolic output is a differential equation.

Live Editor inserts and applies the dsolve function to solve the differential equation.

var7 = dsolve(eqn)
var7 = C1cos(3x)-C2sin(3x)

Next, find the Laplace transform of the solution. Right-click the output of dsolve and select Computing integral transforms > Compute Laplace transform.

Live Editor inserts and applies the laplace function to compute the Laplace transform.

var8 = laplace(var7)
var8 = 

C1ss2+9-3C2s2+9

Finally, find the poles of the Laplace transform. Right-click the output of the Laplace transform and select Applying calculus functions > Compute poles of function.

Live Editor inserts and applies the poles function to compute the poles.

var9 = poles(var8,s)
var9 = 

(-3i3i)

Convert Units and Check Consistency of Units

Create a ratio of two lengths with different units. Run the following code to generate the symbolic output.

u = symunit;
ratio = (7*u.mi)/(420*u.ft)
ratio = 

160mi"mile - a physical unit of length."ft"foot - a physical unit of length."

Convert the units to the meter-gram-second system. Right-click the output and select Applying physical unit functions > Convert units. Note that the Applying physical unit functions suggestion is available only if the symbolic output contains symbolic units.

Live Editor inserts and applies the unitConvert function to convert the units to the meter-gram-second system.

var10 = unitConvert(ratio,[symunit('m'),symunit('g'),symunit('s')])
var10 = 88

Next, create two symbolic expressions x and y that describe the x- and y-coordinates of a moving projectile. Create a symbolic equation r that compares the units of x and y to the length units m and ft. Run the following code to generate the symbolic output.

syms theta ts x y r
g = 9.81*u.m/u.s^2;
v = 10*u.m/u.s;
t = ts*u.s;
x = v*cos(theta)*t;
y = v*sin(theta)*t + (-g*t^2)/2;
r = [x == u.m y == u.ft]
r = 

(10tscos(θ)m"meter - a physical unit of length."=m"meter - a physical unit of length."10tssin(θ)m"meter - a physical unit of length."-981ts2200m"meter - a physical unit of length."=ft"foot - a physical unit of length.")

Check the consistency of units in r. Right-click the output and select Applying physical unit functions > Check consistency of units.

Live Editor inserts and applies the checkUnits function to check the consistency and compatibility of the units in r.

The checkUnits function returns a structure containing the fields Consistent and Compatible. The Consistent field returns logical 1(true) if all terms in r have the same dimension and same unit with a conversion factor of 1. The Compatible field returns logical 1(true) if all terms have the same dimension, but not necessarily the same unit.

var11 = checkUnits(r)
var11 = struct with fields:
    Consistent: [1 0]
    Compatible: [1 1]