Change Output Display Format of Symbolic Results in the Live Editor
This example shows how to modify the output display format of symbolic results in the MATLAB® Live Editor by using the sympref
function. To demonstrate the use of the function, this example uses a third-degree polynomial.
Modify Output Order of Third-Degree Polynomial
Create a third-degree polynomial consisting of one variable and three coefficients. Define the variable and coefficients as symbolic variables by using the syms
command.
syms x a b c f(x) = (a*x^2 + b)*(b*x - a) + c
f(x) =
Symbolic preferences persist through successive MATLAB sessions. Restore all symbolic preferences to the default values. Expand the polynomial and return the output in the default order.
sympref('default');
poly = expand(f)
poly(x) =
The default output format displays the terms of a symbolic polynomial in alphabetical order, without distinguishing the different symbolic variables in each monomial term.
To change the output order of a polynomial, set the 'PolynomialDisplayStyle'
preference. The 'ascend'
option sorts the output in an ascending order based on the standard mathematical notation for polynomials. Here, the variable x
with the highest order in a monomial term is displayed last.
sympref('PolynomialDisplayStyle','ascend'); poly
poly(x) =
Modify Output Display of Polynomial Roots
By default, symbolic results in Live Scripts are typeset in standard mathematical notation, long expressions are abbreviated, and matrices are set in parentheses (round brackets). You can modify the output display format by setting the symbolic preferences.
Find the roots or zeros of the third-degree polynomial using solve
. In Symbolic Math Toolbox™, the root
function represents the roots of a polynomial.
sols = solve(poly,x)
sols =
To display the results without being abbreviated, set 'AbbreviateOutput'
preference to false
.
sympref('AbbreviateOutput',false);
sols
sols =
To display the symbolic matrix with square brackets, rather than parentheses, set 'MatrixWithSquareBrackets'
preference to true
.
sympref('MatrixWithSquareBrackets',true);
sols
sols =
To display the results in ASCII characters instead of in typeset mathematical notation, set 'TypesetOutput'
preference to false
.
sympref('TypesetOutput',false);
sols
sols = root(a*b*z^3 - a^2*z^2 + b^2*z - a*b + c, z, 1) root(a*b*z^3 - a^2*z^2 + b^2*z - a*b + c, z, 2) root(a*b*z^3 - a^2*z^2 + b^2*z - a*b + c, z, 3)
The preferences you set using sympref
persist through your current and future MATLAB sessions. Restore the symbolic preferences to the default values for the next step.
sympref('default');
Display Floating-Point Output of Symbolic Numbers
Replace the polynomial coefficients with symbolic numbers using subs
. The function returns the solutions without any approximation.
numSols = subs(sols,[a b c],[sqrt(2) pi 0.001])
numSols =
To display the results in floating-point format, set 'FloatingPointOutput'
preference to true
. This option displays symbolic numbers in fixed-decimal format with 4 digits after the decimal point. For a complex result of class 'sym'
, this preference affects the real and imaginary parts independently.
sympref('FloatingPointOutput',true);
numSols
numSols =
The display preferences you set do not affect the computation of symbolic results. You can use the vpa
function to approximate symbolic numbers in floating-point precision with 4 significant digits.
vpaSols = vpa(numSols,4)
vpaSols =
Restore the default value of 'FloatingPointOutput'
by specifying the 'default'
option.
sympref('FloatingPointOutput','default');