Main Content

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) = c-ax2+ba-bx

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.

poly = expand(f)
poly(x) = -a2x2+abx3-ab+b2x+c

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.

poly(x) = c-ab+b2x-a2x2+abx3

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 = 

(root(σ1,z,1)root(σ1,z,2)root(σ1,z,3))where  σ1=abz3-a2z2+b2z-ab+c

To display the results without being abbreviated, set 'AbbreviateOutput' preference to false.

sols = 


To display the symbolic matrix with square brackets, rather than parentheses, set 'MatrixWithSquareBrackets' preference to true.

sols = 


To display the results in ASCII characters instead of in typeset mathematical notation, set 'TypesetOutput' preference to false.

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.


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 = 

(root(σ1,z,1)root(σ1,z,2)root(σ1,z,3))where  σ1=1000π2z3-2000z2+1000zπ2-1000π2+1

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.

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.