2022年MATLAB程序设计教程课后答案教学文稿.pdf
资料收集于网络,如有侵权请联系网站删除word 可编辑MATLAB第二版课后答案unit3-8 unit3 实验指导1、 n=input(请输入一个三位数:); a=fix(n/100); b=fix(n-a*100)/10); c=n-a*100-b*10; d=c*100+b*10+a 2(1)n=input(请输入成绩 ); switch n case num2cell(90:100) p=A; case num2cell(80:89) p=B; case num2cell(70:79) p=C; case num2cell(60:69) p=D; otherwise p=E; end price=p (2)n=input(请输入成绩 ); if n=90&n=80&n=70&n=60&na a=n(m); elseif n(m)b b=n(m); end end max=a min=b 法 2 n=1,5,56,4,3,476,45,6,3,76,45,6,4,3,6,4,23,76,908,6; min=min(n) max=max(n) 4 b=-3.0:0.1:3.0; for n=1:61 a=b(n); y(n)=(exp(0.3*a)-exp(-0.3*a)/2*sin(a+0.3)+log(0.3+a)/2); end y 5 y1=0; y2=1; n=input( 请输入 n 的值: ); for i=1:n y1=y1+1/i2; y2=y2*(4*i*i)/(2*i-1)*(2*i+1); end y1 y2 6 A=1,1,1,1,1,1;2,2,2,2,2,2;3,3,3,3,3,3;4,4,4,4,4,4;5,5,5,5,5,5;6,6,6,6,6,6; n=input( 请输入 n 的值 :); if n=0 disp(A(n,:); elseif n0 disp(lasterr); else disp(A(6,:); disp(lasterr); end 7(1)f=; 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 2 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑for n=1:40 f(n)=n+10*log(n2+5); end y=f(40)/(f(30)+f(20) (2)f=;a=0; for n=1:40 f(n)=a+n*(n+1); a=f(n); end y=f(40)/(f(30)+f(20) 8 y=0; m=input( 输入 m 的值 :); n=input( 输入 n 值: ); for i=1:n y=y+im; end y * function s=shi8_1(n,m) s=0; for i=1:n s=s+im; end * shi8_1(100,1)+shi8_1(50,2)+shi8_1(10,1/2) 思考练习2 N=1,2,3,4,5; 2.*N N./2 1./N 1./N.2 3 s=fix(100*rand(1,20)*9/10+10) y=sum(s)/20 j=0; for i=1:20 if s(i)y&rem(s(i),2)=0 j=j+1; A(j)=s(i); else continue; end 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 3 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑end A 4 y1=0;y2=0; n=input( 请输入 n 的值: ); for i=1:n y1=y1+-(-1)i/(2*i-1); y2=y2+1/4i; end y1 y2 unit4 实验指导1(1)x=-10:0.05:10; y=x-x.3./6; plot(x,y) (2)x=-10:0.5:10; ezplot(x2+2*y2-64,-8,8); grid on; 2 t=-pi:pi/10:pi; y=1./(1+exp(-t); subplot(2,2,1);bar(t,y); title(条形图 (t,y); axis(-pi,pi,0,1); subplot(2,2,2); stairs(t,y,b); title(阶梯图 (t,y); axis(-pi,pi,0,1); subplot(2,2,3); stem(t,y,k); title(杆图 (t,y); axis(-pi,pi,0,1); subplot(2,2,4); loglog(t,y,y); title(对数坐标图 (t,y); 3(1)t=0:pi/50:2*pi; r=5.*cos(t)+4; polar(t,r); title(rho=5*costheta+4); (2)精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 4 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑t=-pi/3:pi/50:pi/3; r=5.*(sin(t).2)./cos(t); polar(t,r); 4(1)t=0:pi/50:2*pi; x=exp(-t./20).*cos(t); y=exp(-t./20).*sin(t); z=t; plot3(x,y,z); grid on; (2)x,y=meshgrid(-5:5); z=zeros(11)+5; mesh(x,y,z); shading interp; 5 x,y,z=sphere(20); surf(x,y,z); axis off; shading interp; m=moviein(20); for i=1:20 axis(-i,i,-i,i,-i,i) m(:,i)=getframe; end movie(m,4); 思考练习2(1)x=-5:0.1:5; y=(1./(2*pi).*exp(-(x.2)/2); plot(x,y); (2)t=-2*pi:0.1:2*pi; x=t.*sin(t); y=t.*cos(t); plot(x,y); grid on; 3 t=0:pi/1000:pi; x=sin(3.*t).*cos(t); y1=sin(3.*t).*sin(t); y2=2.*x-0.5; plot(x,y1,k,x,y2); hold on; 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 5 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑k=find(abs(y1-y2)0.5) n=n+1; end end n y=n/30000 5 p=45,74,54,55,14;78,98,45,74,12;87,98,85,52,65 M,S=max(p) N,H=min(p) junzhi=mean(p,1) fangcha=std(p,1,1) zong=sum(p,2); Max,wei=max(zong) Min,wei=min(zong) zcj,xsxh=sort(zong,descend) 6 x=1:10:101; y=0,1.0414,1.3222,1.4914,1.6128,1.7076,1.7853,1.8513,1.9085,1.9590,2.0043; p,s=polyfit(x,y,5) a=1:5:101; y1=polyval(p,a); plot(x,y,:o,a,y1,-*) unit6 实验指导1 A=1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6; 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 8 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑p=0.95,0.67,0.52; x=Ap A=1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6; p=0.95,0.67,0.53; x=Ap cond(A) 2(1)x1=fzero(funx1,-1) function fx=funx1(x) fx=x41+x3+1; (2)x2=fzero(funx2,0.5) function fx=funx2(x) fx=x-sin(x)/x; (3)options=optimset(Display,off); x=fsolve(fun3,1,1,1,options) q=fun3(x) function q=fun3(p) x=p(1); y=p(2); z=p(3); q(1)=sin(x)+y2+log(z)-7; q(2)=3*x+2y-z3+1; q(3)=x+y+z-5; 3(1)t0=0; tf=5; y0=1; t,y=ode23(fun4,t0,tf,y0); t y function yp=fun4(t,y) yp=-(1.2+sin(10*t)*y; (2)t0=0; tf=5; y0=1; t,y=ode23(fun5,t0,tf,y0); t y function yp=fun5(t,y) 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 9 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑yp=cos(t)-y/(1+t2); 4 x=fminbnd(mymin,0,2); -mymin(x) function fx=mymin(x) fx=-(1+x.2)/(1+x.4); 5 options=optimset(Display,off); x,fval=fmincon(fun6,0,0,0,a,b,lb,ub) -fval function f=fun6(x) f=-(sqrt(x(1)+(400-x(1)*1.1+(sqrt(x(2)+(400-x(1)*1.1-x(2)*1.1+sqrt(3)+(400-x(1)*1.1-x(2)*1.1-x(3)*1.1+sqrt(x(x4); 思考练习1(1)A=2,3,5;3,7,4;1,-7,1; B=10,3,5; C1=inv(A)*B C2=AB L,U=lu(A); x=U(LB) (2)A=6,5,-2,5;9,-1,4,-1;3,4,2,-2;3,-9,0,2; B=-4,13,1,11; C1=inv(A)*B C2=AB L,U=lu(A); x=U(LB) 2(1)x1=fzero(funx1,1.5) function fx=funx1(x) fx=3*x+sin(x)-exp(x); (2)x1=fzero(funx2,1) function fx=funx2(x) fx=x-1/x+5; (3)options=optimset(Display,off); x=fsolve(fun3,3,0,options) q=fun3(x) function q=fun3(p) x=p(1); 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 10 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑y=p(2); q(1)=x2+y2-9; q(2)=x+y-1; 3(1)t0=0; tf=5; y0=0,1; t,y=ode45(vdpol,t0,tf,y0); t,y function ydot=vdpol(t,y); ydot(1)=(2-3*y(2)-2*t*y(1)./(1+t2); ydot(2)=y(1); ydot=ydot; (2)t0=0; tf=5; y0=1;0;2; t,y=ode45(vdpoll,t0,tf,y0); t,y function ydot=vdpoll(t,y); ydot(1)=cos(t)-y(3)./(3+sin(t)+5*y(1).*cos(2*t)/(t+1).2)-y(2); ydot(2)=y(1); ydot(3)=y(2); ydot=ydot; 4 x=fminbnd(mymin,0,pi); -mymin(x) function fx=mymin(x) fx=-sin(x)-cos(x.2); 5 x,y1=fminbnd(mymax,0,1.5); -y1 function fx=mymax(x); fx=-(9*x+4*x.3-12*x.2); unit7 实验指导1(1)format long fx=inline(sin(x)./x); I,n=quadl(fx,0,2,1e-10) (2)format long fx=inline(1./(x-0.3).2+0.01)-1./(x-0.9).2+0.04)-6); 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 11 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑I,n=quad(fx,0,1,1e-10) 2(1)global ki; ki=0; I=dblquad(fxy,0,1,0,1) ki (2)f=inline(abs(cos(x+y),x,y); I=dblquad(f,0,pi,0,pi) 3 X=0.3:0.2:1.5; F=0.3895,0.6598,0.9147,1.1611,1.3971,1.6212,1.8325; trapz(X,F) 4 p=0:pi/5:2*pi; for n=1:3 n DX=diff(sin(p),n) end 5 f=inline(sin(x)./(x+cos(2.*x); g=inline(cos(x).*(x+cos(2*x)-sin(x).*(1-2.*sin(2*x)/(x+cos(2.*x).2); x=-pi:0.01:pi; p=polyfit(x,f(x),5); dp=polyder(p); dpx=polyval(dp,x); %求 dp 在假设点的函数值dx=diff(f(x,3.01)/0.01; %直接对 f(x) 求数值导数gx=g(x); %求函数 f 的导函数g 在假设点的导数plot(x,dpx,x,dx,.,x,gx,-); %作图思考练习2 format long fx=inline(1./(1+x.2); I,n=quad(fx,-Inf,Inf,1e-10) I,n=quadl(fx,-Inf,Inf,1e-10) x=-100000:100000; y=1./(1+x.2); trapz(x,y) format short 3(1) format long fx=inline(log(1+x)./(1+x.2); I,n=quad(fx,0,1,1e-10) 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 12 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑(2) format long fx=inline(sqrt(cos(t.2)+4*sin(2*t).2)+1); I,n=quad(fx,0,2*pi,1e-10) 4 f=inline(4.*x.*z.*exp(-z.2.*y-x.2); I=triplequad(f,0,pi,0,pi,0,1) 5 f=inline(sin(x); g=inline(cos(x); x=0:0.01:2*pi; p=polyfit(x,f(x),5); dp=polyder(p); dpx=polyval(dp,x); dx=diff(f(x,2*pi+0.01)/0.01; gx=g(x); plot(x,dpx,x,dx,.,x,gx,-) unit8 实验指导1 syms x y; s=x4-y4; factor(s) factor(5135) 2 syms x; f=(x-2)/(x2-4); limit(f,x,2) sym x; f=(sqrt(pi)-sqrt(acos(x)/sqrt(x+1); limit(f,x,-1,right) 3 sym x; f=sin(1/x); diff(f,x) diff(f,x,2) sym x; 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 13 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑f=(1-cos(2*x)/x; diff(f,x) diff(f,x,2) 4 sym x; f=sqrt(exp(x)+1); int(f,x) syms x y; f=x/(x+y); int(f,y) sym x; f=exp(x)*(1+exp(x)2; int(f,x,0,log(2) sym x; f=x*log(x); int(f,x,1,exp(1) 5 sym x; s=symsum(-1)(x+1)/x,1,Inf) sym y; z=symsum(y(2*y-1)/(2*y-1),1,Inf) 6 sym x; f1=(exp(x)+exp(-x)/2; f2=sqrt(x3-2*x+1); taylor(f1,x,5,0) taylor(f2,x,6,0) 7 syms x y a; x=solve(x3+a*x+1=0,x) x=solve(sin(x)+2*cos(x)-sqrt(x)=0,x) x y=solve(log(x/y)=9,exp(x+y)=3,x,y) 8 syms n; x,y=dsolve(x*(D2y)+(1-n)*(Dy)+y=0,y(0)=0,Dy(0)=0,x) 思考练习2 syms x B1 B2 a b 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 14 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑s1=2*(cos(x)2)-(sin(x)2; s2=sin(B1)*cos(B2)-cos(B1)*sin(B2); s3=sqrt(a+sqrt(a2-b)/2)+sqrt(a-sqrt(a2-b)/2); s4=(4*x2+8*x+3)/(2*x+1); h1=simplify(s1) h2=simplify(s2) h3=simplify(s3) h4=simplify(s4) 3 syms x a; f=abs(x)/x; limit(f,x,0,left) f=(x+a/x)x; limit(f,x,inf) 4 syms x y m f=sqrt(x+sqrt(x+sqrt(x); m=diff(f,x) diff(m,x) syms x y; f=x+y-sqrt(x2+y2); z1=diff(f,x) z2=diff(f,y) 5 syms x y; f=x+y-sqrt(x2+y2); z1=diff(f,x) z2=diff(f,y) sym x; f=1/(asin(x)2*(1-x2)(1/2); int(f) 6 syms x f=1/(1+x); int(f,0,4) 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 15 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑sym x; f=x3*sin(x)2/(x6+2*x4+1); int(f,-1,1) sym x; f=x3*sin(x)2/(x6+2*x4+1); int(f,-1,1) 7 syms n s=symsum(1/4n,1,inf) sym n; s=symsum(n+1)/n)(1/2),1,inf) eval(y) 8 syms x f=tan(x); taylor(f,x,3,0) syms x f=sin(x)2; taylor(f,x,5,0) 9 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 16 页,共 17 页 - - - - - - - - - - 资料收集于网络,如有侵权请联系网站删除word 可编辑syms x x=solve(log(1+x)-5/(1+sin(x)=2,x) syms x y z x y z=solve(4*x2/(4*x2+1)=y,4*y2/(4*y2+1)=z,4*z2/(4*z2+1)=x,x,y,z) 10 x ,y=dsolve(Dx=3*x+4*y,Dy=5*x-7*y,x(0)=0,y(0)=1,t) 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 17 页,共 17 页 - - - - - - - - - -