Main Content

sym

Create symbolic variables, expressions, functions, matrices

金宝app支持无效变量名称且未定义数字的字符矢量的支持。要创建符号表达式,请首先创建符号变量,然后在其上使用操作。例如,使用syms x; x + 1代替sym('x + 1'),,,,eXp((SYM(PI))代替SYM('Exp(pi)'),,,,一个ndsyms f(var1,...varN)代替f(var1,...varN) = sym('f(var1,...varN)')

Description

例子

X= sym('<一个Href="//www.tatmou.com/au/help/symbolic/#bun735q-x" class="intrnllnk">X')创建符号变量X

例子

一个= sym('<一个Href="//www.tatmou.com/au/help/symbolic/#bun735q-a" class="intrnllnk">一个',,<一个Href="//www.tatmou.com/au/help/symbolic/#bun735q-n1nM" class="intrnllnk">[n1 ... nM]创建一个n1-by-。。。-by-nMsymbolic array filled with automatically generated elements. For example,a = sym('a',[1 3])creates the row vectora =[a1 a2 a3]。The generated elements一个1,,,,一个2,,,,一个nd一个3do not appear in the MATLAB®workspace. For multidimensional arrays, these elements have the prefix一个followed by the element’s index using_作为定界符,例如一个1_3_2

例子

一个= sym('<一个Href="//www.tatmou.com/au/help/symbolic/#bun735q-a" class="intrnllnk">一个',,n)创建一个n-by-n符号矩阵充满了自动生成的元素。

例子

sym(___,,,,<一个Href="//www.tatmou.com/au/help/symbolic/#bun735q-set" class="intrnllnk">创建一个符号变量或数组,并设置一个假设,即变量或所有数组元素属于一个。Here,'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作为["positive" "rational"]or{'positive','rational'}

例子

sym(___,'清除')clears assumptions set on a symbolic variable or array. You can specify'清除'一个fter the input arguments in any of the previous syntaxes, except combining'清除'一个nd<一个Href="//www.tatmou.com/au/help/symbolic/#bun735q-set" class="intrnllnk">。You cannot set and clear an assumption in the same function call tosym

例子

sym(<一个Href="//www.tatmou.com/au/help/symbolic/#bun735q-num" class="intrnllnk">num转换一个数字或数字矩阵num对于符号数或符号矩阵。

例子

sym(<一个Href="//www.tatmou.com/au/help/symbolic/#bun735q-num" class="intrnllnk">num,,,,<一个Href="//www.tatmou.com/au/help/symbolic/#bun735q-flag" class="intrnllnk">flaguses the technique specified byflag用于将浮点数转换为符号数字。

例子

sym(<一个Href="//www.tatmou.com/au/help/symbolic/#mw_b4228c40-a002-49a8-a6cc-1bec4af17ca8" class="intrnllnk">strnumconverts the character vector or string specified bystrnumto an accurate symbolic number that avoids any approximation.

例子

symexpr= sym(<一个Href="//www.tatmou.com/au/help/symbolic/#bun735q-h" class="intrnllnk">Hcreates a symbolic expression or matrixsymexprfrom an anonymous MATLAB function associated with the function handleH

Examples

collapse all

Create the symbolic variablesX一个ndy

x = sym('x'
X=
                       
                        
                         
                          X
                        
                       
y= sym('y'
y=
                       
                        
                         
                          y
                        
                       

创建1 x-4符号向量一个with automatically generated elements一个1,,,,。。。,,,,一个4

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

格式化元素的名称一个byusing a format character vector as the first argument.symreplaces%d一世ntHeformat character vector with the index of the element to generate the element names.

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

This syntax does not create symbolic variablesX_1,,,,。。。,,,,X_4一世ntHeMATLAB® workspace. Access elements of一个使用标准索引方法。

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

创建具有自动生成元素的3 x-4符号矩阵。元素是形式的一个一世_j,,,,which generates the elements一个1_1,,,,。。。,,,,一个3_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_2,a3_3,a3_4]

Create a 4-by-4 matrix with the element namesX_1_1,,,,。。。,,,,X_4_4byusing a format character vector as the first argument.symreplaces%d一世ntHeformat 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_2,x_2_3,x_2_4] [x_3_1,x_3_1,x_3_2,x_3_3,x_3_3,x_3_3,x_3_4]

This syntax does not create symbolic variables一个1_1,,,,。。。,,,,一个3_4,,,,X_1_1,,,,。。。,,,,X_4_4一世ntHeMATLAB® workspace. To access an element of a matrix, use parentheses.

一个((2,,,,3)B(4,2)
ans =一个2_3ans =X_4_2

创建一个具有自动生成元素的2 by-2by-2符号阵列 一个 1 ,,,, 1 ,,,, 1 ,,,, ,,,, 一个 2 ,,,, 2 ,,,, 2

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

(( 一个 1 ,,,, 1 ,,,, 1 一个 1 ,,,, 2 ,,,, 1 一个 2 ,,,, 1 ,,,, 1 一个 2 ,,,, 2 ,,,, 1

一个((:,:,2) =

(( 一个 1 ,,,, 1 ,,,, 2 一个 1 ,,,, 2 ,,,, 2 一个 2 ,,,, 1 ,,,, 2 一个 2 ,,,, 2 ,,,, 2

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

一世n一个ccurate1 = sym(1/1234567)
一世n一个ccurate1 =

7650239286923505 9444732965739290427392

一个ccurate1 = 1/sym(1234567)
一个ccurate1 =

1 1234567

一世n一个ccurate2 = sym(sqrt(1234567))
一世n一个ccurate2 =

4886716562018589 4398046511104

一个ccurate2 = sqrt(sym(1234567))
一个ccurate2 =
                       
                        
                         
                          
                           
                            1234567
                          
                         
                        
                       
一世n一个ccurate3 = sym(exp(pi))
一世n一个ccurate3 =

6513525919879993 281474976710656

准确3 = EXP(SYS(pi))
一个ccurate3 =
                       
                        
                         
                          
                           
                            e
                          
                          
                           
                            π
                          
                         
                        
                       

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

一世n准确= sym(sym(11111111111111111111)
一世n精确量=
                       
                        
                         
                          11111111111111110656
                        
                       
准确= sym(sym('11111111111111111111'
精确量=
                       
                        
                         
                          11111111111111111111
                        
                       

When you use quotation marks to create symbolic complex numbers, specify the imaginary part of a number as1一世,,,,2一世,,,,一个nd so on.

sym('1234567 + 1i'
ans =
                       
                        
                         
                          
                           
                            1234567
                           
                            +
                           
                            一世
                          
                         
                        
                       

将与MATLAB®手柄关联的匿名函数转换为符号表达式和符号矩阵。

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

(( X X X X 2 X 3 X X 3 X 6 X

Create the symbolic variablesX,,,,y,,,,z,,,,一个ndtwhile simultaneously assuming thatX是真实的,y是积极的,z合理的,,,,一个ndt一世s positive integer.

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

Check the assumptions onX,,,,y,,,,z,,,,一个ndtusing作为sumptions

作为sumptions
ans =
                       
                        
                         
                          
                           ((
                          
                           
                            
                             
                              
                               
                                
                                 
                                  t
                                
                                
                                
                                 
                                  z
                                
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 
                                  X
                                
                                
                                
                                 
                                  r
                                
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 
                                  z
                                
                                
                                
                                 
                                
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 1
                                
                                
                                 t
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 0
                                
                                 <
                                
                                 y
                               
                              
                             
                            
                           
                          
                          
                         
                        
                       

For further computations, clear the assumptions using作为sume

作为sume([x y z t],'清除')作为sumptions
ans =空符号: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 =

(( 一个 11 一个 12 一个 21 一个 22

Solve an equation involving the first element of一个。MATLAB® assumes that this element is positive.

解决((1,1)^ 2 - 1,(1,1))
ans =
                       
                        
                         
                          1
                        
                       

Check the assumptions set on the elements of一个byusing作为sumptions

作为sumptions(A)
ans =
                       
                        
                         
                          
                           ((
                          
                           
                            
                             
                              
                               
                                
                                 0
                                
                                 <
                                
                                 
                                  
                                   一个
                                 
                                 
                                  
                                   11
                                 
                                
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 0
                                
                                 <
                                
                                 
                                  
                                   一个
                                 
                                 
                                  
                                   12
                                 
                                
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 0
                                
                                 <
                                
                                 
                                  
                                   一个
                                 
                                 
                                  
                                   21
                                 
                                
                               
                              
                             
                            
                            
                             
                              
                               
                                
                                 0
                                
                                 <
                                
                                 
                                  
                                   一个
                                 
                                 
                                  
                                   22
                                 
                                
                               
                              
                             
                            
                           
                          
                          
                         
                        
                       

Clear all previously set assumptions on elements of a symbolic matrix by using作为sume

作为sume(A,'清除'); assumptions(A)
ans =空符号:1-by-0

Solve the same equation again.

解决((1,1)^ 2 - 1,(1,1))
ans =

(( - 1 1

Convertp一世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'。有关转换技术的详细信息,请参见输入参数部分。

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

884279719003555 281474976710656

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

π - 198 eps 359

输入参数

collapse all

Variable name, specified as a character vector. ArgumentXmust be a valid variable name. That is,Xmust begin with a letter and can contain only alphanumeric characters and underscores. To verify that the name is a valid variable name, use<一个Href="https://au.mathworks.com/help/matlab/ref/isvarname.html">一世svarname

Example:X,,,,y123,,,,z_1

匿名函数,,,,specified as a MATLAB function handle. For more information, see<一个Href="https://au.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html" class="a">匿名函数。

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

自动生成的矩阵元素的前缀,指定为字符向量。争论一个must be a valid variable name. That is,一个must begin with a letter and can contain only alphanumeric characters and underscores. To verify that the name is a valid variable name, use<一个Href="https://au.mathworks.com/help/matlab/ref/isvarname.html">一世svarname

Example:一个,,,,b,,,,A_BC

向量,矩阵或数组尺寸,指定为整数的向量。作为快捷方式,您可以通过仅指定一个整数来创建一个方形矩阵。例如,a =sym('A',3)creates a square3-by-3matrix.

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

对符号变量或矩阵的假设,指定为字符向量,字符串数组或单元格数组。可用的假设是'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作为["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,,,,p一世,,,,catalan,,,,希尔布(3)

转换技术,,,,specified as one of the characters listed in this table.

'r' Whensymuses the合理的模式,它通过评估表格的表达方式转换了获得的浮点数p/q,,,,p*pi/q,,,,sqrt(p),,,,2^q,,,,一个nd10^q((for modest sized integersp一个nd)到相应的年代ymbolic form. For example,sym(1/10,'r')返回1/10。这有效地补偿了原始评估中涉及的圆形错误,但可能不能精确地代表浮点值。如果symcannot find simple rational approximation, then it uses the same technique as it would use with the flag'f'
'd' Whensymuses the小数模式,它从当前设置的数字数量数字。少于16位的转换失去了一些精度,而可能不保证超过16位数字。例如,sym(4/3,'d')with the 10-digit accuracy returns1。333333333,,,,while with the 20-digit accuracy it returns1。3333333333333332593。The latter does not end in3的年代,但它是一个精确的十进制表示tHefloating-point number nearest to4/3
'e' Whensymuses theestimate error模式,它通过涉及变量的术语来补充以有理模式获得的结果eps。This term estimates the difference between the theoretical rational expression and its actual floating-point value. For example,sym(3*pi/4,'e')返回((3*pi)/4 - (103*eps)/249
'f' Whensymuses thefloating-point to rationalmode, it returns the symbolic form for all values in the formn*2^eor-n*2^e,,,,whereN >= 0一个nde一个re integers. The returned symbolic number is a precise rational number that is equal to the floating-point value. For example,sym(1/10,'f')返回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 likepi = sym(pi)一个nddelta = sym('1/10')创建符号数字,以避免浮点近似值p一世一个nd1/10。Thep一世以这种方式创建的符号编号在命名的工作区变量p一世,,,,which temporarily replaces the built-in numeric function with the same name. Useclear pi恢复浮点的表示p一世

  • sym一个lways treats一世在字符矢量输入中作为标识符。输入虚构数字一世,,,,use1一世反而。

  • 清除xdoes not clear the symbolic object of its assumptions, such as real, positive, or any assumptions set by作为sume,,,,sym,,,,orsyms。要删除假设,请使用以下选项之一:

    • 作为sume(x,'clear')删除所有影响的假设X

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

    • 作为sume一个nd作为sumeAlsoprovide more flexibility for setting assumptions on variable.

  • 当您用符号编号替换数字向量或矩阵的一个或多个元素时,MATLAB将该数字转换为双精度编号。

    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 syntaxa =sym('a',[n1 ... nM]), 这symfunction assigns only the symbolic array一个to the MATLAB workspace. To also assign the automatically generated elements of一个, 使用<一个Href="//www.tatmou.com/au/help/symbolic/syms.html">syms功能。例如,SYMS A [1 3]creates the row vector一个= [a1 a2 a3]一个nd the symbolic variables一个1,,,,一个2,,,,一个nd一个3一世ntHeMATLAB workspace.

一个lternative Functionality

创建符号变量的替代方法

To create several symbolic variables in one function call, usesyms。Usingsyms还清除了命名变量的假设。

版本历史记录

Introduced before R2006a

eXp一个nd all

Behavior changed in R2020a

Errors starting in R2018a