주요 콘텐츠

displayFormula

R2026b

Display symbolic formula and text

Description

displayFormula(symstr) displays the symbolic formula and text represented by the string array symstr without evaluating the operations. The function replaces any workspace variables specified in symstr with their current values.

example

displayFormula(symstr,old,new) replaces only the expression or variable old with new. The function does not replace any expressions or variables other than old with their values.

example

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 = 

(-K20K2K22K20K2K20)

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

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

displayFormula("F = K^2*A")

F=K2(-101120110)

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 = 

(B1=A1,1v1+A1,2v2+A1,3v3B2=A2,1v1+A2,2v2+A2,3v3B3=A3,1v1+A3,2v2+A3,3v3)

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

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

displayFormula("B == A*v")

(B1B2B3)=(A1,1A1,2A1,3A2,1A2,2A2,3A3,1A3,2A3,3)(v1v2v3)

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

S = "m*diff(y,t,t) == m*g - k*y";
symstr = ["'The equation of motion is'"; S; ...
          "'where k is the elastic coefficient.'"];
displayFormula(symstr)
The equation of motion is

m2t2 y=mg-ky

where k is the elastic coefficient.

Since R2026b

Create two vectors and compute their cross product.

a = sym([2 3 1]);
b = sym([-1 2 -3]);
c = cross(a,b);

Create a string for the introductory text.

str1 = "'The cross product of the two vectors is: '";

Create a string row vector for the formula that includes the × symbol by using its HTML representation, along with the result of the cross product.

str2 = ["'a ⨯ b = '","c"];

Combine the strings.

symstr = [str1 str2]
symstr = 1×3 string
    "'The cross product of the two vectors is: '"    "'a ⨯ b = '"    "c"

Display the cross product of the two vectors.

displayFormula(symstr)
The cross product of the two vectors is: a ⨯ b = (-1157)

Create a string S that represents 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+e2πi+e2πi2+cos(e2πi)

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

S = str2sym(S)
S = 1
expr = str2sym(symstr)
expr = S+cos(S)+S2+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

Create 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)
kx2+bx+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])
2x2+3x-1

To solve the quadratic equation, convert the string to a symbolic expression using str2sym. Find the zeros of the quadratic equation using solve.

f = str2sym(symstr);
sol = solve(f)
sol = 

(-b+b2-4ac2a-b-b2-4ac2a)

Replace a, b, and c in the solution with 2, 3, and -1, respectively, by using subs.

solValues = subs(sol,[a b c],[2 3 -1])
solValues = 

(-174-34174-34)

Input Arguments

collapse all

String representing a symbolic formula and text, specified as a string scalar or string array.

  • To display a symbolic formula that includes built-in operators, functions, and substituted workspace variables, define symstr as a string enclosed in double quotes. For example, symstr = "cos(pi/4) - sqrt(2)".

  • To include regular text in the displayed output, enclose the text in single quotes when defining the string symstr. For example, symstr = ["'The angle summation is:'", "theta"].

  • To display mathematical symbols and notations within your regular text, you can use HTML character entities by specifying their names or Unicode code points (in hexadecimal or decimal form). (since R2026b) For example, you can specify the outer product symbol in several ways:

    symstr = "'The outer product of two vectors is u ⊗ v.'"

    symstr = "'The outer product of two vectors is u ⊗ v.'"

    symstr = "'The outer product of two vectors is u ⊗ v.'"

For examples that show how to combine several strings that represent symbolic formulas and regular text as a string array, see Display Differential Equation and Display Cross Product of Two Vectors.

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

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

Version History

Introduced in R2019b

expand all

See Also

| | | |