当前位置: 首页 > 工具软件 > Symbolic > 使用案例 >

MatLab中符号对象的数据类型是,符号对象(Symbolic Object)的使用

从光启
2023-12-01

MatLab中符号对象的数据类型是,符号对象(Symbolic Object)的使用

符号对象是什么?符号对象是Matlab中一种特殊的数据类型, 其实质是“a data structure that stores a string representation of the symbol”, 即它存储的是字符串表示的符号表达式, 只是这些字符串对应的是数学运算法则。如果用whos 命令查询, 符号变量对应的类型显示的是“sym“, 这也是区分符号变量与数值变量的方法。操作这些对象的步骤, 先是Matlab利用Toolbox中的m文件,传递参数到Maple核心, Maple核心经过运算后将结果再传递回Matlab, 再由M文件将结果解释成为Matlab的通用格式。 如果只需要执行基本的符合运算, 那么只要掌握如何使用Matlab中有关symbolic运算的基本命令就可以了, 而要做复杂的工作, 建议学习下Maple。

1 创建符号计算对象

该工具箱包含的运算对象为以下5个,可使用多种方法进行创建,核心函数为symsyms.

  • Symbolic Numbers
  • Symbolic Variables
  • Symbolic Expressions
  • Symbolic Functions
  • Symbolic Matrices

1.1 Symbolic Numbers

>> sym(1/3)
>> ans =
1/3

>> 1/3
ans =
    0.3333

1.2 Symbolic Variables

>> syms x;
>> y = sym('y'); 
>> whos
  Name      Size            Bytes  Class    Attributes

  x         1x1                 8  sym                
  y         1x1                 8  sym
>> A = sym('a', [1 20])

A =

[ a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20]

>> whos
  Name      Size            Bytes  Class    Attributes

  A         1x20                8  sym
>> syms(sym('a', [1 10]))
>> whos
  Name      Size            Bytes  Class    Attributes

  a1        1x1                 8  sym                
  a10       1x1                 8  sym                
  ...etc...
>> sym('a_%d',[1,10])

ans =

[ a_1, a_2, a_3, a_4, a_5, a_6, a_7, a_8, a_9, a_10]

1.3 Symbolic Expressions

For ϕ = 1 + 5 2 \phi = \frac{1+\sqrt{5}}{2} ϕ=21+5 ,

>> phi = (1 + sqrt(sym(5)))/2

phi =

5^(1/2)/2 + 1/2

>> f = phi^2 - phi - 1

f =

(5^(1/2)/2 + 1/2)^2 - 5^(1/2)/2 - 3/2
>> ff = str2sym('a^2+b^2')

ff =

a^2 + b^2

1.4 Symbolic Functions

Use symbolic functions that accept symbolic inputs, such as f ( x , y ) f(x,y) f(x,y).

>> syms f(x,y)
>> f

f(x, y) =

f(x, y)

Assign a mathematical expression to f.

>> f(x,y) = x^2*y

f(x, y) =

x^2*y

>> f

f(x, y) =

x^2*y

Find the value of f at (3,2).

>> f(3,2)

ans =

18

Find the value of array inputs.

>> xVal = 1:5; yVal = 3:7; f(xVal,yVal)

ans =

[ 3, 16, 45, 96, 175]

You can differentiate symbolic functions, integrate or simplify an expression. The result is also a symbolic function. Differentiate,

>> dfx = diff(f,x)

dfx(x, y) =

2*x*y

1.5 Symbolic Matrices

Use matrices containing symbolic values.
Use Existing Symbolic Variables

>> syms a b c
A = [a b c; c a b; b c a]

A =

[ a, b, c]
[ c, a, b]
[ b, c, a]

To check if the sum of the elements of the first row equals the sum of the elements of the second column, use the isAlways function:

isAlways(cond) checks if the condition cond is valid for all possible values of the symbolic variables in cond.

>> isAlways(sum(A(1,:)) == sum(A(:,2)))

ans =

  logical

   1

Generate Elements While Creating a Matrix

>> A = sym('A', [2 4])

A =

[ A1_1, A1_2, A1_3, A1_4]
[ A2_1, A2_2, A2_3, A2_4]

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

