创建符号数字,变量和表达式

此页面显示如何创建符号编号,变量和表达式。学习如何使用符号数学,参见Perform Symbolic Computations

Create Symbolic Numbers

您可以通过使用创建符号编号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

Create Symbolic Variables

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.

Thesymscommand is shorthand for thesymsyntax, but the two functions handle assumptions differently. For more details, see重用符号对象的名称

创建符号变量xyusingsymssym, respectively.

syms x y = sym('y')

The first command creates a symbolic variablex在matlab工作区,具有值xassigned to the variablex。第二个命令创建一个符号变量ywith 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, thesymssyntax 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

Ais a1-通过-20数组为20个符号变量。

By combiningsymsyms, 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变量,该变量是符号变量。

Thesymscommand is a convenient shorthand for thesymsyntax, and its typical use is to create fresh symbolic variables for interactive symbolic workflows. Use thesymsyntax 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

Create Symbolic Expressions

Suppose you want to use a symbolic variable to represent the golden ratio

φ. = 1 + 5 2

命令

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=ax2+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;

Tip

To create a symbolic number, use thesymcommand. Do not use thesymsfunction to create a symbolic expression that is a constant. For example, to create the expression whose value is5, enterf =符号(5)。命令F = 5.doesnot界定fas a symbolic expression.

重用符号对象的名称

如果设置一个等于符号表达式的变量,然后应用symscommand 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 usingsymdoes not clear its assumptions. For more information, seeDelete Symbolic Objects and Their Assumptions

相关话题