Change Output Display Format of Symbolic Results

This example shows how to modify the output display format of symbolic results in Symbolic Math Toolbox™ 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.

sympref('default');
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.

sympref('PolynomialDisplayStyle','ascend');
poly
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.

sympref('AbbreviateOutput',false);
sols
sols = 

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

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

sympref('MatrixWithSquareBrackets',true);
sols
sols = 

[root(abz3-a2z2+b2z-ab+c,z,1)root(abz3-a2z2+b2z-ab+c,z,2)root(abz3-a2z2+b2z-ab+c,z,3)]

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 = 

(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.

sympref('FloatingPointOutput',true);
numSols
numSols = 

(0.45014.6427e-05-1.4904i4.6427e-05+1.4904i)

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 = 

(0.4501-1.4904i1.4904i)