MATLAB教程2012a第5章习题解答-张志涌..
- 格式:pdf
- 大小:396.00 KB
- 文档页数:12


MATLAB教程(张志涌杨祖樱编2006.8第一版)习题答案第一章5,6,7第二章2,3,6,7,8,9,10,11,22,24第三章2,5,6第四章2,5,11,13,17,18第五章1,2,5,6,7第六章1,2,3第七章1,2第一章5,6,7%p41_5直接计算时得到处于第一象限的方根a=-8; a^(2/3) %-2.0000 + 3.4641i(a^2)^(1/3) %等价于64^(1/3) 得4.0000(a^(1/3))^2 %等价于a^(1/3)也就是1.0000 + 1.7321i的平方得-2.0000 + 3.4641i第二章2,3,6,7,8,9,10,11,22,24%p92_3A1=sym('sin(w*t)'); findsym(A1)A2=sym('a*exp(-X)'); findsym(A2)A3=sym('x*exp(j*theta)'); findsym(A3)%p92_6clear, syms x k, x=abs(x);%或syms x positive;f1=sym(2./(2*k+1).*((x-1)./(x+1)).^(2*k+1))s1=symsum(f1,'k',0,inf), simple(s1), simple(ans)%p92_7clear t, syms tdf=diff('abs(sin(t))'),class(df)df1=limit(df,t,0,'left')df2=subs(df,'t',sym(pi/2))%p92_8clear, syms x;f=exp(-abs(x)).*abs(sin(x));fint=int(f,x,-5*pi,1.7*pi), digits(64), vpa(fint)%p92_9clear,syms x y,f=x.^2+y.^2, fint=(int(int(f,y,1,x.^2),x,1,2)), double(fint)%p92_10clear, syms t x; f=sin(t)./t, yx=int(f,t,0,x), ezplot(yx,[0 2*pi]) fint=subs(yx,x,4.5),%或yxd=int(f,t,0,4.5),fint=double(yxd)hold on, plot(4.5,fint,'*r')%p92_11clear, syms x n; f=sin(x).^n; yn=int(f,'x',0,pi/2), class(yn) %y(1/3)=?yn1=subs(yn,'n',sym(1/3)),vpa(yn1)%或yn=limit(yn,n,1/3),vpa(yn)%或yy=int(sin(x).^(1/3),x,0,pi/2) ,vpa(yy)%p93_22clear, syms x y SS = dsolve('Dy*y/5+x/4=0','x')ezplot(subs(S(1),'C1',1),[-2,2 -2,2],1), hold onezplot(subs(S(2),'C1',1),[-2,2 -2,2],1)%p93_24[f,g]=dsolve('Df=3*f+4*g','Dg=-4*f+3*g','f(0)=0','g(0)=1','x')第三章2,5,6%122_1a=linspace(0,2*pi,10),b=0:2*pi/9:2*pi%122_2rand('twister',0),A=rand(3,5),B=A>0.5,C=find(B)[ci,cj]=ind2sub(size(A),C) %此法太繁[Ci,Cj]=find(B)%122_5t=linspace(1,10,100)(1) y=1-exp(-0.5.*t).*cos(2.*t),plot(t,y)(2) L=length(t)for k=1:L, yy(k)=1-exp(-0.5*t(k))*cos(2*t(k)), end, plot(t,yy)%122_6clear,format long, rand('twister',1),A=rand(3,3),B=diag(diag(A)),C=A.*(~B)%或C=A-B%或C=triu(A,1)+tril(A,-1)第四章2,5,11,13,17,18%p171_2d=0.5; tt=0:d:10; t=tt+(tt==0)*eps; y=sin(t)./t; s=d*trapz(y) % 计算出积分值ss=d*(cumtrapz(y)) %计算梯形法累计积分并绘积分曲线plot(t,y,t,ss,'r'),hold on%插值法计算y(4.5),并绘出该点yi=interp1(t,ss,4.5), plot(4.5,yi,'r+')%矩形算法计算y(4.5)yy=quad('sin(t)./t',0,4.5)%匿名函数求y(4.5)f=@(x)(int('sin(t)/t',0,x)),vpa(f(4.5))%符号解法syms x t y1y2 y1i, y1=sin(t)./t, y1i=int(y1,t,0,x), y2=subs(y1i,x,4.5)hold on ,plot(4.5,y2,'*m')%此法可用,但有警告性提示tt=0:0.1:10;for i=1:101q(i)=quad('sin(t)./t',0,tt(i));endplot(t, q)y=quad('sin(t)./t',0,4.5)%p171_5x1=-5;x2=5;yx=@(t)((sin(5*t)).^2.*exp(0.06*t.^2)-1.5.*t.*cos(2*t)+1.8.*abs(t+0.5))[xn0,fval]=fminbnd(yx,x1,x2)%绘函数图并标出最小值点t=x1:0.1:x2; plot(t,yx(t)),hold on ,plot(xn0,fval,'r*')%p171_11A=magic(4)b=[1;2;3;4]x=A\b%p171_13S=solve('sin(x-y)=0','cos(x+y)=0','x','y')S.x, S.y%p172_17format rat, NX=conv([3,0,1,0],[1,0,0,0.5]), DX=conv([1,2,-2],[5,2,0,1])[q,r]=deconv(NX,DX), cq='商多项式为 ';cr='余多项式为 ';disp([cq,poly2str(q,'s')]),disp([cr,poly2str(r,'s')])%验算qp2=conv(q,DX), pp1=qp2+r, pp1==NX%或验算norm(conv(D,Q)+r-N,'fro')%p172_18load prob_data418, who,xP=polyfit(x,y,5), Pt=poly2str(P,t)xx=-1:0.01:4, yy=polyval(P,xx), plot(xx,yy,x,y,'*r')legend('拟合曲线','原始曲线','Location','SouthEast')%解法二(拟合的基本思想)x0=x';y0=y';m=length(x);n=5;X=zeros(m,n+1);for k=1:nX(:,n-k+1)=(x0.^k);endX(:,n+1)=ones(m,1);aT=(X\y0)',(P==aT)<eps第五章1,2,5,6,7%p213_1t=2*pi*(0:199)/199; a=4;b=2;x=a*cos(t);y=b*sin(t); plot(x,y,'r.','MarkerSize',15)%p213_2t = 0:.01:2*pi; P=1-cos(t); polar(t,P,’r’), title('P=1-cos\theta')%p215_5t=4*pi*(0:100)/100; x=sin(t);y=cos(t);z=t; plot3(x,y,z,'g','LineWidth',3),box on%p216_6x=-3:0.1:3;y=x;[X,Y]=meshgrid(x,y);Z=4.*X.*exp(-X.^2-Y.^2);mesh(X,Y,Z)colormap(hot),shading interp,hidden off%p216_7x=4*pi*(-50:50)/50;y=x;[X,Y]=meshgrid(x,y);Z=sin(X+Y)./(X+Y);surf(X,Y,Z)view([21,32]) %图形界面旋转图形,认为合适后记下方位角和俯视角,再写出命令shading interp第六章1,2,3%p239_1%for循环S=0.2;SUM=1; ticfor i=0:1000000SUM=SUM+S; S=S*0.2;endt=toc, SUM%while循环S=0.2; SUM=1; i=0; ticwhile i<=1000000SUM=SUM+S; S=S*0.2; i=i+1;endt=toc, SUM%symsyms i; tic, SUM=vpa(symsum(0.2.^i,0,1000000),6), t=toc %向量法(用时最短)N=1000000; S=zeros(1,N);tick=0:N;S=0.2.^k;t=toc, sum(S)%函数法function y=logsum(x,n)y=(1-x.^n)./(1-x);调用:loysum(0.2,1000001)%p239_2function funp239_2(n)%function name: p239_2.m% n The number of sides, while n>=3% draw circle, while no parameter% input "n" again ,while n<3 or not natural numberif nargin~=0 & (n<0 | fix(n)~=n)disp('input parameter error! Please try again.') elseswitch nargincase 0a=100;t=(0:a)/a*2*pi;ss=exp(i*t);plot(real(ss),imag(ss))axis equal, axis offotherwiseif n==1 | n==2,disp('please input n>=3')elset=(0:n)/n*2*pi;ss='exp(i*t)';se=eval(ss);plot(real(se),imag(se))axis equal, axis offendendend%调用funp239_2(59),funp239_2(-2)%p239_2另解function fun(N)if nargin~=0 &(N<3 | fix(N)~=N)disp('input parameter error!')elseif nargin==0t=-pi:pi/100:pi;plot(sin(t),cos(t)), axis equal square elset=-pi:2*pi/(N):pi;plot(sin(t),cos(t)), axis equal square end%p239_2(学生)解function aa(n)if nargin==0t=0:pi/100:2*pi; x=exp(i*t); str='Circle';elseif(nargin~=0)&(n<=2)error('输入边数少了')endif n-round(n)~=0error('输入了非自然数')endt=(0:n)/n*2*pi;x=exp(i*t);str=['Poly gon nit ',int2str(n),' edges']endplot(real(x),imag(x),'r','LineWidth',4)title(str)axis square image offshg%p240_3%p240_3_@(x)(exp),匿名函数表达式法y0=@(x)(-exp(-x)*abs(sin(cos(x)))); fplot(y0,[-5,+4])或ezplot(y0,[-5,4])[x,fval]=fminbnd(y0,-1,1); hold on, plot(x,fval,'*r')%p240_3_function子程序函数句柄法function y=funp240_3(x)y=-exp(-x)*abs(sin(cos(x)));%main programhh=@funp240_3; fplot(hh,[-5,+4]), [x,fval]=fminbnd(hh,-1,1); hold on, plot(x,fval,'*r')%p240_3_inline内联函数法y1=inline('-exp(-x)*abs(sin(cos(x)))'); [x,fval]=fminbnd(y1,-1,1)x0=[-5,4]; fplot(y1,x0), hold on, plot(x,fval,'*r')%p240_3_char字符串法y2='-exp(-x)*abs(sin(cos(x)))'; [x,fval]=fminbnd(y2,-1,1)x0=[-5,4]; ezplot(y2,x0), hold on, plot(x,fval,'*r')第七章%p263_1.m and p263_1.mdlsimulinkxFinal %integer values%p263_2.mdl。
习题 11. 执行下列指令,观察其运算结果, 理解其意义: (1) [1 2;3 4]+10-2i(2) [1 2; 3 4].*[0.1 0.2; 0.3 0.4] (3) [1 2; 3 4].\[20 10;9 2] (4) [1 2; 3 4].^2 (5) exp([1 2; 3 4]) (6)log([1 10 100]) (7)prod([1 2;3 4])(8)[a,b]=min([10 20;30 40]) (9)abs([1 2;3 4]-pi)(10) [1 2;3 4]>=[4,3;2 1](11)find([10 20;30 40]>=[40,30;20 10])(12) [a,b]=find([10 20;30 40]>=[40,30;20 10]) (提示:a 为行号,b 为列号) (13) all([1 2;3 4]>1) (14) any([1 2;3 4]>1) (15) linspace(3,4,5) (16) A=[1 2;3 4];A(:,2)2. 执行下列指令,观察其运算结果、变量类型和字节数,理解其意义: (1) clear; a=1,b=num2str(a),c=a>0, a= =b, a= =c, b= =c (2) clear; fun='abs(x)',x=-2,eval(fun),double(fun)3. 本金K 以每年n 次,每次p %的增值率(n 与p 的乘积为每年增值额的百分比)增加,当增加到rK 时所花费的时间为)01.01ln(ln p n rT +=(单位:年)用MA TLAB 表达式写出该公式并用下列数据计算:r =2, p =0.5, n =12.4.已知函数f (x )=x 4-2x 在(-2, 2)内有两个根。
取步长h =0.05, 通过计算函数值求得函数的最小值点和两个根的近似解。