Set symbolic preferences
sets the symbolic preference oldVal
= sympref(pref
,value
)pref
to
value
and returns the previous value of the preference
to oldVal
. You can set the preference to its default value
using sympref(pref,'default')
.
Symbolic preferences can affect the computation of the symbolic functions
fourier
, ifourier
, and
heaviside
, and the display format of symbolic
output.
The Fourier transform F(w) of f = f(t) is
where c and s are parameters with
the default values 1 and –1, respectively. Other common values for
c are 1/2π and , and other common values for s
are 1,
–2π, and 2π.
Find the Fourier transform of sin(t)
with default
c
and s
parameters.
syms t w F = fourier(sin(t),t,w)
F = -pi*(dirac(w - 1) - dirac(w + 1))*1i
Find the same Fourier transform with c =
1/(2
π)
and s = 1
. Set the parameter values by using the
'FourierParameters'
preference. Represent
π exactly by using sym
. Specify
the values of c
and s
as the vector
[1/(2*sym(pi)) 1]
. Store the previous values returned
by sympref
so that you can restore them later.
oldVal = sympref('FourierParameters',[1/(2*sym(pi)) 1]) F = fourier(sin(t),t,w)
oldVal = [ 1, -1] F = (dirac(w - 1)*1i)/2 - (dirac(w + 1)*1i)/2
The preferences you set using sympref
persist through
your current and future MATLAB sessions. Restore the previous values of c
and s
to oldVal
.
sympref('FourierParameters',oldVal);
Alternatively, you can restore the default values of c
and s
by specifying the 'default'
option.
sympref('FourierParameters','default');
In Symbolic Math Toolbox™, the default value of the Heaviside function at the origin is 1/2.
Return the value of heaviside(0)
. Find the Z-transform of
heaviside(x)
for this default value of
heaviside(0)
.
syms x H = heaviside(sym(0)) Z = ztrans(heaviside(x))
H = 1/2 Z = 1/(z - 1) + 1/2
Other common values for the Heaviside function at the origin are 0 and 1.
Set heaviside(0)
to 1
using the
'HeavisideAtOrigin'
preference. Store the previous
value returned by sympref
so that you can restore it
later.
oldVal = sympref('HeavisideAtOrigin',1)
oldVal = 1/2
Check if the new value of heaviside(0)
is 1. Find the
Z-transform of heaviside(x)
for this value.
H = heaviside(sym(0)) Z = ztrans(heaviside(x))
H = 1 Z = 1/(z - 1) + 1
The new output of heaviside(0)
modifies the output of
ztrans
.
The preferences you set using sympref
persist through
your current and future MATLAB sessions. Restore the previous value of
heaviside(0)
to oldVal
.
sympref('HeavisideAtOrigin',oldVal);
Alternatively, you can restore the default value of
'HeavisideAtOrigin'
by specifying the
'default'
option.
sympref('HeavisideAtOrigin','default');
By default, symbolic expressions in live scripts are displayed in abbreviated output format, and typeset in mathematical notation. You can turn off abbreviated output format and typesetting using symbolic preferences.
Create a symbolic expression and return the output, which is abbreviated by default.
syms a b c d x f = a*x^3 + b*x^2 + c*x + d; outputAbbrev = sin(f) + cos(f) + tan(f) + log(f) + 1/f
outputAbbrev =
Turn off abbreviated output format by setting the 'AbbreviateOutput'
preference to false
. Redisplay the expression.
sympref('AbbreviateOutput',false);
outputLong = sin(f) + cos(f) + tan(f) + log(f) + 1/f
outputLong =
Create another symbolic expression and return the output, which is typeset in mathematical notation by default. Turn off rendered output and use ASCII output instead by setting the 'TypesetOutput'
preference to false
. First, show the typeset output.
syms a b c d x f = exp(a^b)+pi
f =
Turn off typesetting by setting the 'TypesetOutput'
preference to false
. Redisplay the expression.
sympref('TypesetOutput',false);
f = exp(a^b)+pi
f = pi + exp(a^b)
The preferences you set using sympref
persist through your current and future MATLAB sessions. Restore the default values of 'AbbreviateOutput'
and 'TypesetOutput'
by specifying the 'default'
option.
sympref('AbbreviateOutput','default'); sympref('TypesetOutput','default');
Display symbolic results in floating-point output format, that is the short, fixed-decimal format with 4 digits after the decimal point.
Create a quadratic equation.
syms x
eq = x^2 - 2e3/sym(pi)*x + 0.5 == 0
eq =
Find the solutions of the equation using solve
.
sols = solve(eq,x)
sols =
Set the 'FloatingPointOutput'
preference to true
. Display the quadratic equation and its solutions in floating-point format.
sympref('FloatingPointOutput',true);
eq
eq =
sols
sols =
The floating-point format displays each symbolic number in the short, fixed-decimal format with 4 digits after the decimal point. Setting the 'FloatingPointOutput'
preference does not affect the floating-point precision in a symbolic computation. To compute symbolic numbers using floating-point arithmetic, use the vpa
function.
Now restore the default value of 'FloatingPointOutput'
by specifying the 'default'
option. Compute the floating-point approximation of the solutions in 8 significant digits using vpa
.
sympref('FloatingPointOutput','default'); sols = vpa(sols,8)
sols =
Create a symbolic polynomial expression consisting of multiple variables. Display the polynomial in the default order.
syms x y a b p1 = b^2*x^2 + a^2*x + y^3 + 2
p1 =
The default option sorts the output in alphabetical order, without distinguishing the different symbolic variables in each monomial term.
Now display the same polynomial in ascending order by setting the preference 'PolynomialDisplayStyle'
to 'ascend'
.
sympref('PolynomialDisplayStyle','ascend'); p1
p1 =
The 'ascend'
option sorts the output in ascending order based on the importance of the variables. Here, the most important variable x
with the highest order in a monomial term is displayed last.
Display the polynomial in descending order by setting the 'PolynomialDisplayStyle'
preference to 'descend'
.
sympref('PolynomialDisplayStyle','descend'); p1
p1 =
The preferences you set using sympref
persist through your current and future MATLAB sessions. Restore the default value of 'PolynomialDisplayStyle'
by specifying the 'default'
option.
sympref('PolynomialDisplayStyle','default');
By default, a symbolic matrix in live scripts is set in parentheses (round brackets). You can specify the use of square brackets instead by using sympref
.
Create a symbolic matrix consisting of symbolic variables and numbers.
syms x y A = [x*y, 2; 4, y^2]
A =
Display the matrix with square brackets by setting the 'MatrixWithSquareBrackets'
preference to true
.
sympref('MatrixWithSquareBrackets',true);
A
A =
The preferences you set using sympref
persist through your current and future MATLAB sessions. Restore the default value by specifying the 'default'
option.
sympref('MatrixWithSquareBrackets','default');
Instead of saving and restoring individual preferences one by one, you can
use sympref
to save and restore all symbolic preferences
simultaneously.
Return a structure containing the values of all symbolic preferences by
using sympref()
.
oldPrefs = sympref()
oldPrefs = struct with fields: FourierParameters: [1×2 sym] HeavisideAtOrigin: [1×1 sym] AbbreviateOutput: 1 TypesetOutput: 1 FloatingPointOutput: 0 PolynomialDisplayStyle: 'default' MatrixWithSquareBrackets: 0
Access the value of each symbolic preference by addressing the field of
the structure. Alternatively, you can use the command
sympref(pref)
.
val1 = oldPrefs.FourierParameters val2 = oldPrefs.HeavisideAtOrigin val3 = sympref('FourierParameters')
val1 = [ 1, -1] val2 = 1/2 val3 = [ 1, -1]
To modify multiple symbolic preferences simultaneously, you can create a
structure prefs
that contains the preference values. Use
the command sympref(prefs)
to set multiple
preferences.
prefs.FourierParameters = [1/(2*sym(pi) 1] prefs.HeavisideAtOrigin = 1 sympref(prefs);
Because symbolic preferences persist through your current and future
MATLAB sessions, you need to restore your previous preferences.
Restore the saved preferences using
sympref(oldPrefs)
.
sympref(oldPrefs);
Alternatively, you can set all symbolic preferences to their default
values by specifying the 'default'
option.
sympref('default');
pref
— Symbolic preferenceSymbolic preference, specified as a character vector or string. The value options for each symbolic preference follow.
Preference | Value | Description |
---|---|---|
'FourierParameters' | Two-element row vector Default:
| Set the values of the parameters c and s in the Fourier transform:
|
'HeavisideAtOrigin' | Scalar value, specified as a numeric or symbolic number. Default:
| Set the value of the Heaviside function
|
'AbbreviateOutput' | Logical value (boolean). Default:
logical | Specify whether or not to use abbreviated output format of symbolic variables and expressions in Live Scripts. |
'TypesetOutput' | Logical value (boolean). Default:
logical | Typeset or use ASCII characters for the output of symbolic variables and expressions in Live Scripts. |
'FloatingPointOutput' | Logical value (boolean). Default:
logical | Specify whether or not to display symbolic results in floating-point output format. The
|
'PolynomialDisplayStyle' | Character vector or scalar string, specified as
Default:
| Display a symbolic polynomial in default, ascending, or descending order.
|
'MatrixWithSquareBrackets' | Logical value (boolean). Default:
logical | Set matrices in round brackets or parentheses (round brackets) in Live Scripts. |
value
— Value of symbolic preference'default'
(default) | valid valueValue of symbolic preference, specified as 'default'
or
a valid value of the specified preference pref
.
prefs
— Symbolic preferencesSymbolic preferences, specified as a structure array. You can set multiple preferences by declaring the field names and the valid preference values.
oldVal
— Value of symbolic preferenceValue of symbolic preference, returned as a valid value.
oldVal
represents the existing value of the
preference pref
before the call to
sympref
.
oldPrefs
— All symbolic preferencesAll symbolic preferences, returned as a structure array.
oldPrefs
represent the existing values of all
preferences before the call to sympref
.
The clear
command does not reset or
affect symbolic preferences. Use sympref
to manipulate
symbolic preferences.
The symbolic preferences you set using sympref
also
determine the output generated by the latex
and mathml
functions.
Setting the 'FloatingPointOutput'
preference affects only
the output display format of symbolic numbers. To change the output display
format of numeric numbers, use the format
function. To compute
symbolic numbers using floating-point precision, use the vpa
or digits
functions.
You have a modified version of this example. Do you want to open this example with your edits?