MATLAB中的数值计算与符号计算.ppt
微积分实验(2)MATLAB中的数值计算与符号计算,函数复合命令格式:compose(f,g,x),即求f(g(x);例子:syms f,x;f=sin(x);compose(f,f,x)ans=sin(sin(x)syms f,x;f=sin(x);g=1+x2;compose(f,g,x)ans=sin(1+x2),设f(sin(x/2)=cos(x)+1,求f(x)与f(cos(x/2).syms x,f;old=cos(x);new=1-2*sin(x/2)2;subs(f(sin(x/2)=cos(x)+1,old,new)ans=f(sin(x/2)=(1-2*sin(x/2)2)+1syms u;subs(ans,sin(x/2),u)ans=f(u)=2-2*u2,Syms x,f,g;f=x/sqrt(1+x2);g=x;g=simple(compose(f,f,x)f=x/(1+x2)(1/2)g=x/(2*x2+1)(1/2)g=simple(compose(g,f,x)g=x/(3*x2+1)(1/2)g=simple(compose(g,f,x)g=x/(4*x2+1)(1/2)g=simple(compose(g,f,x)g=x/(5*x2+1)(1/2),syms x,f;f=sin(x)2/(1+cot(x)+cos(x)2/(1+tan(x);diff(f,x)Ans=2*sin(x)/(1+cot(x)*cos(x)-sin(x)2/(1+cot(x)2*(-1-cot(x)2)-2*cos(x)/(1+tan(x)*sin(x)-cos(x)2/(1+tan(x)2*(1+tan(x)2)simple(ans)ans=-2*cos(x)2+1diff(f,x,2)ans=2*cos(x)2/(1+cot(x)-4*sin(x)/(1+cot(x)2*cos(x)*(-1-cot(x)2)-2*sin(x)2/(1+cot(x)+2*sin(x)2/(1+cot(x)3*(-1-cot(x)2)2+2*sin(x)2/(1+cot(x)2*cot(x)*(-1-cot(x)2)+2*sin(x)2/(1+tan(x)+4*cos(x)/(1+tan(x)2*sin(x)*(1+tan(x)2)-2*cos(x)2/(1+tan(x)+2*cos(x)2/(1+tan(x)3*(1+tan(x)2)2-2*cos(x)2/(1+tan(x)2*tan(x)*(1+tan(x)2)simple(ans)ans=4*sin(x)*cos(x),syms x y z f g;f=exp(z)-z+x*y-3;g=jacobian(f,x,y,z)g=y,x,exp(z)-1g(1)ans=yg(2)ans=xg(3)ans=exp(z)-1g(1)/g(3)ans=y/(exp(z)-1),syms x y t a;x=a*cos(t)3;y=a*sin(t)3;y=simple(diff(y,t)/diff(x,t)y=-tan(t)simple(diff(y,t)/diff(x,t)ans=1/3/cos(t)4/a/sin(t),syms x;int(1-sin(x)+cos(x)/(1+sin(x)-cos(x)ans=2*log(tan(1/2*x)-2*log(tan(1/2*x)+1)-2*atan(tan(1/2*x),simple(ans)ans=2*log(tan(1/2*x)-2*log(tan(1/2*x)+1)-2*atan(tan(1/2*x),MATLAB做不到这一点,但这个积分在Mathematica中的计算结果为,先画出图形,syms x y;y=2*x3-6*x2-18*x+7;ezplot(y,-5,5),再求出驻点(有2个,分别为-1及3)solve(diff(y,x),x)ans=-1 3求函数的2阶导数y=diff(y,x,2)y=12*x-12,计算2阶导数在驻点处的函数值x=-1;eval(y)ans=-24x=3;eval(y)ans=24由高等数学定理知,在-1处取得极大值,在3处取得极小值,这个积分无法计算出理论解,只能计算数值解syms x;int(sin(sin(x),1,10)ans=int(sin(sin(x),x=1.10),第一种方法,用梯形法计算,其命令为trapz(x,y),其中x,y是具有相同长度的向量,表示用梯形法分割时,数据点上的值.此积分的实际值为0.199(精确到20位)x=1:0.1:10;y=sin(sin(x);trapz(x,y)ans=1.1981,第二种方法,用Simpson(辛普森法)计算,其命令是quad(f,a,b,tol),其中a,b为积分的上下限,tol为迭代误差,其默认值为10(-3)quad(sin(sin(x),1,10,10(-7)ans=1.1990第三种方法,用Newton-cotes法(牛顿-科特茨法),其命令是quad8(f,a,b,tol),其用法与Simpson法一样quad8(sin(sin(x),1,10,10(-3)ans=1.1990Newton-cotes法的计算精度比其它2种要高,例如,在相同的计算量下,比较quad(sin(sin(x),1,10,10(-3)ans=1.1976,命令:dblquad(f,xmin,xmax,ymin,ymax)先画出区域的图形syms x y;ezplot(y2-x,0,5,-3,3);hold on;ezplot(x-2,0,5,-3,3);hold off,再求2条曲线的2个交点syms x y;x,y=solve(y2-x=0,x-2=y)x=1 4y=-1 2因此,求出的2个交点是(1,-1)及(4,2),这是一个左右型区域,应该先对x积分,再对y积分比较简单,因此有syms x y;dblquad(x*y,y2,y+2,-1,2)结果,MATLAB会提示出现错误,是语法错误吗?将上面改成,syms x y;dblquad(x*y,0,1,-1,2)ans=0.7500这说明,不是语法错误.实际上,这个命令只能计算矩形区域的积分,如果积分区域不上矩形区域,那么,你就得定义一个二元函数,让这个函数在积分区域外为0,比如,对此积分,定义,而且,它也只能计算二重积分的数值解.,关于微分方程的求解,命令格式:dsolve(eqn1,eqn2,.);如果不额外说明,默认的变量是tdsolve(Dy=y2*(1-y)Warning:Explicit solution could not be found;implicit solution returned.In C:MATLABR12toolboxsymbolicdsolve.m at line 292ans=t+1/y-log(y)+log(-1+y)+C1=0dsolve(D2y=-a2*y,y(0)=1,Dy(pi/a)=0)ans=cos(a*t)x,y=dsolve(Dx=y,Dy=-x,x(0)=0,y(0)=1)x=sin(t)y=cos(t),关于数项级数求和问题.,命令格式:symsum(f,a,b),计算表达式f从a到b的和.syms n;symsum(1/n2,1,inf)ans=1/6*pi2syms n;symsum(1/n3,1,inf)ans=zeta(3)syms n;symsum(1/n4,1,inf)ans=1/90*pi4syms n;symsum(-1)(n-1)/n,1,inf)ans=log(2),