# 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 = ${\left(\pi +2\right)}^{2}$`

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 =  $\frac{2 {x}^{2}+a}{x+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 =  $\left(\begin{array}{c}0.75-0.25 \sqrt{33.0-8.0 a}\\ 0.25 \sqrt{33.0-8.0 a}+0.75\end{array}\right)$```

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 =  $\frac{{\mathrm{e}}^{-x \mathrm{i}} \mathrm{i}-{\mathrm{e}}^{x \mathrm{i}} \mathrm{i}}{{\mathrm{e}}^{-x \mathrm{i}}+{\mathrm{e}}^{x \mathrm{i}}}$```

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 = $a {x}^{2}+b x+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 = $3 {x}^{2}+2 x=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 = $\mathrm{sin}\left(2 x\right)$`

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)` Next, create an equation that represents a hyperbola. Run the following code to generate the symbolic output.

`eqn = x^2 - y^2 == 1`
`eqn = ${x}^{2}-{y}^{2}=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)` 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) = ${\left({x}^{2}+{y}^{2}+{z}^{2}+5\right)}^{2}-36 {x}^{2}-36 {y}^{2}$`

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)` ### 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 =  $\left(\begin{array}{cc}{x}^{2}+a& x\\ y+2& 3 {y}^{2}\end{array}\right)$```

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

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 = $\left(\begin{array}{ccc}x y z& {y}^{2}& x+z\end{array}\right)$`

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 =  $\left(\begin{array}{ccc}y z& x z& x y\\ 0& 2 y& 0\\ 1& 0& 1\end{array}\right)$```

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 = $\left(\begin{array}{ccc}{x}^{2} y& 2 x& z\end{array}\right)$`

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 =  $\left(\begin{array}{c}0\\ 0\\ 2-{x}^{2}\end{array}\right)$```

### 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) =  ```

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 = ${C}_{1} \mathrm{cos}\left(3 x\right)-{C}_{2} \mathrm{sin}\left(3 x\right)$`

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 =  $\frac{{C}_{1} s}{{s}^{2}+9}-\frac{3 {C}_{2}}{{s}^{2}+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 =  $\left(\begin{array}{c}-3 \mathrm{i}\\ 3 \mathrm{i}\end{array}\right)$```

### 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 =  $\frac{1}{60} \frac{\mathrm{mi}\mathrm{"mile - a physical unit of length."}}{\mathrm{ft}\mathrm{"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 =  $\left(\begin{array}{cc}10 \mathrm{ts} \mathrm{cos}\left(\theta \right) \mathrm{m}\mathrm{"meter - a physical unit of length."}=\mathrm{m}\mathrm{"meter - a physical unit of length."}& 10 \mathrm{ts} \mathrm{sin}\left(\theta \right) \mathrm{m}\mathrm{"meter - a physical unit of length."}-\frac{981 {\mathrm{ts}}^{2}}{200} \mathrm{m}\mathrm{"meter - a physical unit of length."}=\mathrm{ft}\mathrm{"foot - a physical unit of length."}\end{array}\right)$```

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

## Support

#### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos