此页面显示如何创建符号编号,变量和表达式。学习如何使用符号数学,参见Perform Symbolic Computations。
您可以通过使用创建符号编号sym
。Symbolic numbers are exact representations, unlike floating-point numbers.
Create a symbolic number by usingsym
并将其与相同的浮点数进行比较。
sym(1/3)1/3
ans = 1/3 ans = 0.3333
The symbolic number is represented in exact rational form, while the floating-point number is a decimal approximation. The symbolic result is not indented, while the standard MATLAB®result is indented.
Calculations on symbolic numbers are exact. Demonstrate this exactness by finding罪(PI)
象征性地和数字。符号结果精确,而数字结果是近似值。
SIN(SYM(PI))SIN(PI)
ans = 0 ans = 1.2246e-16
To learn more about symbolic representation of numbers, seeNumeric to Symbolic Conversion。
You can create symbolic variables using eithersyms
要么sym
。Typical uses of these functions include:
sym
- 创建编号的符号变量或在MATLAB函数中创建符号变量。
syms
– Createfreshsymbolic variables for interactive symbolic workflows, that is, for symbolic variable creation at the MATLAB command line or in MATLAB live scripts. Afreshsymbolic variable does not have any assumptions.
Thesyms
command is shorthand for thesym
syntax, but the two functions handle assumptions differently. For more details, see重用符号对象的名称。
创建符号变量x
和y
usingsyms
和sym
, respectively.
syms x y = sym('y')
The first command creates a symbolic variablex
在matlab工作区,具有值x
assigned to the variablex
。第二个命令创建一个符号变量y
with the valuey
。
Withsyms
, you can create multiple variables in one command. Create the variablesa
,b
,和c
。
syms a b c
If you want to create a MATLAB array of numbered symbolic variables, thesyms
syntax is inconvenient. Therefore, usesym
而是要创建一个数组的许多编号符号变量。
清除工作区。创建包含符号变量的行向量a1, . .。, a20
并将其分配给matlab变量A
。在MATLAB工作区中显示变量。
明确所有=符号('a',[1 20])谁
A = [A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,... A11,A12,A13,A14,A15,A16,A17,A18,A19,A20]名称大小字节类属性1x20 8 sym
A
is a1
-通过-20
数组为20个符号变量。
By combiningsym
和syms
, you can create many fresh symbolic variables with corresponding variables name in the MATLAB workspace.
清除工作区。Create the fresh symbolic variablesa1, . .。, a10
和assign them the MATLAB variable namesa1, . .。, a10
, respectively. Display the variables in the MATLAB workspace.
明确所有Syms(Sym('a',[1 10]))谁
名称大小字节类属性A1 1x1 8 SYM A10 1x1 8 SYM A2 1x1 8 SYM A SYM A3 1X1 8 SYM A4 1X1 8 SYM A5 1x1 8 SYM A6 1X1 8 SYM A7 1x1 8 SYM A8 1x1 8 SYM A9 1X1 8 SYM
MATLAB工作区包含10个MATLAB变量,该变量是符号变量。
Thesyms
command is a convenient shorthand for thesym
syntax, and its typical use is to create fresh symbolic variables for interactive symbolic workflows. Use thesym
syntax to create the following:
Symbolic variables in MATLAB functions
Many numbered symbolic variables
Symbolic variable whose value differs from its name in the MATLAB workspace
Symbolic number, such as符号(5)
Symbolic variable that inherits the assumptions from a previously used symbolic variable having the same name
Suppose you want to use a symbolic variable to represent the golden ratio
命令
PHI =(1 + SQRT(SYM(5)))/ 2;
achieves this goal. Now you can perform various mathematical operations on披
。For example,
f = phi ^ 2 - phi - 1
returns
f = (5^(1/2)/2 + 1/2)^2 - 5^(1/2)/2 - 3/2
Now suppose you want to study the quadratic functionf
=ax
2+bx
+c
。First, create the symbolic variablesa
,b
,c
,和x
:
syms a b c x
Then, assign the expression tof
:
f = a * x ^ 2 + b * x + c;
To create a symbolic number, use thesym
command. Do not use thesyms
function to create a symbolic expression that is a constant. For example, to create the expression whose value is5
, enterf =符号(5)
。命令F = 5.
doesnot界定f
as a symbolic expression.
如果设置一个等于符号表达式的变量,然后应用syms
command to the variable, MATLAB software removes the previously defined expression from the variable. For example,
syms a b f = a + b
returns
f = a + b
If later you enter
syms f f
然后matlab删除了值a + b
从表达式f
:
f = f
You can use thesyms
命令以清除先前分配给Matlab会话中的定义的变量。syms
明确s the assumptions of the variables: complex, real, integer, and positive. These assumptions are stored separately from the symbolic object. However, recreating a variable usingsym
does not clear its assumptions. For more information, seeDelete Symbolic Objects and Their Assumptions。