Change Output Format of Symbolic and Variable-Precision Arithmetic
Symbolic Math Toolbox™ enables you to perform analytical calculations by using symbolic numbers, variables, functions, and expressions to represent mathematical objects in their exact form. You can also use variable-precision arithmetic to specify the significant digits used in symbolic computations. However, symbolic outputs in their analytical form can be long and complicated. You can change the display format of symbolic outputs by setting preferences for the toolbox.
This example shows how to:
Use
sym
andsyms
to create symbolic numbers, variables, functions, and expressions to perform analytical calculations.Use
digits
andvpa
to evaluate symbolic objects with variable-precision arithmetic to specified significant digits.Use
sympref
to set the output display format of symbolic objects to a short, fixed-decimal format with four digits after the decimal point, without affecting the precision used in symbolic computations.
If you want to use the results of your symbolic computations in numeric computations using MATLAB® (without Symbolic Math Toolbox), you can convert symbolic values to numeric values by using the double
and matlabFunction
functions.
Symbolic Arithmetic
Symbolic Math Toolbox provides the sym
and syms
functions to create symbolic objects for symbolic computations. In symbolic arithmetic, you can perform analytical computations involving numbers and variables in their exact form, such as 5/11
, pi
, or x^(1/2)
.
For example, create the number in its exact form by using sym
.
a = sqrt(sym(11)/35)
a =
Create a symbolic variable x
using syms
. Create a symbolic function y(x)
that takes the variable x
as an input argument.
syms x
y(x) = a*x^2
y(x) =
Evaluate y
for x = sqrt(35)
.
y_eval = y(sqrt(35))
y_eval =
Variable-Precision Arithmetic
To specify the number of significant digits when performing calculations in Symbolic Math Toolbox with variable-precision arithmetic, you can use the digits
and vpa
functions.
For example, specify 10 significant digits and evaluate the previous symbolic values and functions to the specified precision.
digits(10) a_vpa = vpa(a)
a_vpa =
y_vpa(x) = vpa(y)
y_vpa(x) =
y_vpaeval = y_vpa(sqrt(35))
y_vpaeval =
Change Output Display Format
The results of symbolic computations in their analytical form can be long and complicated. To change the display format of symbolic results, you can use the sympref
function. This function sets the preferences used in symbolic computations.
For example, set the "FloatingPointOutput"
preference to true
. Then display the values and functions from the previous symbolic and variable-precision arithmetic examples. The output of all symbolic calculations are now in the short, fixed-decimal format with four digits after the decimal point.
sympref("FloatingPointOutput",true);
a
a =
a_vpa
a_vpa =
y
y(x) =
y_vpa
y_vpa(x) =
y_eval
y_eval =
y_vpaeval
y_vpaeval =
Setting the "FloatingPointOutput"
preference affects only the output display format and does not affect the floating-point precision in symbolic computations. For example, restore the default setting of the symbolic preference by using sympref("default")
. The variables y_eval
and y_vpaeval
still contain the exact symbolic arithmetic and 10-digit variable-precision arithmetic, respectively.
sympref("default");
y_eval
y_eval =
y_vpaeval
y_vpaeval =
Convert Symbolic Values to Numeric Values
You can also convert symbolic values to the default MATLAB double-precision values.
For example, you can use the double
function to convert symbolic numbers to double-precision numbers.
a_num = double(a)
a_num = 0.5606
You can use matlabFunction
to generate a MATLAB function that operates on the double
data type from an existing symbolic function.
y_num = matlabFunction(y)
y_num = function_handle with value:
@(x)(sqrt(1.1e+1).*sqrt(3.5e+1).*x.^2)./3.5e+1
Evaluate the generated function y_num
for x = sqrt(35)
.
y_numeval = y_num(sqrt(35))
y_numeval = 19.6214
Setting the "FloatingPointOutput"
preference using sympref
affects only the output display format of symbolic results. To change the output display format for MATLAB numeric results, use the format
function.