sym

Create symbolic variables, expressions, functions, matrices

sym('pi') now creates a symbolic variable named pi instead of a symbolic number representing the mathematical constant π. For more information, see Compatibility Considerations.

Support of character vectors that are not valid variable names and that do not define a number has been removed. To create symbolic expressions, first create symbolic variables, and then use operations on them. For example, use syms x; x + 1 instead of sym('x + 1'), exp(sym(pi)) instead of sym('exp(pi)'), and syms f(var1,...varN) instead of f(var1,...varN) = sym('f(var1,...varN)').

Description

example

x = sym('x') creates symbolic variable x.

example

A = sym('a',[n1 ... nM]) creates an n1-by-...-by-nM symbolic array filled with automatically generated elements. For example, A = sym('a',[1 3]) creates the row vector A = [a1 a2 a3]. The generated elements a1, a2, and a3 do not appear in the MATLAB® workspace. For multidimensional arrays, these elements have the prefix a followed by the element’s index using _ as a delimiter, such as a1_3_2.

example

A = sym('a',n) creates an n-by-n symbolic matrix filled with automatically generated elements.

example

sym(___,set) creates a symbolic variable or array and sets the assumption that the variable or all array elements belong to a set. Here, set can be 'real', 'positive', 'integer', or 'rational'. You also can combine multiple assumptions by specifying a string array or cell array of character vectors. For example, assume a positive rational value by specifying set as ["positive" "rational"] or {'positive','rational'}.

example

sym(___,'clear') clears assumptions set on a symbolic variable or array. You can specify 'clear' after the input arguments in any of the previous syntaxes, except combining 'clear' and set. You cannot set and clear an assumption in the same function call to sym.

example

sym(num) converts a number or numeric matrix specified by num to a symbolic number or symbolic matrix.

example

sym(num,flag) uses the technique specified by flag for converting floating-point numbers to symbolic numbers.

example

sym(strnum) converts the character vector or string specified by strnum to an accurate symbolic number that avoids any approximation.

example

symexpr = sym(h) creates a symbolic expression or matrix symexpr from an anonymous MATLAB function associated with the function handle h.

Examples

collapse all

Create the symbolic variables x and y.

x = sym('x')
x = xx
y = sym('y')
y = yy

Create a 1-by-4 symbolic vector a with automatically generated elements a1, ..., a4.

a = sym('a',[1 4])
 
a =
 
[a1, a2, a3, a4]
 

Format the names of elements of a by using a format character vector as the first argument. sym replaces %d in the format character vector with the index of the element to generate the element names.

a = sym('x_%d',[1 4])
 
a =
 
[x_1, x_2, x_3, x_4]
 

This syntax does not create symbolic variables x_1, ..., x_4 in the MATLAB workspace. Access elements of a using standard indexing methods.

a(1)
a(2:3)
 
ans =
 
x_1
 
 
ans =
 
[x_2, x_3]
 

Create a 3-by-4 symbolic matrix with automatically generated elements. The elements are of the form Ai_j, which generates the elements A1_1, ..., A3_4.

A = sym('A',[3 4])
 
A =
 
[A1_1, A1_2, A1_3, A1_4]
[A2_1, A2_2, A2_3, A2_4]
[A3_1, A3_2, A3_3, A3_4]
 

Create a 4-by-4 matrix with the element names x_1_1, ..., x_4_4 by using a format character vector as the first argument. sym replaces %d in the format character vector with the index of the element to generate the element names.

B = sym('x_%d_%d',4)
 
B =
 
[x_1_1, x_1_2, x_1_3, x_1_4]
[x_2_1, x_2_2, x_2_3, x_2_4]
[x_3_1, x_3_2, x_3_3, x_3_4]
[x_4_1, x_4_2, x_4_3, x_4_4]
 

This syntax does not create symbolic variables A1_1, ..., A3_4, x_1_1, ..., x_4_4 in the MATLAB workspace. To access an element of a matrix, use parentheses.

A(2,3)
B(4,2)
 
ans =
 
A2_3
 
 
ans =
 
x_4_2
 

Create a 2-by-2-by-2 symbolic array with automatically generated elements a1,1,1,,a2,2,2.

A = sym('a',[2 2 2])
A(:,:,1) = 

(a1,1,1a1,2,1a2,1,1a2,2,1)[a1_1_1, a1_2_1; a2_1_1, a2_2_1]

A(:,:,2) = 

(a1,1,2a1,2,2a2,1,2a2,2,2)[a1_1_2, a1_2_2; a2_1_2, a2_2_2]

