displayFormula
R2026bDisplay symbolic formula and text
Description
displayFormula( displays the symbolic
formula and text represented by the string array symstr)symstr without
evaluating the operations. The function replaces any workspace variables specified in
symstr with their current values.
Examples
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 =
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")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 =
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")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)
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)
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)
To evaluate the strings S and symstr as symbolic expressions, use str2sym.
S = str2sym(S)
S =
expr = str2sym(symstr)
expr =
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)
Display the quadratic formula again, replacing a, b, and c with 2, 3, and -1, respectively.
displayFormula(symstr,[a b c],[2 3 -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 =
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 =
Input Arguments
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
symstras 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 R2019bWhen using the displayFormula function, you can display mathematical
symbols and notations by specifying their HTML character identities. Provide the input to
displayFormula(symstr) as a string array that contains regular text
enclosed in single quotes. Within this regular text, you can include HTML character entities
by specifying their names or Unicode code points (in hexadecimal or decimal form). For
example, you can specify the circled times 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.'"
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)