A =

[ A11, A12, A13, A14]
[ A21, A22, A23, A24]

Create Matrix of Symbolic Numbers

>> A = hilb(3)

A =

    1.0000    0.5000    0.3333
    0.5000    0.3333    0.2500
    0.3333    0.2500    0.2000

>> A = sym(A)

A =

[   1, 1/2, 1/3]
[ 1/2, 1/3, 1/4]
[ 1/3, 1/4, 1/5]

2 进行符号计算

Perform computations on symbolic objects.

2.1 求导,求微分

Differentiate Symbolic Expressions

通过该工具箱,可以进行多种符号对象的多种求导计算:

  • Derivatives of single-variable expressions
  • Partial derivatives
  • Second and higher order derivatives
  • Mixed derivatives

2.1.1 单变量

>> syms x
f = sin(x)^2; 
diff(f)

ans =

2*cos(x)*sin(x)

2.1.2 偏导

Specify the differentiation variable,

>> syms x y
f = sin(x)^2 + cos(y)^2;
diff(f, y)

ans =

-2*cos(y)*sin(y)

2.1.3 高阶导

>> syms x; f = x^4; diff(f, x, 3)

ans =

24*x
>> syms x y
f = sin(x)^2 + cos(y)^2;
diff(f, y, 2)

ans =

2*sin(y)^2 - 2*cos(y)^2

2.1.4 混合求导

>>  syms x y
f = sin(x)^2 + cos(y)^2;
diff(f, y)

ans =

-2*cos(y)*sin(y)

>> syms x y
f = sin(x)^2 + cos(y)^2;
diff(diff(f, y), x)

ans =

0

2.2 求积分

Indefinite and definite integration
Integration of multivariable expressions

2.2.1 单变量不定积分

Indefinite Integrals of One-Variable Expressions

>> syms x
f = sin(x)^2;
>> int(f)

ans =

x/2 - sin(2*x)/4

2.2.2 多变量不定积分

Indefinite Integrals of Multivariable Expressions

>> syms x y n
f = x^n + y^n;
int(f,x)

ans =

x*y^n + (x*x^n)/(n + 1)
>> syms x y n
f = x^n + y^n;
int(f, n)

ans =

x^n/log(x) + y^n/log(y)

2.2.3 定积分

Definite Integrals

>> syms x; f = x^2; int(f, x)

ans =

x^3/3

>> int(f,x,1,2)

ans =

7/3
>> syms x y n
f = x^n + y^n;
int(f, 1, 10)

ans =

piecewise(n == -1, log(10) + 9/y, n ~= -1, (10*10^n - 1)/(n + 1) + 9*y^n)

2.2.4 没有封闭形式解

Cannot Find a Closed Form of an Integral
It returns an unresolved integral,

>> syms x
int(sin(sinh(x)))

ans =

int(sin(sinh(x)), x)

2.3 解方程

Solve Equations 延申Equation Solving
该工具箱可以求解多种类型的方程: Algebraic equations with one symbolic variable Algebraic equations with several symbolic variables * Systems of algebraic equations

2.3.1 单变量

>> syms x
solve(x^3 - 6*x^2 == 6 - 11*x)

ans =

 1
 2
 3
It assumes the undefined right side is zero:

>> syms x
solve(x^3 - 6*x^2 + 11*x - 6)

ans =

 1
 2
 3

2.3.2 多变量

可以指定求解某变量关于另一变量的解表达式,

>> syms x y
solve(6*x^2 - 6*x^2*y + x*y^2 - x*y + y^3 - y^2 == 0, y)

ans =

    1
  2*x
 -3*x

2.3.3 方程组

>> syms x y z
[x, y, z] = solve(z == 4*x, x == y, z == x^2 + y^2)

x =

 0
 2


y =

 0
 2


z =

 0
 8

2.4 化简

Simplify Symbolic Expressions

2.4.1 simplify(f)化简

>> syms x
>> phi = (1 + sqrt(x))/2;
>> f = phi^2 - phi - 1

f =

(x^(1/2)/2 + 1/2)^2 - x^(1/2)/2 - 3/2

>> simplify(f)

ans =