Convert numeric values to symbolic numbers or expressions. Use sym on subexpressions instead of the entire expression for better accuracy. Using sym on entire expressions is inaccurate because MATLAB first converts the expression to a floating-point number, which loses accuracy. sym cannot always recover this lost accuracy.

inaccurate1 = sym(1/1234567)
inaccurate1 = 

76502392869235059444732965739290427392sym('7650239286923505/9444732965739290427392')

accurate1 = 1/sym(1234567)
accurate1 = 

11234567sym('1/1234567')

inaccurate2 = sym(sqrt(1234567))
inaccurate2 = 

48867165620185894398046511104sym('4886716562018589/4398046511104')

accurate2 = sqrt(sym(1234567))
accurate2 = 1234567sqrt(sym(1234567))
inaccurate3 = sym(exp(pi))
inaccurate3 = 

6513525919879993281474976710656sym('6513525919879993/281474976710656')

accurate3 = exp(sym(pi))
accurate3 = eπexp(sym(pi))

When creating symbolic numbers with 15 or more digits, use quotation marks to accurately represent the numbers.

inaccurateNum = sym(11111111111111111111)
inaccurateNum = 11111111111111110656sym('11111111111111110656')
accurateNum = sym('11111111111111111111')
accurateNum = 11111111111111111111sym('11111111111111111111')

When you use quotation marks to create symbolic complex numbers, specify the imaginary part of a number as 1i, 2i, and so on.

sym('1234567 + 1i')
ans = 1234567+i1234567 + sym(1i)

Create a symbolic expression and a symbolic matrix from anonymous functions associated with MATLAB handles.

h_expr = @(x)(sin(x) + cos(x));
sym_expr = sym(h_expr)
sym_expr = cos(x)+sin(x)cos(x) + sin(x)
h_matrix = @(x)(x*pascal(3));
sym_matrix = sym(h_matrix)
sym_matrix = 

(xxxx2x3xx3x6x)[x, x, x; x, 2*x, 3*x; x, 3*x, 6*x]

Create the symbolic variables x, y, z, and t while simultaneously assuming that x is real, y is positive, z rational, and t is positive integer.

x = sym('x','real');
y = sym('y','positive');
z = sym('z','rational');
t = sym('t',{'positive','integer'});

Check the assumptions on x, y, z, and t using assumptions.

assumptions
ans = (tZxRzQ1t0<y)[in(t, 'integer'), in(x, 'real'), in(z, 'rational'), 1 <= t, 0 < y]

For further computations, clear the assumptions using assume.

assume([x y z t],'clear')
assumptions
 
ans =
 
Empty sym: 1-by-0
 

Create a symbolic matrix and set assumptions on each element of that matrix.

A = sym('A%d%d',[2 2],'positive')
A = 

(A11A12A21A22)[A11, A12; A21, A22]

Solve an equation involving the first element of A. MATLAB assumes that this element is positive.

solve(A(1,1)^2-1, A(1,1))
ans = 1sym(1)

Check the assumptions set on the elements of A by using assumptions.

assumptions(A)
ans = (0<A110<A120<A210<A22)[0 < A11, 0 < A12, 0 < A21, 0 < A22]

Clear all previously set assumptions on elements of a symbolic matrix by using assume.

assume(A,'clear');
assumptions(A)
 
ans =
 
Empty sym: 1-by-0
 

Solve the same equation again.

solve(A(1,1)^2-1, A(1,1))
ans = 

(-11)[-sym(1); sym(1)]

Convert pi to a symbolic value.

Choose the conversion technique by specifying the optional second argument, which can be 'r', 'f', 'd', or 'e'. The default is 'r'. See the Input Arguments section for the details about conversion techniques.

r = sym(pi)
r = πsym(pi)
f = sym(pi,'f')
f = 

884279719003555281474976710656sym('884279719003555/281474976710656')

d = sym(pi,'d')
d = 3.1415926535897931159979634685442vpa('3.1415926535897931159979634685442')
e = sym(pi,'e')
e = 

π-198eps359sym(pi) - (198*eps)/359

Input Arguments

collapse all

Variable name, specified as a character vector. Argument x must be a valid variable name. That is, x must begin with a letter and can contain only alphanumeric characters and underscores. To verify that the name is a valid variable name, use isvarname.

Example: x, y123, z_1

Anonymous function, specified as a MATLAB function handle. For more information, see Anonymous Functions.

Example: h = @(x)sin(x); symexpr = sym(h)

Prefix for automatically generated matrix elements, specified as a character vector. Argument a must be a valid variable name. That is, a must begin with a letter and can contain only alphanumeric characters and underscores. To verify that the name is a valid variable name, use isvarname.

