1、符号数学基础一 符号对象的创建(Creating a symbolic object)1. 创建符号变量和表达式(Creating a symbolic variable and expression)创建符号变量和表达式的两个基本函数:sym, syms*x=sym(x) 创建一个符号变量x,可以是字符、字符串、表达式或字符表达式。*syms用于方便地一次创建多个符号变量,调用格式为: syms a b c d . 书写简洁意义清楚,建议使用。例1:使用sym函数创建符号变量.a=sym(a)b=sym( hello)c=sym( (1+sqrt(5)/2)y=sym( x3+5*x2+12
2、*x+20)a =ab = helloC = (1+sqrt(5)/2 Y =x3+5*x2+12*x+20例2:用syms函数创建符号变量。syms a b c d 2. 创建符号矩阵(Symbolic matrix Creating)例1:创建一个循环矩阵。syms a b c dn=a b c d;b c d a;c d a b;d a b cn = a, b, c, d b, c, d, a c, d, a, b d, a, b, c例2:将3阶Hilbert矩阵转换为符号矩阵。 h=hilb(3)h1=sym(h)h = 1.0000 0.5000 0.3333 0.5000 0.3
3、333 0.2500 0.3333 0.2500 0.2000h1 = 1, 1/2, 1/3 1/2, 1/3, 1/4 1/3, 1/4, 1/5注意符号矩阵于数值矩阵的区别。3. 默认符号变量(Implied symbolic variable)在MATLAB的符号数学工具箱中,以最接近x的顺序排列默认自变量的顺序,可利用findsym函数对默认自变量进行查询。例1: 求符号函数在不同自变量情况下的结果。 创建符号变量x和n,建立函数f=xn,然后分别求f对x和f对n的导数. syms x nf=xndiff(f) % x作为自变量,求f对x的导数diff(f,n) % n作为自变量,求
4、f对n的导数f =xnans = xn*n/x ans =xn*log(x)例2: 查询符号函数中的默认自变量。创建符号变量 a,b, n, x 和t ,建立函数f=axn+bt,然后求f的默认自变量。syms a b n t xf=a*xn+b*tfindsym(f,1)findsym(f,2)findsym(f,5) % f表达式中按最接近x顺序排列的5个默认自变量findsym(f) % f表达式中按最接近字母顺序排列的全部自变量f = a*xn+b*t ans =xans =x,tans =x,t,n,b,aans =a, b, n, t, x二. 符号表达式的化简和替换(simpli
5、fying and replacing of Symbolic xpressions) 符号数学工具箱提供的符号表达式的因式分解、展开、合并、化简、通分等操作:1. 符号表达式的化简(Simplifying of symbolic expression)(1).因式分解(Factorization)符号表达式的因式分解函数为 factor(S), 可分解符号表达式S的各个元素。例1: 对表达式f=x9-1进行因式分解。syms x f=factor(x9-1)pretty(f)f =(x-1)*(x2+x+1)*(x6+x3+1) 2 6 3 (x - 1) (x + x + 1) (x +
6、x + 1)例2:对大整数12345678901234567890进行因式分解。factor(sym(12345678901234567890)ans = (2)*(3)2*(5)*(101)*(3803)*(3607)*(27961)*(3541)(2)符号表达式的展开(Expanding of symbolic expressions)符号表达式的展开函数为expand(S), 此函数因数展开符号表达式S.例: 展开表达式f=(x+1)5和f=sin(x+y) syms x yf=(x+1)5;expand(f)f=sin(x+y);expand(f)ans = x5+5*x4+10*x3
7、+10*x2+5*x+1 ans = sin(x)*cos(y)+cos(x)*sin(y)(3).符号表达式的同类项合并(Similar team merging for symbolic expression)符号表达式的同类项合并函数为 collect(S,n),此函数将符号表达式中自变量的同次幂项的系数合并。例:对于表达式f=x(x(x-6)+12)t, 分别将自变量x和t的同类项合并。 syms x tf=x*(x*(x-6)+12)*t;collect(f)collect(f,t)ans =t*x3-6*t*x2+12*t*x ans =x*(x*(x-6)+12)*tCOLLEC
8、T Collect coefficients.COLLECT(S,v) regards each element of the symbolic matrix S as a polynomial in v and rewrites S in terms of the powers of v.COLLECT(S) uses the default variable determined by FINDSYM. (4). 符号表达式的化简(Simplifying of symbolic expression)符号表达式的两个化简函数:simplify, simple ,simplify:化简函数,
9、可用于化简各种表达式 例1:对表达式f=sin2(x)+cos2(x)进行化简.syms xf=sin(x)2+cos(x)2;simplify(f)ans =(5)符号表达式的分式通分(Reduction symbolic expression to common denominator)符号表达式的分式通分函数为 n,d=numden(S), 此函数将符号表达式转换为分子(Numerator)和分母(denominator)都是正系数的最佳多项式。例:对表达式 f=x/y+y/x 进行通分。 syms x yf=x/y+y/x;n,d=numden(f)n = x2+y2d = y*xNU
10、MDEN Numerator and denominator of a symbolic expression.N,D = NUMDEN(A) converts each element of A to a rational form where the numerator and denominator are relatively prime polynomials with integer coefficients.(6) 符号表达式的嵌套形式重写(Representation of nested symbolic expression)符号表达式的嵌套形式重写函数为 horner(S)
11、, 此函数将符号表达式转换为嵌套形式。 例: 对表达式f=x3+6x2+11x-6进行嵌套形式重写。 syms xf=x3+6*x2+11*x-6;horner(f)ans =-6+(11+(6+x)*x)*xHORNER Horner polynomial representation.HORNER(P) transforms the symbolic polynomial P into its Horner, or nested, representation.2. 符号表达式的替换(Replacing of symbolic expression)MATLAB 的符号数学工具箱提供了两个
12、符号表达式的替换函数subexpr 和subs,可通过符号替换使表达式的输出形式简化。subexpr函数可将表达式中重复出现的字符串用变量代替。调用格式:Y,SIGMA=subexpr(S,SIGMA): 用变量SIGMA的值代替符号表达式S中重复出现的字符串,Y返回替换后的结果。例:求解并化简三次方程x3+ax+1=0的符号解。 t=solve(x3+a*x+1=0)r,s=subexpr(t,s)t = 1/6*(-108+12*(12*a3+81)(1/2)(1/3)-2*a/(-108+12*(12*a3+81)(1/2)(1/3) -1/12*(-108+12*(12*a3+81)(
13、1/2)(1/3)+a/(-108+12*(12*a3+81)(1/2)(1/3)+1/2*i*3(1/2)*(1/6*(-108+12*(12*a3+81)(1/2)(1/3)+2*a/(-108+12*(12*a3+81)(1/2)(1/3) -1/12*(-108+12*(12*a3+81)(1/2)(1/3)+a/(-108+12*(12*a3+81)(1/2)(1/3)-1/2*i*3(1/2)*(1/6*(-108+12*(12*a3+81)(1/2)(1/3)+2*a/(-108+12*(12*a3+81)(1/2)(1/3)r = 1/6*s(1/3)-2*a/s(1/3) -
14、1/12*s(1/3)+a/s(1/3)+1/2*i*3(1/2)*(1/6*s(1/3)+2*a/s(1/3) -1/12*s(1/3)+a/s(1/3)-1/2*i*3(1/2)*(1/6*s(1/3)+2*a/s(1/3)s =-108+12*(12*a3+81)(1/2)函数subs是用指定符号替换符号表达式中的某一特定符号,调用格式为:R=subs(S,old,new), 它可用新的符号变量new替换原来符号表达式S中的old. 当new为数值形式时,显示的结果虽然是数值,但它事实上是符号变量。例:分别用新变量替换表达式a+b和cos(a)+sin(b)中变量。 syms a bsu
15、bs(a+b,a,4)subs(cos(a)+sin(b), a,b,sym(alpha),2) %用单元数组完成不同性质%元素的替换ans =4+bans = cos(alpha)+sin(2)三符号微积分(Differential and integral calculus)1. 符号极限(Symbolic limit)*limit(F,x,a) 计算符号表达式F在xa条件下的极限;*limit(F,a) 计算符号表达式F中由默认自变量趋向于a条件下的极限;*limit(F,) 计算符号表达式F在默认自变量趋向于0条件下的极限;*limit(F,x,a,right) 和limit(F,x,
16、a,left) 计算符号表达式F在xa条件下的右极限和左极限。例:分别计算表达式, , 及和 syms x a;limit(sin(x)/x)limit(1/x,x,0,right)limit(1/x,x,0,left)v=(1+a/x)x,exp(-x);limit(v,x,inf,left) ans =1ans =inf ans = -infans = exp(a), 02. 符号微分(symbolic differential calculus)*diff(S) 求符号表达式S对于默认自变量的微分;*diff(S,v) 求符号表达式S对于自变量v的微分;*diff(S,n) 求符号表达式
17、S对于默认自变量的n次微分;例: 分别计算表达式f=xx的导数和3次导数. syms x; f=xx;diff(f)diff(f,3)ans = xx*(log(x)+1) ans = xx*(log(x)+1)3+3*xx*(log(x)+1)/x-xx/x23. 符号积分(Symbolic integral calculus)*int(S) 求符号表达式S对于默认自变量的不定积分; *int(S,v) 求符号表达式S对于自变量v的不定积分;*int(S,a,b) 求符号表达式S对于默认自变量从a到b的定积分; 例:分别计算表达式、 和。 syms x z;f=-2*x/(1+x2)2;in
18、t(f)f=x/(1+z2);int(f)int(f,z)f=x*log(1+x);int(f,0,1)ans =1/(1+x2)ans =1/2*x2/(1+z2) ans = x*atan(z)ans =1/44. 符号求和(Symbolic summation)*symsum(S) 求符号表达式S对于默认自变量的不定和; * symsum(S,v) 求符号表达式S对于自变量v的不定和;* symsum(S,a,b) 求符号表达式S对于默认自变量从a到b的有限和;例: 分别计算表达式k,和 syms k xsymsum(k)symsum(k2,0,10)symsum(xk/sym(k!),
19、k,0,inf)ans =1/2*k2-1/2*kans =385 ans =exp(x)5 .Taylor级数展开(Taylor series expanding)*Taylor(f) 计算符号表达式f对于默认自变量等于0 处的5阶taylor级数展开式; *taylor(f,n,v) 计算符号表达式f在自变量v=0处的n-1阶Taylor级数展开式;*taylor(f,n,v,a) 计算符号表达式f在自变量v=a 处的n-1阶Taylor 级数展开式。例: 分别计算表达式 的5 阶Taylor级数展开式和f=exsin(x) 的5 阶及12 阶Taylor级数展开式。 syms xf=1/
20、(5+cos(x);r=taylor(f)f=exp(x*sin(x);r=taylor(f,12)r=taylor(f)r = 1/6+1/72*x2 r = 1+x2+1/3*x4+1/120*x6-11/560*x8-1079/362880*x10 r = 1+x2+1/3*x4四. 符号方程的求解(Symbolic equation solution)1 . 符号代数方程组的求解(symbolic algebra equations set solution)*g=solve(eq) 求解符号表达式eq=0的代数方程,自变量为默认自变量;*g=solve(eq,var) 求解符号表达式
21、eq=0的代数方程,自变量为var;*g=solve(eq1,eq2,eqn,var1,var2,varn)求解符号表达式eq1,eq2,eqn组成的代数方程组,自变量分别为var1,var2,varn。例1:分别求解代数方程ax2+bx+c=0和cos(2x)+sin(x)=1 syms a b c x s=a*x2+b*x+c;solve(s)solve(cos(2*x)+sin(x)=1)ans = 1/2/a*(-b+(b2-4*a*c)(1/2) 1/2/a*(-b-(b2-4*a*c)(1/2)ans = 0 pi 1/6*pi 5/6*pi例2:求解代数方程组x2-y2+z=10
22、, x+y-5z=0, 2x=4y+z=0 syms x y zf=x2-y2+z-10;g=x+y-5*z;h=2*x-4*y+z;x,y,z=solve(f,g,h) %以数值数组形式输出求解结果S=solve(f,g,h); %缺省情况将方程组的解存放在结构变量中S.x,S.y,S.zx = -19/80+19/240*2409(1/2) -19/80-19/240*2409(1/2)y = -11/80+11/240*2409(1/2) -11/80-11/240*2409(1/2) z = -3/40+1/40*2409(1/2) -3/40-1/40*2409(1/2)ans =-
23、19/80+19/240*2409(1/2), -11/80+11/240*2409(1/2), -3/40+1/40*2409(1/2) -19/80-19/240*2409(1/2), -11/80-11/240*2409(1/2), -3/40-1/40*2409(1/2)SOLVE Symbolic solution of algebraic equations. SOLVE(eqn1,eqn2,.,eqnN) SOLVE(eqn1,eqn2,.,eqnN,var1,var2,.,varN) SOLVE(eqn1,eqn2,.,eqnN,var1,var2,.varN) The eqn
24、s are symbolic expressions or strings specifying equations. Thevars are symbolic variables or strings specifying the unknown variables.SOLVE seeks zeros of the expressions or solutions of the equations. If not specified, the unknowns in the system are determined by FINDSYM.If no analytical solution
25、is found and the number of equations equals the number of dependent variables, a numeric solution is attempted.Three different types of output are possible. For one equation and oneoutput, the resulting solution is returned, with multiple solutions to a nonlinear equation in a symbolic vector. For s
26、everal equations and an equal number of outputs, the results are sorted in lexicographic order and assigned to the outputs. For several equations and a single output, a structure containing the solutions is returned.Examples:solve(p*sin(x) = r) chooses x as the unknown and returns ans = asin(r/p) x,
27、y = solve(x2 + x*y + y = 3,x2 - 4*x + 3 = 0) returns x = 1 3 y = 1 -3/2 S = solve(x2*y2 - 2*x - 1 = 0,x2 - y2 - 1 = 0) returns the solutions in a structure. S = x: 8x1 sym y: 8x1 sym u,v = solve(a*u2 + v2 = 0,u - v = 1) regards a as a parameter and solves the two equations for u and v. S = solve(a*u
28、2 + v2,u - v = 1,a,u) regards v as aparameter, solves the two equations, and returns S.a and S.u.a,u,v = solve(a*u2 + v2,u - v = 1,a2 - 5*a + 6) solves the three equations for a, u and v.a = 2 2 3 3u = 1/3+1/3*i*2(1/2) 1/3-1/3*i*2(1/2) 1/4+1/4*i*3(1/2) 1/4-1/4*i*3(1/2)v = -2/3+1/3*i*2(1/2) -2/3-1/3*
29、i*2(1/2) -3/4+1/4*i*3(1/2) -3/4-1/4*i*3(1/2)FSOLVE Solves nonlinear equations by a least squares method. FSOLVE solves equations of the form: F(X)=0 where F and X may be vectors or matrices. X=FSOLVE(FUN,X0) starts at the matrix X0 and tries to solve the equations in FUN. FUN accepts input X and ret
30、urns a vector (matrix) of equation values F evaluated at X. X=FSOLVE(FUN,X0,OPTIONS) minimizes with the default optimization parameters replaced by values in the structure OPTIONS, an argument created with the OPTIMSET function.See OPTIMSET for details. Used options are Display, TolX, TolFun, Deriva
31、tiveCheck, Diagnostics, Jacobian, JacobMult, JacobPattern, LineSearchType, LevenbergMarquardt, MaxFunEvals, MaxIter, DiffMinChange and DiffMaxChange, LargeScale, MaxPCGIter, PrecondBandWidth, TolPCG, TypicalX. Use the Jacobian option to specify that FUN also returns a second output argument J that i
32、s the Jacobian matrix at the point X. If FUN returns a vector F of m components when X has length n, then J is an m-by-n matrix where J(i,j) is the partial derivative of F(i) with respect to x(j). (Note that the Jacobian J is the transpose of the gradient of F.)X=FSOLVE(FUN,X0,OPTIONS,P1,P2,.) passe
33、s the problem-dependent parameters P1,P2,. directly to the function FUN: FUN(X,P1,P2,.). Pass an empty matrix for OPTIONS to use the default values. X,FVAL=FSOLVE(FUN,X0,.) returns the value of the objective function at X. X,FVAL,EXITFLAG=FSOLVE(FUN,X0,.) returns a string EXITFLAG that describes the
34、 exit condition of FSOLVE. If EXITFLAG is: 0 then FSOLVE converged to a solution X. 0 then the maximum number of function evaluations was reached. 0 then FSOLVE did not converge to a solution.X,FVAL,EXITFLAG,OUTPUT=FSOLVE(FUN,X0,.) returns a structure OUTPUT with the number of iterations taken in OU
35、TPUT.iterations, the number of function evaluations in OUTPUT.funcCount, the algorithm used in OUTPUT.algorithm, the number of CG iterations (if used) in OUTPUT.cgiterations, and the first-order optimality (if used) in OUTPUT.firstorderopt. X,FVAL,EXITFLAG,OUTPUT,JACOB=FSOLVE(FUN,X0,.) returns the J
36、acobian of FUN at X. Examples FUN can be specified using : x = fsolve(myfun,2 3 4, optimset(Display,iter) where MYFUN is a MATLAB function such as: function F = myfun(x) F = sin(x); FUN can also be an inline object: fun = inline(sin(3*x); x = fsolve(fun,1 4,optimset(Display,off);2 符号微分方程求解(Symbolic
37、differential equation solution)符号微分方程求解函数:r=dsolve(eq1,eq2,cond1,cond2,v) 求由eq1,eq2,指定的微分方程的符号解,参数cond1,cond2,为指定常微分方程的边界条件或初始条件,自变量v如果不指定,将为默认自变量。方程中D表示一次微分D2和D3分别表示二次及三次微分,D后的字符为默认自变量。例1: 求微分方程dy/dx=ay的通解和当y(0)=b时的特解。dsolve(Dy=a*y)dsolve(Dy=a*y,y(0)=b,x)ans =C1*exp(a*t) 默认自变量为t ans =b*exp(a*x)例2: 求微分方程=-a2y当y=(0)=1及时的特解。dsolve(D2y=-a2*y,y(0)=1,Dy(pi/a)=0)ans =cos(a*t)