Matlab学习笔记(1)-符号变量及其运算
符号变量及其运算
绪:什么是符号计算?
所谓符号计算是指在运算时,⽆须事先对变量赋值,⽽将所得到结果以标准的符号形式来表⽰。
例如,在符号变量运算过程中pi就⽤pi表⽰,⽽不是具体的近似数值在这⾥插⼊代码⽚3.14或3.1415926。使⽤符号变量进⾏运算能最⼤限度地减少运算过程中因舍⼊⽽造成的误差。符号变量也便于进⾏运算过程的演⽰。
1.字符型数据变量的创建
var = ‘expression
字符型变量是以矩阵的形式存在于Matlab的⼯作空间中的
⽰例
>> var ='hello world'
var =
hello world
>>size(var)
ans =
111
>> 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 -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.
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 and1/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 and10^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*yvariable怎么记
>>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函数:⽤于进⾏可控精度运算
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论