Example: a, b, a_bc

Vector, matrix, or array dimensions, specified as a vector of integers. As a shortcut, you can create a square matrix by specifying only one integer. For example, A = sym('A',3) creates a square 3-by-3 matrix.

Example: [2 3], [2,3], [2;3]

Assumptions on symbolic variable or matrix, specified as a character vector, string array, or cell array. The available assumptions are 'integer', 'rational', 'real', or 'positive'.

You can combine multiple assumptions by specifying a string array or cell array of character vectors. For example, assume a positive rational value by specifying set as ["positive" "rational"] or {'positive','rational'}.

Example: 'integer'

Numeric value to be converted to symbolic number or matrix, specified as a number, symbolic constant, or a matrix of numbers.

Example: 10, pi, catalan, hilb(3)

Conversion technique, specified as one of the characters listed in this table.

'r'When sym uses the rational mode, it converts floating-point numbers obtained by evaluating expressions of the form p/q, p*pi/q, sqrt(p), 2^q, and 10^q (for modest sized integers p and q) to the corresponding symbolic form. For example, sym(1/10,'r') returns 1/10. This effectively compensates for the round-off error involved in the original evaluation, but might not represent the floating-point value precisely. If sym cannot find simple rational approximation, then it uses the same technique as it would use with the flag 'f'.
'd'When sym uses the decimal mode, it takes the number of digits from the current setting of digits. Conversions with fewer than 16 digits lose some accuracy, while more than 16 digits might not be warranted. For example, sym(4/3,'d') with the 10-digit accuracy returns 1.333333333, while with the 20-digit accuracy it returns 1.3333333333333332593. The latter does not end in 3s, but it is an accurate decimal representation of the floating-point number nearest to 4/3.
'e'When sym uses the estimate error mode, it supplements a result obtained in the rational mode by a term involving the variable eps. This term estimates the difference between the theoretical rational expression and its actual floating-point value. For example, sym(3*pi/4,'e') returns (3*pi)/4 - (103*eps)/249.
'f'When sym uses the floating-point to rational mode, it returns the symbolic form for all values in the form N*2^e or -N*2^e, where N >= 0 and e are integers. The returned symbolic number is a precise rational number that is equal to the floating-point value. For example, sym(1/10,'f') returns 3602879701896397/36028797018963968.

Characters representing symbolic number, specified as a character vector or string.

Example: '1/10', '12/34'

Output Arguments

collapse all

Variable, returned as a symbolic variable.

Vector or matrix with automatically generated elements, returned as a symbolic vector or matrix. The elements of this vector or matrix do not appear in the MATLAB workspace.

Expression or matrix generated from an anonymous MATLAB function, returned as a symbolic expression or matrix.

Tips

  • Statements like pi = sym(pi) and delta = sym('1/10') create symbolic numbers that avoid the floating-point approximations inherent in the values of pi and 1/10. The pi created in this way stores the symbolic number in a workspace variable named pi, which temporarily replaces the built-in numeric function with the same name. Use clear pi to restore the floating-point representation of pi.

  • sym always treats i in character vector input as an identifier. To input the imaginary number i, use 1i instead.

  • clear x does not clear the symbolic object of its assumptions, such as real, positive, or any assumptions set by assume, sym, or syms. To remove assumptions, use one of these options:

    • assume(x,'clear') removes all assumptions affecting x.

    • clear all clears all objects in the MATLAB workspace and resets the symbolic engine.

    • assume and assumeAlso provide more flexibility for setting assumptions on variable.

  • When you replace one or more elements of a numeric vector or matrix with a symbolic number, MATLAB converts that number to a double-precision number.

    A = eye(3);
    A(1,1) = sym(pi)
    A =
        3.1416         0         0
             0    1.0000         0
             0         0    1.0000

    You cannot replace elements of a numeric vector or matrix with a symbolic variable, expression, or function because these elements cannot be converted to double-precision numbers. For example, A(1,1) = sym('a') throws an error.

  • When you use the syntax A = sym('a',[n1 ... nM]), the sym function assigns only the symbolic array A to the MATLAB workspace. To also assign the automatically generated elements of A, use the syms function instead. For example, syms a [1 3] creates the row vector a = [a1 a2 a3] and the symbolic variables a1, a2, and a3 in the MATLAB workspace.

Alternative Functionality

Alternative Approaches for Creating Symbolic Variables

To create several symbolic variables in one function call, use syms. Using syms also clears assumptions from the named variables.

Compatibility Considerations

expand all

Behavior changed in R2020a

Errors starting in R2018a

Introduced before R2006a
Для просмотра документации необходимо авторизоваться на сайте