x/4 - 5/4

2.4.2 factor(f) 因式分解

>> syms x
g = x^3 + 6*x^2 + 11*x + 6;
factor(g)

ans =

[ x + 3, x + 2, x + 1]

2.4.3 expand(f) 展开为多项式标准形式

>> syms x
f = (x ^2- 1)*(x^4 + x^3 + x^2 + x + 1)*(x^4 - x^3 + x^2 - x + 1);
expand(f)

ans =

x^10 - 1

2.4.4 horner(f) 转为嵌套形式

>> syms x
h = x^5 + x^4 + x^3 + x^2 + x;
horner(h)

ans =

x*(x*(x*(x*(x + 1) + 1) + 1) + 1)

2.5 将值代入表达式

Substitutions in Symbolic Expressions

2.5.1 代入数值

>> syms x
f = 2*x^2 - 3*x + 1;
subs(f, 1/3)

ans =

2/9

指定变量代入,

>> syms x y
f = x^2*y + 5*x*sqrt(y);
>> subs(f, x, 3)

ans =

9*y + 15*y^(1/2)

2.5.2 代入符号变量

>> syms x y
f = x^2*y + 5*x*sqrt(y);
>> subs(f, y, x)

ans =

x^3 + 5*x^(3/2)
>> syms a b c
A = [a b c; c a b; b c a]

A =

[ a, b, c]
[ c, a, b]
[ b, c, a]

>> alpha = sym('alpha');
beta = sym('beta');
A(2,1) = beta;
A = subs(A,b,alpha)

A =

[     a, alpha,     c]
[  beta,     a, alpha]
[ alpha,     c,     a]

2.5.3 代入矩阵

Substitute a Matrix into a Polynomial

2.5.3.1 Element-by-Element 代入

即按矩阵单个元素依次代入,

>> syms x
f = x^3 - 15*x^2 - 24*x + 350;
A = [1 2 3; 4 5 6];
subs(f,A)

ans =

[ 312, 250,  170]
[  78, -20, -118]
2.5.3.2 矩阵整体代入

使用sym2poly(f)将原符号表达式转换为多项式b,然后直接使用polyvalm(b,A)求解。 也可以重写符号表达式对应的矩阵形式,再代入矩阵求解。

>> syms x
f = x^3 - 15*x^2 - 24*x + 350;
>> A = magic(3)

A =

     8     1     6
     3     5     7
     4     9     2

>> b = sym2poly(f)

b =

     1   -15   -24   350

>> polyvalm(b,A)

ans =

   -10     0     0
     0   -10     0
     0     0   -10

>> A^3 - 15*A^2 - 24*A + 350*eye(3)

ans =

   -10     0     0
     0   -10     0
     0     0   -10

3 使用假设限定

Use Assumptions on Symbolic Variables

3.1 默认假设

工具箱默认符号变量为不带假设,使用assumptions(z)检查,若返回空,即不带任何假设,

>> syms z
>> assumptions(z)

ans =

Empty sym: 1-by-0

>> assume(z>=0)
>> assumptions(z)

ans =

0 <= z

3.2 设置假设

上例中已使用assume()设置了假设,也可以使用其他方式设置假设,

>> assumeAlso(x,'integer')
>> assumptions(x)

ans =

in(x, 'integer')
>> a = sym('a', 'real');
b = sym('b', 'real');
c = sym('c', 'positive');
>> assumptions(a)

ans =

in(a, 'real')
>> syms a b real
syms c positive
>> assumptions(c)

ans =

0 < c

3.3 删除假设

注意,在使用clear x语句删除假设时:使用syms创建的带有假设的符号被删除后,若使用sym再次创建该符号,该符号会保持之前的假设。若要彻底删除假设,需要使用syms创建并再次删除。

即理论上,下式应有复数解,但sym使其保持了之前的实数假设,

>> syms x real
>> clear x
>> x = sym('x');
>> solve(x^2 + 1 == 0, x)

ans =

Empty sym: 0-by-1

若要去除假设,需使用syms创建变量,

>> syms x real
clear x
syms x;
solve(x^2 + 1 == 0, x)

ans =

 -1i
  1i
 类似资料: