1、实 验 报 告实验名称 MATLAB 数值运算 指导教师 魏永生 专 业 光信 班 级 14 物 81 姓 名 达旭琦 学 号 140220008 实验四 MATLAB 符号运算一、实验目的掌握符号变量和符号表达式的创建,掌握 MATLAB 的 symbol 工具箱的一些基本应用。二、实验内容(1) 符号变量、表达式、方程及函数的表示。(2) 符号微积分运算。(3) 符号表达式的操作和转换。(4) 符号微分方程求解。三、实验步骤1. 符号运算的引入 f=sym(sin(pi*x)/x)f =sin(pi*x)/x limit(f,x,0)ans =Pi2. 符号常量、符号变量、符号表达式的创建
2、1) 使用 sym( )创建 A=sym(1)A =1 B=sym(x)B =x f=sym(2*x2+3*y-1)f =2*x2+3*y-1 clear f1=sym(1+2)f1 =1+2 f2=sym(1+2)f2 =3 f3=sym(2*x+3)f3 =2*x+3 f4=sym(2*x+3)f4 =52) 使用 syms 创建 clear syms x y z x,y,zx =xy =yz =z f1=x2+2*x+1f1 =x2+2*x+1 f2=exp(y)+exp(z)2f2 =exp(y)+exp(z)2 f3=f1+f2f3 =x2+2*x+1+exp(y)+exp(z)2
3、f1=sym(x2+y+sin(2)f1 =x2+y+sin(2) syms x y f2=x2+y+sin(2)f2 =x2+y+4095111552621091/4503599627370496 x=sym(2),y=sym(1)x =2y =1 f3=x2+y+sin(2)f3 =26613109689473571/4503599627370496 y=sym(w)y =w f4=x2+y+sin(2)f4 =22109510062103075/4503599627370496+w思考题:syms x 是不是相当于 xsym(x)?理解:这两个式子是不相同的。syms x 是定义 x 为
4、符号变量,而 xsym(x)?是给 x赋值。3. 符号矩阵创建 syms a1 a2 a3 a4 A=a1 a2;a3 a4A = a1, a2 a3, a4 A(1),A(3)ans =a1ans =a2或者 B=sym( b1 b2;b3 b4)B = b1, b2 b3, b4 c1=sym(sin(x)c1 =sin(x) c2=sym(x2)c2 =x2 c3=sym(3*y+z)c3 =3*y+z c4=sym(3)c4 =3 C=c1 c2;c3 c4C = sin(x), x2 3*y+z, 3练习:分别用 sym 和 syms 创建符号表达式4.符号算术运算1) 符号量相乘、
5、相除 a=sym(5);b=sym(7); c1=a*bc1 =35 c2=a/bc2 =5/7 a=sym(5);B=sym(3 4 5); C1=a*B,C2=aB ye2t.C1 = 15, 20, 25C2 = 3/5, 4/5, 1 syms a b A=5 a;b 3;B=2*a b;2*b a; C1=A*B, C2=A.*BC1 = 10*a+2*a*b, 5*b+a2 2*a*b+6*b, b2+3*aC2 = 10*a, a*b 2*b2, 3*a C3=AB,C4=A./BC3 = 2*a*(b-3)/(-15+a*b), (a2-3*b)/(-15+a*b) 2*b*(
6、a-5)/(-15+a*b), -(5*a-b2)/(-15+a*b)C4 = 5/2/a, a/b 1/2, 3/a2) 符号数值任意精度控制和运算 a=sym(2*sqrt(5)+pi)a =2*sqrt(5)+pi b=sym(2*sqrt(5)+pi)b =8572296331135796*2(-50) digitsDigits = 32 vpa(a)ans =7.6137286085893726312809907207421 digits(15) vpa(a)ans =7.61372860858937 c1=vpa(a,56)c1 =7.61372860858937263128099
7、07207420553550784061185981572696 c2=vpa(b,56)c2 =7.61372860858937272610091895330697298049926757812500000003) 符号类型与数值类型的转换 a1=sym(2*sprt(5)+pi)a1 =2*sprt(5)+pi b1=double(a1) a2=vpa(a1,70)a2 =2.*sprt(5)+3.1415926535897932384626433832795028841971693993751058209749445923078165. 符号表达式的操作和转换1)独立变量的确定原则 f=
8、sym(a+b+i+j+x+y+xz)f =a+b+i+j+x+y+xz findsym(f)ans =a, b, j, x, xz, y findsym(f,1), findsym(f,2), findsym(f,3)ans =xans =x,xzans =x,xz,y findsym(f,4), findsym(f,5), findsym(f,6)ans =x,xz,y,jans =x,xz,y,j,bans =x,xz,y,j,b,a2) 符号表达式的化简合并同类项(collect) 。分别按 x 的同幂项和 e 指数同幂项合并表达式:(x2xet1)(xet). syms x t;f=
9、(x2+x*exp(-t)+1)*(x+exp(-t); f1=collect(f) f2=collect(f,exp(-t)对显示格式加以美化(pretty) 。针对上例,用格式美化函数可以使显示出的格式更符合数学书写习惯。 pretty(f1)3 2 2x + 2 exp(-t) x + (1 + exp(-t) ) x + exp(-t) pretty(f2)2 2 2x exp(-t) + (2 x + 1) exp(-t) + (x + 1) x多项式展开(expand).展开(x1)12 成 x 不同幂次的多项式。 clear all syms x; f=(x-1)12; pret
10、ty(expand(f)12 11 10 9 8 7 6 1 + x - 12 x + 66 x - 220 x + 495 x - 792 x + 924 x - 792 x4 3 2+ 495 x - 220 x + 66 x - 12 x因式分解(factor)。将表达式 x121 作因式分解。 clear all syms x;f=x12-1; pretty(factor(f)2 2 2 4 (x - 1) (1 + x + x) (1 + x) (1 - x + x ) (x + 1) (x - x + 1)化简(simple 或 simplify). 将函 数化简。 clear a
11、ll,syms x;f=(1/x3+6/x2+12/x+8)(1/3); gi=simple(f)gi =5 2(2*x+1)/x g2=simplify(f)g2 =(2*x+1)3/x3)(1/3)6. 符号表达式的变量替换 clear f=sym(x+y)2+4*x+10)f =(x+y)2+4*x+10 f1=subs(f,x,s)f1 =(s)+y)2+4*(s)+10 f2=subs(f,x+y,z)f2 =(z)2+4*x+107. 符号极限、符号积分与微分1) 求极限函数的调用格式 limit(F,x,a) limit(F,a) limit(F) limit(F,x,a,rig
12、ht) limit(F,x,a,left)例一 f=sym(sin(x)/x+a*x)f =sin(x)/x+a*x limit(f,x,0)ans =1 limit(f,a,0)ans =sin(x)/x limit(f)ans =1 findsym(f)ans =a, x例二 clear f=sym(sqrt(1+1/n); limit(f,n,inf)2) 求积分函数的调用格式 int(F) int(F,v) int(F,a,b) int(F,v,a,b)3) 求微分函数的调用格式 diff(F) diff(F,v) diff(F,n) diff(F,v,n)8. 符号方程的求解1) 常
13、规方程求解函数的调用格式 g=solve(eq) g=solve(eq,var) g=solve(eq1,eq2,.,var1,var2,.,varn)求一元二次方程 axbxx0 的解。其求解方法有多种形式 Seq=solve(a*x2+b*x+c)Seq =-1/2*(b-(b2-4*a*c)(1/2)/a-1/2*(b+(b2-4*a*c)(1/2)/a Seq=solve(a*x2+b*x+c=0)Seq =-1/2*(b-(b2-4*a*c)(1/2)/a-1/2*(b+(b2-4*a*c)(1/2)/a eq=a*x2+b*x+c; eq=a*x2+b*x+c=0; Seq=sol
14、ve(eq)Seq =-1/2*(b-(b2-4*a*c)(1/2)/a-1/2*(b+(b2-4*a*c)(1/2)/a syms x a b c; eq=a*x2+b*x+c; Seq=solve(eq)2Seq =-1/2*(b-(b2-4*a*c)(1/2)/a-1/2*(b+(b2-4*a*c)(1/2)/a2) 常微分方程求解常微分方程求解函数的调用格式: r=dsolve(eq1,eq2,.,cond1,cond2,.,v) r=dsolve(eq1,eq2,.,cond1,cond2,.,v) x=sym(x)x =x f=(x2-1)/(x2-3*x+2)f =(x2-1)/
15、(x2-3*x+2) limit(f,x,2)ans =NaN x=sym(x)x =x fx=cos(2*x)-sin(2*x)fx =cos(2*x)-sin(2*x) int(fx,x)ans =1/2*sin(2*x)+1/2*cos(2*x) gx=sqrt(exp(x)+sin(x)gx =(exp(x)+sin(x)(1/2) pretty(gx)1/2(exp(x) + sin(x) clear x=sym(x)x =x gx=sqrt(exp(x)+sin(x)gx =(exp(x)+sin(x)(1/2) diff(gx,x)ans =1/2/(exp(x)+sin(x)(
16、1/2)*(exp(x)+cos(x) clear x=sym(x)x =x gx=sqrt(exp(x)+sin(x)gx =(exp(x)+sin(x)(1/2) clear x=sym(x)x =x f=sin(x)+2f =sin(x)+2 int(f,x,0,pi/6)ans =1-1/2*3(1/2)+1/3*pi clear x=sym(x)x =x f=sin(x)+2f =sin(x)+2 int(f,x,0,pi/6)ans =1-1/2*3(1/2)+1/3*pi clear syms x y z x,y,z=solve(x+y+z=10,3*x+2*y+z=14,2*x+3*y-z=1)x =1y =2z =7 dsolve(Dy-z=sin(x),Dz+y=1+x,y(0)=2,z(0)=7,x)ans = y: 1x1 symz: 1x1 sym