# displayFormula

Display symbolic formula from string

Since R2019b

## Syntax

``displayFormula(symstr)``
``displayFormula(symstr,old,new)``

## Description

example

````displayFormula(symstr)` displays the symbolic formula from the string `symstr` without evaluating the operations. All workspace variables that are specified in `symstr` are replaced by their values.```

example

````displayFormula(symstr,old,new)` replaces only the expression or variable `old` with `new`. Expressions or variables other than `old` are not replaced by their values.```

## Examples

collapse all

Create a 3-by-3 matrix. Multiply the matrix by the scalar coefficient `K^2`.

```syms K A A = [-1, 0, 1; 1, 2, 0; 1, 1, 0]; B = K^2*A```
```B =  $\left(\begin{array}{ccc}-{K}^{2}& 0& {K}^{2}\\ {K}^{2}& 2 {K}^{2}& 0\\ {K}^{2}& {K}^{2}& 0\end{array}\right)$```

The result automatically shows the multiplication being carried out element-wise.

Show the multiplication formula without evaluating the operations by using `displayFormula`. Input the formula as a string. The variable `A` in the string is replaced by its values.

`displayFormula("F = K^2*A")`
`$F={K}^{2} \left(\begin{array}{ccc}-1& 0& 1\\ 1& 2& 0\\ 1& 1& 0\end{array}\right)$`

Create a 3-by-3 matrix and a 3-by-1 vector. Create a symbolic equation that multiplies the matrix and the vector.

```syms A [3 3] syms v B [3 1] eqn = B == A*v```
```eqn =  $\left(\begin{array}{c}{B}_{1}={A}_{1,1} {v}_{1}+{A}_{1,2} {v}_{2}+{A}_{1,3} {v}_{3}\\ {B}_{2}={A}_{2,1} {v}_{1}+{A}_{2,2} {v}_{2}+{A}_{2,3} {v}_{3}\\ {B}_{3}={A}_{3,1} {v}_{1}+{A}_{3,2} {v}_{2}+{A}_{3,3} {v}_{3}\end{array}\right)$```

The result shows the multiplication being carried out where the elements of the matrix and the vector are combined.

Show the multiplication formula without combining the elements by using `displayFormula`. Input the formula as a string.

`displayFormula("B == A*v")`
`$\left(\begin{array}{c}{B}_{1}\\ {B}_{2}\\ {B}_{3}\end{array}\right)=\left(\begin{array}{ccc}{A}_{1,1}& {A}_{1,2}& {A}_{1,3}\\ {A}_{2,1}& {A}_{2,2}& {A}_{2,3}\\ {A}_{3,1}& {A}_{3,2}& {A}_{3,3}\end{array}\right) \left(\begin{array}{c}{v}_{1}\\ {v}_{2}\\ {v}_{3}\end{array}\right)$`

Define a string that describes a differential equation.

`S = "m*diff(y,t,t) == m*g-k*y";`

Create a string array that combines the differential equation and additional text. Display the formula along with the text.

```symstr = ["'The equation of motion is'"; S;"'where k is the elastic coefficient.'"]; displayFormula(symstr)```

Create a string `S` representing a symbolic expression.

`S = "exp(2*pi*i)";`

Create another string `symstr` that contains `S`.

`symstr = "1 + S + S^2 + cos(S)"`
```symstr = "1 + S + S^2 + cos(S)" ```

Display `symstr` as a formula without evaluating the operations by using `displayFormula`. `S` in `symstr` is replaced by its value.

`displayFormula(symstr)`
`$1+{\mathrm{e}}^{2 \pi \mathrm{i}}+{\left({\mathrm{e}}^{2 \pi \mathrm{i}}\right)}^{2}+\mathrm{cos}\left({\mathrm{e}}^{2 \pi \mathrm{i}}\right)$`

To evaluate the strings `S` and `symstr` as symbolic expressions, use `str2sym`.

`S = str2sym(S)`
`S = $1$`
`expr = str2sym(symstr)`
`expr = $S+\mathrm{cos}\left(S\right)+{S}^{2}+1$`

Substitute the variable `S` with its value by using `subs`. Evaluate the result in double precision using `double`.

`double(subs(expr))`
```ans = 3.5403 ```

Define a string that represents a quadratic formula with the coefficients `a`, `b`, and `c`.

```syms a b c k symstr = "a*x^2 + b*x + c";```

Display the quadratic formula, replacing `a` with `k`.

`displayFormula(symstr,a,k)`
`$k {x}^{2}+b x+c$`

Display the quadratic formula again, replacing `a`, `b`, and `c` with `2`, `3`, and `-1`, respectively.

`displayFormula(symstr,[a b c],[2 3 -1])`
`$2 {x}^{2}+3 x-1$`

To solve the quadratic equation, convert the string into a symbolic expression using `str2sym`. Use `solve` to find the zeros of the quadratic equation.

```f = str2sym(symstr); sol = solve(f)```
```sol =  $\left(\begin{array}{c}-\frac{b+\sqrt{{b}^{2}-4 a c}}{2 a}\\ -\frac{b-\sqrt{{b}^{2}-4 a c}}{2 a}\end{array}\right)$```

Use `subs` to replace `a`, `b`, and `c` in the solution with `2`, `3`, and `-1`, respectively.

`solValues = subs(sol,[a b c],[2 3 -1])`
```solValues =  $\left(\begin{array}{c}-\frac{\sqrt{17}}{4}-\frac{3}{4}\\ \frac{\sqrt{17}}{4}-\frac{3}{4}\end{array}\right)$```

## Input Arguments

collapse all

String representing a symbolic formula, specified as a character vector, string scalar, cell array of character vectors, or string array.

You can also combine a string that represents a symbolic formula with regular text (enclosed in single quotation marks) as a string array. For an example, see Display Differential Equation.

Expression or variable to be replaced, specified as a character vector, string scalar, cell array of character vectors, string array, symbolic variable, function, expression, or array.

New value, specified as a number, character vector, string scalar, cell array of character vectors, string array, symbolic number, variable, expression, or array.

## Version History

Introduced in R2019b