当前位置:文档之家› 第三讲 MATLAB的符号运算

第三讲 MATLAB的符号运算

第三讲MATLAB的符号运算——matlab 不仅具有数值运算功能,还开发了在matlab环境下实现符号计算的工具包Symbolic Math Toolbox符号运算的功能•符号表达式、符号矩阵的创建•符号线性代数•因式分解、展开和简化•符号代数方程求解•符号微积分•符号微分方程一、创建符号变量1.什么是符号运算•与数值运算的区别※数值运算中必须先对变量赋值,然后才能参与运算。

※符号运算无须事先对独立变量赋值,运算结果以标准的符号形式表达。

•特点:①运算对象可以是没赋值的符号变量②可以获得任意精度的解•Symbolic Math Toolbox——符号运算工具包通过调用Maple软件实现符号计算的。

•maple软件——主要功能是符号运算,它占据符号软件的主导地位。

2. Sym函数定义符号变量(1)S=sym(arg)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 valuesx = 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') andr = sym ( 'Rho‘ , 'real') are other examples. Similarly, k =sym('k','positive') makes k a positive (real) variable.x = sym('x','unreal') makes x a purely formal variable with no additional properties (i.e., insures that x is NEITHER real NOR positive). See also: SYMS.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 replacesthe 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 represented in the form '1.F'*2^(e) or '-1.F'*2^(e) where F is a string of 13 hexadecimal digits and e is an integer. This captures the floatingpoint values exactly, but may not be convenient for subsequent manipulation.For example, sym(1/10,'f') is '1.999999999999a'*2^(-4) because 1/10 cannot be represented exactly in floating point.'r' stands for 'rational'. Floating point numbers obtained by evaluating expressions of the form p/q, p*pi/q, sqrt(p), 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, an expression of the form p*2^q with large integers p and q reproduces the floating point value exactly. For example, sym(4/3,'r') is '4/3', but sym(1+sqrt(5),'r') is 286977268806824*2^(-51)'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) 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. Fewer than 16 digits looses some 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 floating point number nearest to 4/3.3.syms 函数定义符号变量Short-cut for constructing symbolic objects.SYMS arg1 arg2 ...is short-hand notation forarg1 = sym('arg1');arg2 = sym('arg2'); ...SYMS arg1 arg2 ... realis short-hand notation forarg1 = sym('arg1','real');arg2 = sym('arg2','real'); ...SYMS arg1 arg2 ... positiveis short-hand notation forarg1 = sym('arg1','positive');arg2 = sym('arg2','positive'); ...SYMS arg1 arg2 ... unrealis short-hand notation forarg1 = sym('arg1','unreal');arg2 = sym('arg2','unreal'); ...Each input argument must begin with a letter and must contain onlyalphanumeric characters.By itself, SYMS lists the symbolic objects in the workspace.Examples:syms x beta realis equivalent to:x = sym('x','real');beta = sym('beta','real');syms k positiveis equivalent to:k = sym('k','positive');To clear the symbolic objects x and beta of 'real' or 'positive' status, typesyms x beta unreal二、创建符号•1、创建符号表达式和符号方程•2、创建符号矩阵•3、数字矩阵和符号矩阵的转换•4、符号矩阵的引用和修改•5、建立符号数学函数•6、数据类型间的相互转换1、创建符号表达式和符号方程•(1)采用sym 函数•f=sym(‘a*x^2+b*x+c’) 符号表达式•f=sym(‘a*x^2+b*x+c=0’) 符号方程•(2)直接法•f= ‘a*x^2+b*x+c’ 符号表达式•f= ‘a*x^2+b*x+c=0’ 符号方程2、创建符号矩阵•(1)由sym函数直接创建•A=sym(‘[4+x, x^2, x; x^3,5*x-3,x*a]’)或者syms x,aA=[4+x, x^2, x; x^3,5*x-3,x*a](2)由字符串直接创建a=['[4+x x^2 x ]';'[x^3 5*x-3 x*a]']注意:字符串的长度相等,否则出错3、数字矩阵和符号矩阵的转换•用sym函数•例:•A=[2/5,4/0.78, sqrt(23)/3;0.33,0.333,log(4)]• A =•0.4000 5.1282 1.5986•0.3300 0.3330 1.3863•FA=sym(A)•FA =•[ 2/5, 200/39, sqrt(23/9)]•[ 33/100, 333/1000, 6243314768165359*2^(-52)]4、符号矩阵的引用和修改•在符号计算中,引用和修改只能对符号矩阵的元素一个一个地进行•例:syms a b c d•A=[a,a+c,d+b;c,d,a+c;a+c+d,c,c+d*a];•syms eee ddd•A(1,3)=eee•A(3,2)=ddd5、建立符号数学函数•(1)建立一般数学函数•sym x y z•F=sin(x+y)/(x-y)•G=sqrt(x^2+y^2+z^2)•建立m文件•(2)建立抽象的数学函数•sym var1 var2 var3•F=sym(‘f(var1 var2 var3)’)•例:建立一阶差分函数hx fhxfdf)( )(-+=•Syms x h•F=sym(‘f(x)’)•Df=(subs(f, x,x+h)-f))/h6、数据类型之间的相互转换•数值型、字符型、符号型数据类型,符号型级别最高,数值型级别最低•(1)转换为数值变量•X=double(S) 将符号变量变为数值变量•(2)转换为符号变量•S=sym(f) 对变量f没有限制符号矩阵与数值矩阵的转换将数值矩阵转化为符号矩阵函数调用格式:sym(A)A=[1/3,2.5;1/0.7,2/5]A =0.3333 2.50001.4286 0.4000sym(A)ans =[ 1/3, 5/2][10/7, 2/5]将符号矩阵转化为数值矩阵函数调用格式:numeric(A)A =[ 1/3, 5/2][10/7, 2/5]numeric(A)ans =0.3333 2.50001.4286 0.4000三、符号运算1.符号矩阵运算所有涉及符号运算的操作都有专用函数来进行,5.0 以上也可以用运算符,矩阵运算操作指令都比较直观、简单。

相关主题