MATLAB编程练习(含答案很好的).doc_第1页
MATLAB编程练习(含答案很好的).doc_第2页
MATLAB编程练习(含答案很好的).doc_第3页
MATLAB编程练习(含答案很好的).doc_第4页
MATLAB编程练习(含答案很好的).doc_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

001双峰曲线图:z=peaks(40);mesh(z);surf(z)002解方程:A=3,4,-2;6,2,-3;45,5,4; B=14;4;23; root=inv(A)*B003傅里叶变换load mtlb ;subplot(2,1,1);plot(mtlb); title(原始语音信息); y=fft(mtlb); subplot(2,1,2); yy=abs(y); plot(yy); title(傅里叶变换)004输入函数:a=input(How many applesn,s)005输出函数a=1 2 3 4 ;5 6 7 8;12 23 34 45;34 435 23 34a = 1 2 3 4 5 6 7 8 12 23 34 45 34 435 23 34disp(a)a = 1 2 3 4 5 6 7 8 12 23 34 4534 435 23 34b=input(how many peoplen ,s)how many peopletwo peopleb =two people disp(b)two people006求一元二次方程的根a=1;b=2;c=3;d=sqrt(b2-4*a*c);x1=(-b+d)/(2*a)x1 = -1.0000 + 1.4142i x2=(-b-d)/(2*a)x2 = -1.0000 - 1.4142i007求矩阵的相乘、转置、存盘、读入数据A=1 3 5 ;2 4 6;-1 0 -2;-3 0 0; B=-1 3;-2 2;2 1; C=A*BC = 3 14 2 20 -3 -5 3 -9 C=CC = 3 2 -3 3 14 20 -5 -9 save mydat C clear load mydat C008编写数学计算公式:A=2.1;B=-4.5;C=6;D=3.5;E=-5;K=atan(2*pi*A)+E/(2*pi*B*C)/D)K = 1.3121009A=1 0 -1;2 4 1;-2 0 5; B=0 -1 0;2 1 3;1 1 2; H=2*A+BH = 2 -1 -2 6 9 5 -3 1 12 M=A2-3*BM = 3 3 -6 2 13 -2 -15 -3 21 Y=A*BY = -1 -2 -2 9 3 14 5 7 10 R=B*AR = -2 -4 -1 -2 4 14 -1 4 10 E=A.*BE = 0 0 0 4 4 3 -2 0 10 W=ABW = 0.3333 -1.3333 0.6667 0.2500 1.0000 0.2500 0.3333 -0.3333 0.6667 P=A/BP = -2.0000 3.0000 -5.0000 -5.0000 3.0000 -4.0000 7.0000 -9.0000 16.0000 Z=A.BWarning: Divide by zero.Z = 0 -Inf 0 1.0000 0.2500 3.0000 -0.5000 Inf 0.4000 D=A./BWarning: Divide by zero.D = Inf 0 -Inf 1.0000 4.0000 0.3333 -2.0000 0 2.5000010a=4.96;b=8.11; M=exp(a+b)/log10(a+b)M = 4.2507e+005011求三角形面积:a=9.6;b=13.7;c=19.4; s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)area = 61.1739012逻辑运算A=-1 0 -6 8;-9 4 0 12.3;0 0 -5.1 -2;0 -23 0 -7; B=A(:,1:2)B = -1 0 -9 4 0 0 0 -23 C=A(1:2,:)C = -1.0000 0 -6.0000 8.0000 -9.0000 4.0000 0 12.3000 D=BD = -1 -9 0 0 0 4 0 -23 A*Bans = 1.0000 -184.0000 -27.0000 -266.9000 0 46.0000 207.0000 69.0000 C C&Dans = 1 0 0 0 0 1 0 1 C|Dans = 1 1 1 1 1 1 0 1 C|Dans = 0 1 1 1 1 0 1 0013矩阵运算练习:A=8 9 5;36 -7 11;21 -8 5A = 8 9 5 36 -7 11 21 -8 5 BB = -1 3 -2 2 0 3 -3 1 9 RT=A*BRT = -5 29 56 -83 119 6 -52 68 -21 QW=A.*BQW = -8 27 -10 72 0 33 -63 -8 45 ER=A3ER = 6272 3342 2944 15714 -856 5260 8142 -1906 2390 BF=A.3BF = 512 729 125 46656 -343 1331 9261 -512 125 A/Bans = 3.1341 4.9634 -0.4024 -1.2561 12.5244 -3.2317 -1.9878 6.4512 -2.0366 EKV=BAEKV = 10.7195 -1.2683 3.5244 9.4756 1.5854 3.7195 4.8537 -1.4878 1.3171 KDK=A,BKDK = 8 9 5 -1 3 -2 36 -7 11 2 0 3 21 -8 5 -3 1 9 ERI=A;BERI = 8 9 5 36 -7 11 21 -8 5 -1 3 -2 2 0 3 -3 1 9014一般函数的调用:A=2 34 88 390 848 939; S=sum(A)S = 2301 min(A)ans = 2 EE=mean(A)EE = 383.5000 QQ=std(A)QQ = 419.3794 AO=sort(A)AO = 2 34 88 390 848 939 yr=norm(A)yr = 1.3273e+003 RT=prod(A)RT = 1.8583e+012 gradient(A)ans = 32.0000 43.0000 178.0000 380.0000 274.5000 91.0000 max(A)ans = 939 median(A)ans = 239 diff(A)ans = 32 54 302 458 91 length(A)ans = 6 sum(A)ans = 2301 cov(A)ans = 1.7588e+005015矩阵变换:A=34 44 23;8 34 23;34 55 2A = 34 44 23 8 34 23 34 55 2 tril(A)ans = 34 0 0 8 34 0 34 55 2 triu(A)ans = 34 44 23 0 34 23 0 0 2 diag(A)ans = 34 34 2norm(A)ans = 94.5106 rank(A)ans = 3 det(A)ans = -23462 trace(A)ans = 70 null(A)ans = Empty matrix: 3-by-0 eig(A)ans = 80.1587 12.7671 -22.9257 poly(A)ans = 1.0e+004 * 0.0001 -0.0070 -0.1107 2.3462 logm(A)Warning: Principal matrix logarithm is not defined for A with nonpositive real eigenvalues. A non-principal matrix logarithm is returned. In funm at 153 In logm at 27ans = 3.1909 + 0.1314i 1.2707 + 0.1437i 0.5011 - 0.2538i 0.4648 + 0.4974i 3.3955 + 0.5438i 0.1504 - 0.9608i 0.2935 - 1.2769i 0.8069 - 1.3960i 3.4768 + 2.4663i fumn(A)? Undefined command/function fumn. inv(A)ans = 0.0510 -0.0502 -0.0098 -0.0326 0.0304 0.0255 0.0305 0.0159 -0.0343 cond(A)ans = 8.5072 chol(A)? Error using = cholMatrix must be positive definite. lu(A)ans = 34.0000 44.0000 23.0000 0.2353 23.6471 17.5882 1.0000 0.4652 -29.1816 pinv(A)ans = 0.0510 -0.0502 -0.0098 -0.0326 0.0304 0.0255 0.0305 0.0159 -0.0343 svd(A)ans = 94.5106 22.3456 11.1095 expm(A)ans = 1.0e+034 * 2.1897 4.3968 1.9382 1.3154 2.6412 1.1643 1.8782 3.7712 1.6625 sqrtm(A)ans = 5.2379 + 0.2003i 3.4795 + 0.2190i 1.8946 - 0.3869i 0.5241 + 0.7581i 5.1429 + 0.8288i 2.0575 - 1.4644i 3.0084 - 1.9461i 4.7123 - 2.1276i 2.1454 + 3.7589i016多项式的计算:A=34 44 23;8 34 23;34 55 2A = 34 44 23 8 34 23 34 55 2 P=poly(A)P = 1.0e+004 * 0.0001 -0.0070 -0.1107 2.3462 PPA=poly2str(P,X)PPA = X3 - 70 X2 - 1107 X + 23462017多项式的运算:p=2 6 8 3;w=32 56 0 2; m=conv(p,w)m = 64 304 592 548 180 16 6 q,r=deconv(w,p)q = 16r = 0 -40 -128 -46 dp=polyder(w)dp = 96 112 0 num,den=polyder(w,p)num = 80 512 724 312 -16den = 4 24 68 108 100 48 9 b=polyfit(p,w,4)Warning: Polynomial is not unique; degree = number of data points. In polyfit at 74b = -0.6704 9.2037 -32.2593 0 98.1333 r=roots(p)r = -1.2119 + 1.0652i -1.2119 - 1.0652i -0.5761 018求多项式的商和余p=conv(1 0 2,conv(1 4,1 1)p = 1 5 6 10 8 q=1 0 1 1q = 1 0 1 1 w,m=deconv(p,q)w = 1 5m = 0 0 5 4 3 cq=w;cr=m; disp(cr,poly2str(m,x) 5 x2 + 4 x + 3 disp(cq,poly2str(w,x) x + 5019将分式分解a=1 5 6;b=1; r,p,k=residue(b,a)r = -1.0000 1.0000p = -3.0000 -2.0000k = 020计算多项式:a=1 2 3;4 5 6;7 8 9; p=3 0 2 3; q=2 3; x=2; r=roots(p)r = 0.3911 + 1.0609i 0.3911 - 1.0609i -0.7822 p1=conv(p,q)p1 = 6 9 4 12 9 p2=poly(a)p2 = 1.0000 -15.0000 -18.0000 -0.0000 p3=polyder(p)p3 = 9 0 2 p4=polyval(p,x)p4 = 31021求除式和余项:q,r=deconv(conv(1 0 2,1 4),1 1 1)022字符串的书写格式:s=students =student name=mary; s1=name s s1 =marystudent s3=name blanks(3);ss3 =mary student023交换两个数:clearclca=1 2 3 4 5;b=6 7 8 9 10;c=a;a=b;b=c;ab24 If语句n=input(enter a number,n=);if nb max=a;else max=b;endmax026三个数按照由大到小的顺序排列:A=15;B=24;C=45;if AB T=A;A=B;B=T;elseif AC T=A;A=C;C=T; elseif BC T=B;B=C;C=T; end A B C027建立一个收费优惠系统:price=input(please jinput the price : price=)switch fix(price/100) case0,1 rate =0; case2,3,4 rate =3/100; case num2cell(5:9) rate=5/100; case num2cell(10:24) rate=8/100; case num2cell(25:49) rate=10/100; otherwise rate=14/100;endprice=price*(1-rate)028:while循环语句i=0;s=0;while i=1212 s=s+i; i=i+1;ends029,用for循环体语句:sum=0;for i=1:1.5:100; sum=sum+i;endsum030循环的嵌套s=0;for i=1:1:6; for j=1:1:8; s=s+ij; end;end;s031continue 语句的使用:for i=100:120; if rem(i,7)=0; continue; end; iend032x=input (输入X的值 x=)if x1&x2 y=x2-1;else y=x2-2*x+1;endy033求阶乘的累加和sum=0;temp=1;for n=1:10; temp=temp*n; sum=sum+temp;endsum034对角线元素之和sum=0;a=1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16;for i=1:4; sum=sum+a(i,i);endsum035用拟合点绘图A=12 15.3 16 18 25;B=50 80 118 125 150.8;plot(A,B)036绘制正玄曲线:x=0:0.05:4*pi;y=sin(x);plot(x,y)037绘制向量x=1 2 3 4 5 6;7 8 9 10 11 12;13 14 15 16 17 18plot(x)x=0 0.2 0.5 0.7 0.6 0.7 1.2 1.5 1.6 1.9 2.3plot(x)x=0:0.2:2*piy=sin(x)plot(x,y,m:p)038在正弦函数上加标注:t=0:0.05:2*pi;plot(t,sin(t)set(gca,xtick,0 1.4 3.14 56.28)xlabel(t(deg)ylabel(magnitude(v)title(this is a example ()rightarrow 2pi)text(3.14,sin(3.14),leftarrow this zero forpi)039添加线条标注x=0:0.2:12;plot(x,sin(x),-,x,1.5*cos(x),:);legend(First,Second,1)040使用hold on 函数x=0:0.2:12;plot(x,sin(x),-);hold onplot(x,1.5*cos(x),:);041一界面多幅图x=0:0.05:7;y1=sin(x);y2=1.5*cos(x);y3=sin(2*x);y4=5*cos(2*x);subplot(221);plot(x,y1);title(sin(x)subplot(222);plot(x,y2);title(cos(x)subplot(223);plot(x,y3);title(sin(2x)subplot(224);plot(x,y4);title(cos(2x)042染色效果图x=0:0.05:7;y1=sin(x);y2=1.5*cos(x);y3=sin(2*x);y4=5*cos(2*x);subplot(221);plot(x,y1);title(sin(x);fill(x,y1,r)subplot(222);plot(x,y2);title(cos(x);fill(x,y2,b)subplot(223);plot(x,y3);title(sin(2x);fill(x,y3,k)subplot(224);plot(x,y4);title(cos(2x);fill(x,y4,g)043特殊坐标图clcy=0,0.55,2.5,6.1,8.5,12.1,14.6,17,20,22,22.1subplot(221);plot(y);title(线性坐标图);subplot(222);semilogx(y);title(x轴对数坐标图);subplot(223);semilogx(y);title(y轴对数坐标图);subplot(224);loglog(y);title(双对数坐标图)t=0:0.01:2*pi;r=2*cos(2*(t-pi/8);polar(t,r)044特殊函数绘图:fplot(cos(tan(pi*x),-0.4,1.4)fplot(sin(exp(pi*x),-0.4,1.4)045饼形图与条形图:x=8 20 36 24 12;subplot(221);pie(x,1 0 0 0 1);title(饼图);subplot(222);bar(x,group);title(垂直条形图);subplot(223);bar(x,stack);title(累加值为纵坐标的垂直条形图);subplot(224);barh(x,group);title(水平条形图);046梯形图与正弦函数x=0:0.1:10;y=sin(x);subplot(121);stairs(x);subplot(122);stairs(x,y);047概率图x=randn(1,1000);y=-2:0.1:2;hist(x,y)048向量图:x=-2+3j,3+4j,1-7j;subplot(121);compass(x);rea=-2 3 1;imag=3 4 -7;subplot(122);feather(rea,imag);049绘制三维曲线图:z=0:pi/50:10*pi;x=sin(z);y=cos(z);plot3(x,y,z)x=-10:0.5:10;y=-8:0.5:8;x,y=meshgrid(x,y);z=sin(sqrt(x.2+y.2)./sqrt(x.2+y.2);subplot(221);mesh(x,y,z);title(普通一维网格曲面);subplot(222);meshc(x,y,z);title(带等高线的三维网格曲面);subplot(223);meshz(x,y,z);title(带底座的三维网格曲面);subplot(224);surf(x,y,z);title(充填颜色的三维网格面)050 带网格二维图x=0:pi/10:2*pi;y1=sin(x);y2=cos(x);plot(x,y1,r+-,x,y2,k*:)grid onxlabel(Independent Variable x)ylabel(Dependent Variable y1&y2)text(1.5,0.5,cos(x)051各种统计图y=18 5 28 17;24 12 36 14;15 6 30 9;subplot(221);bar(y)x=4,6,8;subplot(222);bar3(x,y)subplot(223);bar(x,y,grouped)subplot(224);bar(x,y,stack)052曲面图x=-2:0.4:2;y=-1:0.2:1;x,y=meshgrid(x,y);z=sqrt(4-x.2/9-y.2/4);surf(x,y,z)grid on053创建符号矩阵e=1 3 5;2 4 6;7 9 11;m=sym(e)符号表达式的计算问题因式分解:syms xf=factor(x3-1)s=sym(sin(a+b);expand(s)syms x tf=x*(x*(x-8)+6)*t;collect(f)syms x f=sin(x)2+cos(x)2;simplify(f)syms xs=(4*x2+8*x+3)/(2*x+1);simplify(s)通分syms x yf=x/y-y/x;m,n=numden(f)嵌套重写syms xf=x4+3*x3-7*x2+12;horner(f)054求极限syms x a;limit(exp(-x),x,0,left)求导数syms x diff(x9+x6)diff(x9+x6,4)055求不定积分与定积分syms x ys=(4-3*x2)2;int(s)int(x/(x+y),x)int(x2/(x+2),x,1,3)double(ans)056函数的变换:syms x ty=exp(-x2);Ft=fourier(y,x,t)fx=ifourier(Ft,t,x)057求解方程syms a b c xs=a*x2+b*x+c;solve(s)syms x y zs1=2*x2+y2-3*z-4;s2=y+z-3;s3=x-2*y-3*z;x,y,z=solve(s1,s2,s3)058求微分方程:y=dsolve(Dy-(t2+y2)/t2/2,t)059求级数和syms x ksymsum(k)symsum(k2-3,0,10)symsum(xk/k,k,1,inf)060泰勒展开式syms xs=(1-x+x2)/(1+x+x2);taylor(s)taylor(s,9)taylor(s,x,12)taylor(s,x,12,5)061练习syms x a;s1=sin(2*x)/sin(5*x);limit(s1,x,0)s2=(1+1/x)(2*x);limit(s2,x,inf)syms xs=x*cos(x);diff(s)diff(s,2)diff(s,12)syms xs1=x4/(1+x2);int(s1)s2=3*x2-x+1int(s2,0,2)syms x y zs1=5*x+6*y+7*z-16;s2=4*x-5*y+z-7;s3=x+y+2*z-2;x,y,z=solve(s1,s2,s3)syms x yy=dsolve(Dy=exp(2*x-y),x)y=dsolve(Dy=exp(2*x-y),y(0)=0,x)n=sym(n);s=symsum(1/n2,n,1,inf)x=sym(x);f=sqrt(1-2*x+x3)-(1-3*x+x2)(1/3);taylor(f,6)062求于矩阵相关的值a=2 2 -1 1;4 3 -1 2;8 5 -3 4;3 3 -2 2adet=det(a)atrace=trace(a)anorm=norm(a)acond=cond(a)arank=rank(a)eiga=eig(a)063矩阵计算A=0.1389 0.6038 0.0153 0.9318;0.2028 0.2772 0.7468 0.4660;0.1987 0.1988 0.4451 0.4186B=var(A)C=std(A)D=range(A)E=cov(A)F=corrcoef(A)064求根及求代数式的值P=4 -3 2 5;x=roots(P)x=3 3.6;F=polyval(P,x)065多项式的和差积商运算:f=1 2 -4 3 -1g=1 0 1g1=0 0 1 0 1f+g1f-g1conv(f,g)q,r=deconv(f,g)polyder(f)066各种插值运算:X=0:0.1:pi/2;Y=sin(X);interp1(X,Y,pi/4)interp1(X,Y,pi/4,nearest)interp1(X,Y,pi/4,spline)interp1(X,Y,pi/4,cubic)067曲线的拟合:X=0:0.1:2*pi;Y=cos(X);p,s=polyfit(X,Y,4)plot(X,Y,K*,X,polyval(p,X),r-)068求函数的最值与0点x=2:0.1:2;x,y=fminbnd(x.3-2*x+1,-1,1)x,y=fzero(x.3-2*x+1,1)069求多项式的表达式、值、及图像y=1 3 5 7 19t=poly(y)x=-4:0.5:8yx=polyval(t,x)plot(x,yx)070数据的拟合与绘图x=0:0.1:2*pi;y=sin(x);p=polyfit(x,y,5);y1=polyval(p,x)plot(x,y,b,x,y1,r)071求代数式的极限:syms xf=sym(log(1+2*x)/sin(3*x);b=limit(f,x,0)072求导数与微分syms xf=sym(x/(cos(x)2);y1=diff(f)y2=int(f,0,1)078划分网格函数x,y=meshgrid(-2:0.01:2,-3:0.01:5);t=x.*exp(-x.2-y.2);px,py=gradient(t,0.05,0.1);td=sqrt(px.2+py.2);subplot(221)imagesc(t)subplot(222)imagesc(td)colormap(gray)079求多次多项方程组的解:syms x1 x2 a ;eq1=sym(x12+x2=a)eq2=sym(x1-a*x2=0)x1 x2=solve(eq1,eq2,x1,x2)v=solve(eq1,eq2)v.x1v.x2an1=x1(1),an2=x1(2)an3=x2(1),an4=x2(2)080求解微分方程:y=dsolve(Dy=-y2+6*y,y(0)=1,x)s=dsolve(Dy=-y2+6*y,y(0)=1,x)u=dsolve(Du=-u2+6*u,u(0)=1)w=dsolve(Du=-u2+6*u,z)u,w=dsolve(Du=-w2+6*w,Dw=sin(z),u(0)=1,w(0)=0,z)v=dsolve(Du=-w2+6*w,Dw=sin(z),u(0)=1,w(0)=0,z)081各种显现隐含函数绘图:f=sym(x2+1)subplot(221)ezplot(f,-2,2)subplot(222)ezplot(y2-x6-1,-2,2,0,10)x=sym(cos(t)y=sym(sin(t)subplot(223)ezplot(x,y)z=sym(t2)subplot(224)ezplot3(x,y,z,0,8*pi)082极坐标图:r=sym(4*sin(3*x)ezpolar(r,0,6*pi)083多函数在一个坐标系内:x=0:0.1:8;y1=sin(x);y2=cos(x);subplot(221)plot(x,y1)subplot(222)plot(x,y1,x,y2)w=2 3;3 1;4 6subplot(223)plot(w)q=4 6:3 5:1 2subplot(224)plot(w,q)084调整刻度图像:x=0:0.1:10;y1=sin(x);y2=exp(x);y3=exp(x).*sin(x);subplot(221)plot(x,y2)subplot(222)loglog(x,y2)subplot(223)plotyy(x,y1,x,y2)085等高线等图形,三维图:t=0:pi/50:10*pi;subplot(2,3,1)plot3(t.*sin(t),t.*cos(t),t.2)grid onx,y=meshgrid(-2:0.1:2)z=x.*exp(-x.2-y.2)subplot(2,3,2)plot3(x,y,z)box offsubplot(2,3,3)meshz(x,y,z)subplot(2,3,4)surf(x,y,z)subplot(2,3,5)contour(x,y,z)subplot(2,3,6)surf(x,y,z)subplot(2,3,5)contour(x,y,z)box offsubplot(2,3,6)contour3(x,y,z)axis off086统计图Y=5 2 1;8 7 3;9 8 6;5 5 5;4 3 2subplot(221)bar(Y)box offsubplot(222)bar3(Y)subplot(223)barh(Y)subplot(224)bar3h(Y)087面积图Y=5 1 2;8 3 7;9 6 8

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论