This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

sympref

Set symbolic preferences

Syntax

oldVal = sympref(pref,value)
oldVal = sympref(pref)
oldPrefs = sympref(prefs)
oldPrefs = sympref()

Description

example

oldVal = sympref(pref,value) sets the symbolic preference 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.

example

oldVal = sympref(pref) returns the current value of pref.

example

oldPrefs = sympref(prefs) sets multiple symbolic preferences to the values in the structure prefs and returns the previous values of all preferences to oldPrefs. You can set all symbolic preferences to their default values using sympref('default').

example

oldPrefs = sympref() returns the current values of all symbolic preferences.

Note

Symbolic preferences persist through successive MATLAB® sessions. Opening a new session does not restore the default preferences.

Examples

collapse all

The Fourier transform F(w) of f = f(t) is

F(w)=cf(t)eiswtdt,

where c and s are parameters with the default values 1 and –1, respectively. Other common values for c are 1/2π and 1/2π, 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 = 

cos(σ1)+log(σ1)+sin(σ1)+tan(σ1)+1σ1where  σ1=ax3+bx2+cx+d

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 = 

cos(ax3+bx2+cx+d)+log(ax3+bx2+cx+d)+sin(ax3+bx2+cx+d)+tan(ax3+bx2+cx+d)+1ax3+bx2+cx+d

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 = π+eab

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 for '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 = 

x2-2000xπ+12=0

Find the solutions of the equation using solve.

sols = solve(eq,x)
sols = 

(-22000000-π2-20002π22000000-π2+20002π)

Set the 'FloatingPointOutput' preference to true. Store the previous value of the preference in oldVal. Display the quadratic equation and its solutions in floating-point format.

oldVal = sympref('FloatingPointOutput',true);
eq
eq = x2-636.6198x+0.5000=0
sols
sols = 

(7.8540e-04636.6190)

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 previous value for 'FloatingPointOutput'. Compute the floating-point approximation of the solutions in 8 significant digits using vpa.

sympref('FloatingPointOutput',oldVal);
sols = vpa(sols,8)
sols = 

(0.00078539913636.61899)

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 = a2x+b2x2+y3+2

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 = 2+y3+a2x+b2x2

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 = b2x2+a2x+y3+2

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 = 

(xy24y2)

Display the matrix with square brackets by setting the 'MatrixWithSquareBrackets' preference to true.

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

[xy24y2]

The preferences you set using sympref persist through your current and future MATLAB sessions. Restore the default value by specifying the 'default' option.

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

Input Arguments

collapse all

Symbolic preference, specified as a character vector or string. The value options for each symbolic preference follow.

PreferenceValueDescription
'FourierParameters'

Two-element row vector [c,s]. The parameters c and s must be numeric or symbolic numbers.

Default: sym([1,-1]).

Set the values of the parameters c and s in the Fourier transform:

F(w)=cf(t)eiswtdt.

See Change Parameter Values of Fourier Transform.

'HeavisideAtOrigin'

Scalar value, specified as a numeric or symbolic number.

Default: sym(1/2).

Set the value of the Heaviside function heaviside(0) at the origin.

See Change Value of Heaviside Function at Origin.

'AbbreviateOutput'

Logical value (boolean).

Default: logical 1 (true).

Specify whether or not to use abbreviated output format of symbolic variables and expressions in Live Scripts.

See Modify Display of Symbolic Expressions in Live Scripts.

'TypesetOutput'

Logical value (boolean).

Default: logical 1 (true).

Typeset or use ASCII characters for the output of symbolic variables and expressions in Live Scripts.

See Modify Display of Symbolic Expressions in Live Scripts.

'FloatingPointOutput'

Logical value (boolean).

Default: logical 0 (false).

Specify whether or not to display symbolic results in floating-point output format.

The true value option displays symbolic results in the short fixed-decimal format with 4 digits after the decimal point.

See Display Symbolic Results in Floating-Point Format.

'PolynomialDisplayStyle'

Character vector or scalar string, specified as 'default', 'ascend', or 'descend'.

Default: 'default'.

Display a symbolic polynomial in default, ascending, or descending order.

  • The 'default' option sorts the output in alphabetical order, without distinguishing the different symbolic variables in each monomial term.

  • The 'ascend' option sorts the output in ascending order based on the standard mathematical notation for polynomials. For example, the variable x with the highest order in a monomial term is displayed last, preceded by monomial terms that contain the variables y, z, t, s, and so on.

  • The 'descend' option sorts the output in descending order based on the standard mathematical notation for polynomials. This option is the exact opposite of 'ascend'.

See Modify Output Order of Symbolic Polynomial.

'MatrixWithSquareBrackets'

Logical value (boolean).

Default: logical 0 (false).

Set matrices in round brackets or parentheses (round brackets) in Live Scripts.

See Modify Display of Symbolic Matrix in Live Scripts.

Value of symbolic preference, specified as 'default' or a valid value of the specified preference pref.

Symbolic preferences, specified as a structure array. You can set multiple preferences by declaring the field names and the valid preference values.

Output Arguments

collapse all

Value of symbolic preference, returned as a valid value. oldVal represents the existing value of the preference pref before the call to sympref.

All symbolic preferences, returned as a structure array. oldPrefs represent the existing values of all preferences before the call to sympref.

Tips

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

Introduced in R2015a