科学计算科学计算 (41).pdf
Chapter 7 MATLABChapter 7 MATLAB SymbolicSymbolic ComputationsComputations7.1 Symbolic Objects7.2 Symbolic Calculus7.3 Series7.4 Solving Symbolic Equations7.1 7.1 Symbolic ObjectsSymbolic ObjectsThe establishment of symbolic objectsThe operations of symbolic objectsSymbolic matrix1.The establishment of symbolic objects1.The establishment of symbolic objects(1)The sym functionThe sym function is used to create singular symbolic object.Its syntax is as follows.symbolic object name=sym(A)The symbolic object is created by A,where Acan be a numeric constant,a numeric matrix ora numeric expression(without single quotes),and the symbolic object is a symbolicconstant;A can also be a variable name(with single quotes),and the symbolic object is asymbolic variable.t=sym(2);t+1/2ans=5/2 sin(sym(pi/3)ans=3(1/2)/2 sin(pi/3)ans=0.8660 a=5;b=-8;x=sym(a);y=sym(b);w=(a+b)*(a-b)w=-39 s=(x+y)*(x-y)s=(a+b)*(a-b)eval(s)ans=-39Compare the following programs:From the above examples,we can see that:The result of symbolic computation is a precise mathematical expression.The result of numerical computation is a number.(2)The syms commandThe syms command can be used to create multiple symbolic variables at the same time.Thegeneral syntax of this command is as follows.syms symbolic variable 1 symbolic variable 2 symbolic variable nThe name of variable cant be quoted with single quotation marks.We should use blank toseparate them.For instance,if we need to create 4 symbolic variables a,b,c,d meantime,wecan input the command as follows.syms a b c d syms x f=2*x2+3*x-5;g=x2-x+7;f+gans=3*x2+2*x+22.T2.The operationhe operations s of symbolic objectsof symbolic objects(1)The four arithmetic operationsThe four arithmetic operations of symbolic objects are the same as those of numericalcomputation,which can be performed with operators of+,-,*,/and.The operating resultis still a symbolic expression.For example:(2)Relational operationsSix relational operators:,=,=and=.Corresponding six functions:lt(),le(),gt(),ge(),eq()and ne().If a symbolic expression is involved in a relational operation,the result will be asymbolic relational expression.If a symbolic matrix is involved in the operation,theresult will be a matrix composed of symbolic relational expressions.syms x assume(x abs(x)=xans=-x=x assume(x,positive);abs(x)=xans=x=xBecause x syms x y=x0&x10y=0 x&x y=and(x0,x10)y=0 x&x syms a b s=a3-b3;factor(s)ans=a-b,a2+a*b+b2 factor(12)ans=2 2 3(4)Factorization and expansionMATLAB provides the functions for factorization and expansion of symbolic expression.Thesyntax of these functions is:factor(s):Factor the symbolic expression s.expand(s):Expand the symbolic expression s.collect(s):Merges items of the same class for symbol expression s.collect(s,v):Merge items of the same class by variable v for symbol expression s.syms p m=2p-1;p=19;m19=eval(m)m19=524287 factor(m19)ans=524287 p=23;m23=eval(m)m23=8388607 factor(m23)ans=47 178481Verify Mersenne prime numberMersenne number is a kind of integer like 2p-1,among which index p is a prime number.Werecord Mersenne number as Mp.If the Mersenne number is a prime number,it is called aMersenne prime number.p=29;m29=eval(m)m29=536870911 factor(m29)ans=233 1103 2089 p=31;m31=eval(m)m31=2147483647 factor(m31)ans=2147483647(5)Other operationsn,d=numden(s):Extract the numerator and denominator from a rational fraction.c=coeffs(s,x):Extract the coefficients of a symbol expression.simplify(s):Simplify symbolic expression.Convert between symbolic polynomial and coefficient vector of polynomial:p=sym2poly(s):Transform symbolic polynomial into polynomial coefficient vector.s=poly2sym(p):Transform coefficient vector of polynomial into symbol polynomial.Example 1:Find the roots of equation ax2+bx+c=0.syms a b c x f=a*x2+b*x+cf=a*x2+b*x+c g=coeffs(f,x)g=c,b,a g=g(end:-1:1)g=a,b,c roots(g)ans=-(b+(b2-4*a*c)(1/2)/(2*a)-(b-(b2-4*a*c)(1/2)/(2*a)(6)Determination of master variables in symbolic operation If the master variable is not specified clearly,MATLAB will determine the mastervariable according to the following principles and perform corresponding operation on it:Look for the lowercase letter closest to x in alphabetical order except i and j.If two symbolic variables in the expression are the same distance from x,the one withlarger ASCII code takes precedence.The symvar function can be used to find symbolic variables in a symbolic expression.The syntax of the function is:symvar(s,n)The function returns n symbolic variables in the symbolic expression s.Therefore,we canuse symvar(s,1)to find the master variable of expression s.sincos33222mab15 xy3xx5 y78=+3 3.S Symbolicymbolic matrimatrix xA symbolic matrix is also an expression,so operations on symbolic expressionsmentioned here can also be performed on matrix.Note:The function will perform operations on every element of matrix.For instance,create symbolic matrix and simplify it.syms a b x y alp m=a3-b3,sin(alp)2+cos(alp)2;(15*x*y-3*x2)/(x-5*y),78m=a3-b3,cos(alp)2+sin(alp)2(-3*x2+15*y*x)/(x-5*y),78 simplify(m)ans=a3-b3,1 -3*x,78xxxxxxxxx123123123(1)2402(3)0(1)0+=+=+=For a system of homogeneous linearequations Ax=0,when the rank(A)n or|A|=0,the system of homogeneous linearequations has non-zero solution.syms lamdaA=1-lamda,-2,4;2,3-lamda,1;1,1,1-lamda;D=det(A);factor(D)The result of program operation is:ans=-1,lamda,lamda-2,lamda-3Therefore,when =0,=2 or =3,the original equations have non-zero solutions.Example 2:What is taken when the system of homogeneous linear equations has non-zero solution.