绪:什么是符号计算?
所谓符号计算是指在运算时,无须事先对变量赋值,而将所得到结果以标准的符号形式来表示。
例如,在符号变量运算过程中pi就用pi表示,而不是具体的近似数值在这里插入代码片3.14或3.1415926。使用符号变量进行运算能最大限度地减少运算过程中因舍入而造成的误差。符号变量也便于进行运算过程的演示。
1.字符型数据变量的创建
var = ‘expression
字符型变量是以矩阵的形式存在于Matlab的工作空间中的
示例
>> var = 'hello world'
var =
hello world
>> size(var)
ans =
1 11
>> y = '1 + sin(2*x)'
y =
1 + sin(2*x)
2.符号型数据变量的创建
符号对象中的符号常量、变量、函数和表达式,可以用sym和syms函数创建。使用class函数测试建立的操作对象为何种操作对象类型及是否为符号对象类型。
以下为Matlab自带的sym函数解释:
>> help sym
sym Construct symbolic numbers, variables and objects.
S = sym(A) constructs an object S, of class 'sym', from A.
If the input argument is a string, the result is a symbolic number
or variable. If the input argument is a numeric scalar or matrix,
the result is a symbolic representation of the given numeric values.
If the input is a function handle the result is the symbolic form
of the body of the function handle.
x = sym('x') creates the symbolic variable with name 'x' and stores the
result in x. x = sym('x','real') also assumes that x is real, so that
conj(x) is equal to x. alpha = sym('alpha') and r = sym('Rho','real')
are other examples. Similarly, k = sym('k','positive') makes k a
positive (real) variable. x = sym('x','clear') restores x to a
formal variable with no additional properties (i.e., insures that x
is NEITHER real NOR positive).
See also: SYMS.
A = sym('A',[N1 N2 ... Nk]) creates an N1-by-N2-...-by-Nk array of
symbolic scalar variables. Elements of vectors have names of the form Ai and elements
of matrices and higher-dimensional arrays have names of the form
Ai1_..._ik where each ij ranges over 1:Nj.
The form can be controlled exactly by using '%d' in the first
input (eg 'A%d%d' will make names Ai1i2).
A = sym('A',N) creates an N-by-N matrix.
sym(A,ASSUMPTION) makes or clears assumptions on A as described in
the previous paragraph.
Statements like pi = sym('pi') and delta = sym('1/10') create symbolic
numbers which avoid the floating point approximations inherent in the
values of pi and 1/10. The pi created in this way temporarily replaces
the built-in numeric function with the same name.
S = sym(A,flag) converts a numeric scalar or matrix to symbolic form.
The technique for converting floating point numbers is specified by
the optional second argument, which may be 'f', 'r', 'e' or 'd'.
The default is 'r'.
'f' stands for 'floating point'. All values are transformed from
double precision to exact numeric values N*2^e for integers N and e.
'r' stands for 'rational'. Floating point numbers obtained by
evaluating expressions of the form p/q, p*pi/q, sqrt(p/q), 2^q and 10^q
for modest sized integers p and q are converted to the corresponding
symbolic form. This effectively compensates for the roundoff error
involved in the original evaluation, but may not represent the floating
point value precisely. If no simple rational approximation can be
found, the 'f' form is used.
'e' stands for 'estimate error'. The 'r' form is supplemented by a
term involving the variable 'eps' which estimates the difference
between the theoretical rational expression and its actual floating
point value. For example, sym(3*pi/4,'e') is 3*pi/4-103*eps/249.
'd' stands for 'decimal'. The number of digits is taken from the
current setting of DIGITS used by VPA. Using fewer than 16 digits
reduces accuracy, while more than 16 digits may not be warranted.
For example, with digits(10), sym(4/3,'d') is 1.333333333, while
with digits(20), sym(4/3,'d') is 1.3333333333333332593,
which does not end in a string of 3's, but is an accurate decimal
representation of the double-precision floating point number nearest
to 4/3.
See also syms, class, digits, vpa.
sym 的参考页
sym函数:可以生成单个的符号变量
- var = sym(‘var’,set):创建一个符号变量,并设置符号对象的格式,set可以不选。
- ‘position’:限定var表示正的实型符号变量
- ‘real’:限定var为实型符号变量
- ‘unreal’:限定var为非实型符号变量
- sym(‘var’,‘clear’):清除先前设置的符号变量var
- num = sym(num,flag):将一个数值转换为符号形式,输入参数flag为转换的符号对象应该符合的格式类型
- ‘r’:最接近有理表示,为系统默认设置
- ‘e’:带估计误差的有理表示
- ‘f’:十六进制浮点表示
- ‘d’:最接近十进制浮点精确表示
- A = sym(‘A’,dim):创建一个矢量或矩阵的符号变量
- A = sym(‘A’,set):创建一个符号矩阵,set用于设置矩阵的维数
- sym(A,‘clear’):清除前面已创建的符号矩阵A
- f(arg1,…,argN) = sym(‘f(arg1,…,argN’):根据f指定的输入参数arg1,…,argN创建符号变量f(arg1,…,argN)
示例:利用sym函数创建符号对象
>> %利用sym函数创建符号对象
>> sqrt(3)
ans =
1.7321
>> a = sqrt(sym(3))
a =
3^(1/2)
>> a = sym(sqrt(3))
a =
3^(1/2)
syms函数:可以创建任意多个符号变量
- syms var…varN:创建符号变量var…varN
- syms var…varN set:set指定符号对象的格式
- ‘position’:限定var表示正的实型符号变量
- ‘real’:限定var为实型符号变量
- syms var…varN clear:清除前面已经定义好的符号对象
- syms f(arg1,…,argN):创建符号函数f,函数中包含多个符号变量
示例:利用syms函数创建符号表达式
>> %利用sym函数创建符号表达式
>> syms s(t) f(x,y)
>> s(t) = 3*t + 2
s(t) =
3*t + 2
>> s(2)
ans =y
8
>> f(x,y) = x + 3*y
f(x, y) =
x + 3*y
>> f(2,3)
ans =
11
也可以利用sym和syms生成符号矩阵
示例:
>> %利用sym和syms生成符号矩阵
>> m1 = [1,2+x,1;3-x,1,4+y;1,2+y,0]
m1 =
[ 1, x + 2, 1]
[ 3 - x, 1, y + 4]
[ 1, y + 2, 0]
>> m2 = sym('[[1,2+x,1;3-x,1,4+y;1,2+y,0]]')
m2 =
[ [1, x + 2, 1], [3 - x, 1, y + 4], [1, y + 2, 0]]
3.符号计算的运算符与函数
Matlab采用了全新的数据结构、面向对象编程和重载技术,使得符号计算和数值计算在形式上和风格上浑然统一
算数运算符号:
(1)运算符号“+”,“-”,“*”,“/”,“\”,“^”分别实现符号矩阵的加法、减法、乘法、左除、右除和求幂运算
示例:
%利用sym实现矩阵的加法
>> A = sym('[x^2 3;4 * xcos(x)]');
>> B = sym('[1/x^2 2*x;3 x^2+x]');
>> C = A + B
C =
[ 1/x^2 + x^2, 2*x + 3]
[ 4*xcos(x) + 3, x^2 + x]
(2)运算符号“.*”,“./”,“.\”,“.^”分别实现“元素对元素”的乘法、左除、右除和求幂运算
>> %利用syms实现矩阵的点乘
>> syms a b c d e f g h;
>> A = sym('[a,b;c,d]')
A =
[ a, b]
[ c, d]
>> B = sym('[e,f;g,h]')
B =
[ e, f]
[ g, h]
>> R = A * B
R =
[ a*e + b*g, a*f + b*h]
[ c*e + d*g, c*f + d*h]
>> R1 = A .* B
R1 =
[ a*e, b*f]
[ c*g, d*h]
(3)运算符号“ ’ ”," .’ "分别实现符号矩阵的共轭和非共轭转置
>> %符号矩阵的共轭和非共轭转置
>> syms a b c d;
>> R = A'
R =
[ conj(a), conj(c)]
[ conj(b), conj(d)]
>> R1 = A.'
R1 =
[ a, c]
[ b, d]
关系运算符号:
与数值计算中关系运算符号相区别的是,符号计算中的关系运算符还有以下两种:
- 运算符号“==”表示对运算符两边的符号对象进行“相等”的比较,返回值“1”表示相等,“0”表示不等
- 运算符号“~=”表示对运算符号两边的符号对象进行不相等的标记。“1”表示不相等,“0”表示相等
复数函数:
复数函数包括复数的共轭、实部、虚部和模函数,与数值计算中是一致的
矩阵代数函数:
符号计算中,常用的矩阵代数函数有:diag函数,triu函数,tril函数,inv函数,det函数,rank函数,rref函数,null函数,colspace函数,ploy函数、expm函数,eig函数和svd函数
%利用diag函数取符号矩阵对角线元素向量
>> f = sym('[1 2 1; 2 3 5;1 7 9]')
f =
[ 1, 2, 1]
[ 2, 3, 5]
[ 1, 7, 9]
>> a = diag(f)
a =
1
3
9
4.寻找符号变量
Matlab中的符号对象可以是符号常量也可以是符号变量,findsym函数可以找到符号表达式中的符号变量
- findsym(s,n):寻找符号表达式s中的n个符号变量,若没有指定n,则返回s中的全部符号变量。
%利用findsym函数寻找符号表达式中的符号变量
>> syms a b x y;
>> f = a ^ 2 + 6 * b + cos(x - 2) + log(5 + y) + 4 - 5i
f =
a^2 + 6*b + cos(x - 2) + log(y + 5) + (4 - 5i)
>> findsym(f)
ans =
a,b,x,y
>> findsym(f,2)
ans =
x,y
5.符号精度计算
符号计算的一个显著特点是:由于计算过程中不会出现舍入误差,从而可以得到任意精度的数值解。因此,如果想要使得计算结果精确,就可以牺牲计算时间和存储空间,用符号计算来获得计算精度。
在符号运算工具箱中,有三种不同类型的算术运算。
- 数值类型:Matlab的浮点算术运算,最快的运算,需要的计算机内存很小,但是计算结果不精确
- 有理数类型:Maple的精度符号计算
- VPA类型:Maple的任意精度算术运算
digits函数:digits函数用于设定所用数值的精度
- digits(d):符号对象的近似解的精度为d位有效数字,参数d的默认值为32位
- d = digits:得到当前采用的数值计算的精度
%Matlab符号函数的的精度
>> digits
Digits = 32
>> a = sym(1.3,'d')
a =
1.3000000000000000444089209850063
>> digits(50)
>> a = sym(1.3,'d')
a =
1.3000000000000000444089209850062616169452667236328
vpa函数:用于进行可控精度运算
- R = vpa(A):计算符号矩阵A的近似解,精度为函数digits(d)指定的有效位数
- R = vpa(A,d):计算符号矩阵A的近似解,有效位数由参数d指定
%使用vpa函数进行可控精度运算
>> a = vpa(hilb(2))
a =
[ 1.0, 0.5]
[ 0.5, 0.33333333333333333333333333333333]
>> b = vpa(hilb(3),6)
b =
[ 1.0, 0.5, 0.333333]
[ 0.5, 0.333333, 0.25]
[ 0.333333, 0.25, 0.2]
6.显示符号表达式
符号表达式的显示过程中,默认采用Matlab形式的显示,除了默认的显示方式外,还可以使用pretty函数,允许用户将符号表达式显示为符合一般数学表达习惯的数学表达式。
pretty(x):将符号表达式用书写方式显示出来,使用默认的宽
%显示符号表达式
>> sym x;
>> s = solve(x^4 + 2*x + 1,x,'MaxDegree',3);
>> pretty(s)
/ -1 \
| |
| 2 1 |
| #2 - ---- + - |
| 9 #2 3 |
| |
| 1 #2 1 |
| ---- - #1 - -- + - |
| 9 #2 2 3 |
| |
| 1 #2 1 |
| #1 + ---- - -- + - |
\ 9 #2 2 3 /
where
/ 2 \
sqrt(3) | ---- + #2 | 1i
\ 9 #2 /
#1 == ------------------------
2
/ sqrt(11) sqrt(27) 17 \1/3
#2 == | ----------------- - -- |
\ 27 27 /
7.合并符号表达式
collect函数用于实现将符号表达式中的同类项进行合并
ss
- R = collect(s):将表达式s中相同次幂的项合并,系统默认为按照x的相同次幂项进行合并
- R = collect(s,v):将表达式s按照v的次幂项进行合并,输入参数s可以是表达式,也可以是一个符号矩阵
%合并符号表达式
>> syms x y
>> collect((exp(x) + x)*(x + 2))
ans =
x^2 + (exp(x) + 2)*x + 2*exp(x)
>> collect(x^2*y + y*x - x^2 - 2*x,x)
ans =
(y - 1)*x^2 + (y - 2)*x
8.展开符号表达式
expand函数用于将符号表达式展开
s
- expand(s):表达式S中如果包含函数,Matlab会利用恒等式变型将其写成相应的形s式。
- expand(s,Name,Value):设置展开式的参数名Name及其对应的参数值Value
%展开符号表达式
>> syms x y
>> expand((x - 2) * (x - 4))
ans =
x^2 - 6*x + 8
>> expand(cos(x + y))
ans =
cos(x)*cos(y) - sin(x)*sin(y)