Display symbolic formula from string
displayFormula( displays the symbolic
formula from the string
symstr without evaluating the operations. All
workspace variables that are specified in
symstr are replaced by their
Multiplication Formula of Matrix and Scalar
Create a 3-by-3 matrix. Multiply the matrix by the scalar coefficient
syms K A A = [-1, 0, 1; 1, 2, 0; 1, 1, 0]; B = K^2*A
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")
Display Differential Equation
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)
Display and Evaluate Symbolic Expression
Create a string
S representing a symbolic expression.
S = "exp(2*pi*i)";
Create another string
symstr that contains
symstr = "1 + S + S^2 + cos(S)"
symstr = "1 + S + S^2 + cos(S)"
symstr as a formula without evaluating the operations by using
symstr is replaced by its value.
To evaluate the strings
symstr as symbolic expressions, use
S = str2sym(S)
expr = str2sym(symstr)
Substitute the variable
S with its value by using
subs. Evaluate the result in double precision using
ans = 3.5403
Display and Solve Quadratic Equation
Define a string that represents a quadratic formula with the coefficients
syms a b c k symstr = "a*x^2 + b*x + c";
Display the quadratic formula, replacing
Display the quadratic formula again, replacing
displayFormula(symstr,[a b c],[2 3 -1])
To solve the quadratic equation, convert the string into a symbolic expression using
solve to find the zeros of the quadratic equation.
f = str2sym(symstr); sol = solve(f)
subs to replace
c in the solution with
solValues = subs(sol,[a b c],[2 3 -1])
symstr — String representing symbolic formula
character vector | string scalar | cell array of character vectors | string array
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.
old — Expression or variable to be replaced
character vector | string scalar | cell array of character vectors | string array | symbolic variable | symbolic function | symbolic expression | symbolic array
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 — New value
number | character vector | string scalar | cell array of character vectors | string array | symbolic number | symbolic variable | symbolic expression | symbolic array
New value, specified as a number, character vector, string scalar, cell array of character vectors, string array, symbolic number, variable, expression, or array.
Introduced in R2019b