精讲多练matlab习题

合集下载

MATLAB编程练习(含答案很好的)

MATLAB编程练习(含答案很好的)

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 apples\n','s')005输出函数a=[1 2 3 4 ;5 6 7 8;12 23 34 45;34 435 23 34]a =1 2 3 45 6 7 812 23 34 4534 435 23 34disp(a)a =1 2 3 45 6 7 812 23 34 4534 435 23 34b=input('how many people\n' ,'s')how many peopletwo peopleb =two people>> disp(b)two people>>006求一元二次方程的根a=1;b=2;c=3;d=sqrt(b^2-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 142 20-3 -53 -9>> C=C'C =3 2 -3 314 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 -26 9 5-3 1 12>> M=A^2-3*BM =3 3 -62 13 -2-15 -3 21>> Y=A*BY =-1 -2 -29 3 145 7 10>> R=B*AR =-2 -4 -1-2 4 14-1 4 10>> E=A.*BE =0 0 04 4 3-2 0 10>> W=A\BW =0.3333 -1.3333 0.66670.2500 1.0000 0.25000.3333 -0.3333 0.6667 >> P=A/BP =-2.0000 3.0000 -5.0000-5.0000 3.0000 -4.00007.0000 -9.0000 16.0000>> Z=A.\BWarning: Divide by zero.Z =0 -Inf 01.0000 0.2500 3.0000-0.5000 Inf 0.4000>> D=A./BWarning: Divide by zero.D =Inf 0 -Inf1.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 40 00 -23>> C=A(1:2,:)C =-1.0000 0 -6.0000 8.0000 -9.0000 4.0000 0 12.3000>> D=B'D =-1 -9 0 00 4 0 -23>> A*Bans =1.0000 -184.0000-27.0000 -266.90000 46.0000 207.0000 69.0000>> C<Dans =0 0 1 01 0 0 0>> C&Dans =1 0 0 00 1 0 1>> C|Dans =1 1 1 11 1 0 1>> ~C|~Dans =0 1 1 11 0 1 0013矩阵运算练习:A=[8 9 5;36 -7 11;21 -8 5]A =8 9 536 -7 1121 -8 5>> BB =-1 3 -22 0 3-3 1 9>> RT=A*BRT =-5 29 56-83 119 6-52 68 -21>> QW=A.*BQW =-8 27 -1072 0 33-63 -8 45>> ER=A^3ER =6272 3342 294415714 -856 52608142 -1906 2390 >> BF=A.^3BF =512 729 12546656 -343 13319261 -512 125 >> A/Bans =3.13414.9634 -0.4024-1.2561 12.5244 -3.2317-1.9878 6.4512 -2.0366>> EKV=B\AEKV =10.7195 -1.2683 3.52449.4756 1.5854 3.71954.8537 -1.4878 1.3171>> KDK=[A,B]KDK =8 9 5 -1 3 -236 -7 11 2 0 321 -8 5 -3 1 9 >> ERI=[A;B]ERI =8 9 536 -7 1121 -8 5-1 3 -22 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+005>>015矩阵变换:A=[34 44 23;8 34 23;34 55 2]A =34 44 238 34 2334 55 2>> tril(A)ans =34 0 08 34 034 55 2>> triu(A)ans =34 44 230 34 230 0 2>> diag(A)ans =34342norm(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.158712.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 matrixlogarithm is returned.> In funm at 153In logm at 27ans =3.1909 + 0.1314i 1.2707 + 0.1437i 0.5011 - 0.2538i0.4648 + 0.4974i 3.3955 + 0.5438i 0.1504 - 0.9608i0.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.02550.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.00000.2353 23.6471 17.58821.0000 0.4652 -29.1816>> pinv(A)ans =0.0510 -0.0502 -0.0098-0.0326 0.0304 0.02550.0305 0.0159 -0.0343>> svd(A)ans =94.510622.345611.1095>> expm(A)ans =1.0e+034 *2.1897 4.3968 1.93821.31542.6412 1.16431.8782 3.7712 1.6625>> sqrtm(A)ans =5.2379 + 0.2003i 3.4795 + 0.2190i 1.8946 - 0.3869i0.5241 + 0.7581i 5.1429 + 0.8288i 2.0575 - 1.4644i3.0084 - 1.9461i4.7123 - 2.1276i 2.1454 + 3.7589i >>016多项式的计算:A=[34 44 23;8 34 23;34 55 2]A =34 44 238 34 2334 55 2>> P=poly(A)P =1.0e+004 *0.0001 -0.0070 -0.1107 2.3462>> PPA=poly2str(P,'X')PPA =X^3 - 70 X^2 - 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.5761018求多项式的商和余p=conv([1 0 2],conv([1 4],[1 1]))p =1 5 6 10 8>> q=[1 0 1 1]q =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 x^2 + 4 x + 3>> disp([cq,poly2str(w,'x')])x + 5019将分式分解a=[1 5 6];b=[1];>> [r,p,k]=residue(b,a)r =-1.00001.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.0609i0.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='student's =student>> name='mary';>> s1=[name s]s1 =marystudent>> s3=[name blanks(3);s]s3 =marystudent>>023交换两个数:clearclca=[1 2 3 4 5];b=[6 7 8 9 10];c=a;a=b;b=c;ab24If语句n=input('enter a number,n=');if n<10nend025 if 双分支结构a=input('enter a number ,a=');b=input('enter a number ,b=');if a>bmax=a;elsemax=b;endmax026三个数按照由大到小的顺序排列:A=15;B=24;C=45;if A<BT=A;A=B;B=T;elseif A<CT=A;A=C;C=T;elseif B<CT=B;B=C;C=T;endABC027建立一个收费优惠系统:price=input('please jinput the price : price=') switch fix(price/100)case[0,1]rate =0;case[2,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;otherwiserate=14/100;endprice=price*(1-rate)028:while循环语句i=0;s=0;while i<=1212s=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+i^j;end;end;s031continue 语句的使用:for i=100:120;if rem(i,7)~=0;continue;end;iend032x=input ('输入X的值x=')if x<1y=x^2;elseif x>1&x<2y=x^2-1;elsey=x^2-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 18] plot(x)x=[0 0.2 0.5 0.7 0.6 0.7 1.2 1.5 1.6 1.9 2.3]plot(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 2\pi')text(3.14,sin(3.14),'\leftarrow this zero for\pi')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.1] subplot(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(x^3-1)s=sym('sin(a+b)'); expand(s)syms x tf=x*(x*(x-8)+6)*t; collect(f)syms xf=sin(x)^2+cos(x)^2; simplify(f)syms xs=(4*x^2+8*x+3)/(2*x+1); simplify(s)通分syms x yf=x/y-y/x;[m,n]=numden(f)嵌套重写syms xf=x^4+3*x^3-7*x^2+12; horner(f)054求极限syms x a;limit(exp(-x),x,0,'left')求导数syms xdiff(x^9+x^6)diff(x^9+x^6,4)055求不定积分与定积分syms x ys=(4-3*x^2)^2;int(s)int(x/(x+y),x)int(x^2/(x+2),x,1,3) double(ans)056函数的变换:syms x ty=exp(-x^2);Ft=fourier(y,x,t)fx=ifourier(Ft,t,x)057求解方程syms a b c xs=a*x^2+b*x+c;solve(s)syms x y zs1=2*x^2+y^2-3*z-4;s2=y+z-3;s3=x-2*y-3*z;[x,y,z]=solve(s1,s2,s3)058求微分方程:y=dsolve('Dy-(t^2+y^2)/t^2/2','t')059求级数和syms x ksymsum(k)symsum(k^2-3,0,10)symsum(x^k/k,k,1,inf)060泰勒展开式syms xs=(1-x+x^2)/(1+x+x^2);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=x^4/(1+x^2);int(s1)s2=3*x^2-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/n^2,n,1,inf)x=sym('x');f=sqrt(1-2*x+x^3)-(1-3*x+x^2)^(1/3);taylor(f,6)062求于矩阵相关的值a=[2 2 -1 1;4 3 -1 2;8 5 -3 4;3 3 -2 2]adet=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.4186]B=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 -1]g=[1 0 1]g1=[0 0 1 0 1]f+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 19]t=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('x1^2+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=-y^2+6*y','y(0)=1','x')s=dsolve('Dy=-y^2+6*y','y(0)=1','x')[u]=dsolve('Du=-u^2+6*u','u(0)=1')w=dsolve('Du=-u^2+6*u','z')[u,w]=dsolve('Du=-w^2+6*w,Dw=sin(z)','u(0)=1,w(0)=0','z') v=dsolve('Du=-w^2+6*w,Dw=sin(z)','u(0)=1,w(0)=0','z')081各种显现隐含函数绘图:f=sym('x^2+1')subplot(221)ezplot(f,[-2,2])subplot(222)ezplot('y^2-x^6-1',[-2,2],[0,10])x=sym('cos(t)')y=sym('sin(t)')subplot(223)ezplot(x,y)z=sym('t^2')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);subplot(221)plot(x,y1)subplot(222)plot(x,y1,x,y2)w=[2 3;3 1;4 6]subplot(223)plot(w)q=[4 6:3 5:1 2]subplot(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 on[x,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)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 2]subplot(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;5 5 5;4 2 3];subplot(221)area(Y)grid onset(gca,'Layer','top','XTick',1:5)sales=[51.6 82.4 90.8 59.1 47.0];x=90:94;profits=[19.3 34.2 61.4 50.5 29.4];subplot(222)area(x,sales,'facecolor',[0.5 0.9 0.6], 'edgecolor','b','linewidth',2) hold onarea(x,profits,'facecolor',[0.9 0.85 0.7], 'edgecolor','y','linewidth',2) hold offset(gca,'Xtick',[90:94])set(gca,'layer','top')gtext('\leftarrow 销售量') gtext('利润')gtext('费用')xlabel('年','fontsize',14)088函数的插值:x=0:2*pi;y=sin(x);xi=0:0.1:8;yi1=interp1(x,y,xi,'linear')yi2=interp1(x,y,xi,'nearest') yi3=interp1(x,y,xi,'spline')yi4=interp1(x,y,xi,'cublic')p=polyfit(x,y,3)yy=polyval(p,xi)subplot(3,2,1)plot(x,y,'o')subplot(3,2,2)plot(x,y,'o',xi,yy)subplot(3,2,3)plot(x,y,'o',xi,yi1)subplot(3,2,4)plot(x,y,'o',xi,yi2)subplot(3,2,5)plot(x,y,'o',xi,yi3)subplot(3,2,6)plot(x,y,'o',xi,yi4)089二维插值计算:[x,y]=meshgrid(-3:0.5:3);z=peaks(x,y);[xi,yi]=meshgrid(-3:0.1:3); zi=interp2(x,y,z,xi,yi,'spline') plot3(x,y,z)hold onmesh(xi,yi,zi+15)hold offaxis tight090函数表达式;function f=exlin(x)if x<0f=-1;elseif x<1f=x;elseif x<2f=2-x;elsef=0;end091:硬循环语句:n=5;for i=1:nfor j=1:nif i==ja(i,j)=2;elsea(i,j)=0;endendendwhile 循环语句:n=1;while prod(1:n)<99^99;n=n+1endn:092 switch开关语句a=input('a=?')switch acase 1disp('It is raning') case 0disp('It do not know')case -1disp('It is not ranging')otherwisedisp('It is raning ?')end093画曲面函数:x1=linspace(-3,3,30)y1=linspace(-3,13,34)[x,y]=meshgrid(x1,y1);z=x.^4+3*x.^2-2*x+6-2*y.*x.^2+y.^2-2*y; surf(x,y,z)。

(完整版)matlab基础练习题(带答案)

(完整版)matlab基础练习题(带答案)

Matlab 基础练习题常量、变量、表达式1、 MATLAB 中,下面哪些变量名是合法的?( )(A )_num (B )num_ (C )num- (D )-num 2、 在MA TLAB 中,要给出一个复数z 的模,应该使用( )函数。

(A )mod(z) (B )abs(z) (C )double(z) (D )angle(z) 3、 下面属于MATLAB 的预定义特殊变量的是?( )(A )eps (B )none (C )zero (D )exp4、 判断:在MA TLAB 的内存工作区中,存放一个英文字符 'a' 需要占用1个字节,存放一个中文字符‘啊’需要占用2个字节。

( 错,都是2个字节 )5、 判断:MA TLAB 中,i 和j ( 对 )6、 判断:MA TLAB 中,pi 代表圆周率,它等于3.14。

( 错,后面还有很多位小数 )7、 在MA TLAB 中,若想计算的51)3.0sin(21+=πy 值,那么应该在MA TLAB 的指令窗中输入的MA TLAB 指令是__y1=2*sin(0.3*pi)/(1+sqrt(5))_。

8、 在MA TLAB 中,a = 1,b = i ,则a 占_8__个字节,b 占_16_个字节,c 占________字节。

9、 在MA TLAB 中,inf 的含义是__无穷大__,nan 的含义是__非数(结果不定)___。

数组1、 在MA TLAB 中,X 是一个一维数值数组,现在要把数组X 中的所有元素按原来次序的逆序排列输出,应该使用下面的( )指令。

(A )X[end:1] (B )X[end:-1:1] (C )X (end:-1:1) (D )X(end:1) 2、 在MA TLAB 中,A 是一个字二维数组,要获取A 的行数和列数,应该使用的MATLAB的命令是( )。

(A )class(A) (B )sizeof(A) (C )size(A) (D )isa(A)3、 在MATLAB 中,用指令x=1:9生成数组x 。

matlab习题及答案

matlab习题及答案

matlab习题及答案2. ⽤MATLAB 语句输⼊矩阵A 和B3.假设已知矩阵A ,试给出相应的MATLAB 命令,将其全部偶数⾏提取出来,赋给B 矩阵,⽤magic(8)A =命令⽣成A 矩阵,⽤上述命令检验⼀下结果是不是正确。

4.⽤数值⽅法可以求出∑=++++++==6363622284212i i S ,试不采⽤循环的形式求出和式的数值解。

由于数值⽅法是采⽤double 形式进⾏计算的,难以保证有效位数字,所以结果不⼀定精确。

试采⽤运算的⽅法求该和式的精确值。

5.选择合适的步距绘制出下⾯的图形。

(1))/1sin(t ,其中)1,1(-∈t ;(2))tan(sin )sin(tan t t -,其中),(ππ-∈t6. 试绘制出⼆元函数2222)1(1)1(1),(yx yx y x f z ++++-==的三维图和三视图7. 试求出如下极限。

(1)xxxx 1)93(lim +∞→;(2)11lim0-+→→xy xy y x ;(3)22)()cos(1lim222200y x y x ey x y x +→→++-8. 已知参数⽅程-==tt t y t x sin cos cos ln ,试求出x y d d 和3/22d d π=t x y9. 假设?-=xyt t e y x f 0d ),(2,试求222222y fy x f x f y x ??+-?? 10. 试求出下⾯的极限。

(1)-++-+-+-∞→1)2(1161141121lim 2222n n ;(2))131211(lim 2222ππππn n n n n n n ++++++++∞→ 11. 试求出以下的曲线积分。

(1)?+ls y x d )(22,l 为曲线)sin (cos t t t a x +=,)cos (sin t t t a y -=,)20(π≤≤t 。

(2)?-+++ly y y xe x e yx )dy 2(xy d )(33,其中l 为22222c y b x a =+正向上半椭圆。

MATLAB40道题与答案修正版

MATLAB40道题与答案修正版

操作方法:首先点击File —New —M-File ,输入相应代码,然后点击运行按钮即可。

P1.已知两序列[]0.8{[][5]}nx n u n u n =--,[]{1,1,1,1,1}h n =计算两序列的卷积并绘制其波形。

%function a1 for i=1:5x(i)=0.8^(i-1); endh=[1 1 1 1 1]; y=conv(x,h) m=0:8;stem(m,y,'filled')P2。

已知复指数序列(1.52)[] 1.2j nx n e +=,绘制20点该序列的实部和虚部。

n=0:19;x=1.2*exp((1.5+j*2)*n); subplot(211); stem(n,real(x)); ylabel('real part'); xlabel('n'); legend('x[n]'); subplot(212); stem(n,imag(x)); ylabel('imag part'); xlabel('n'); legend('x[n]');P3.编写长度为5的中值滤波器程序。

原始未受干扰的序列为:s[n]=3[n(0.5) ]n ,加性噪声信号d[n]为随机序列,幅度0.4,分别绘制长度为40的受干扰序列,以及中值滤波器的输出。

% Program 2_5% Illustration of Median Filtering N = 5; R = 40;a = 0.8*rand(1,R)-0.4;b = round(a); % Generate impulse noise m = 0:R-1;s =3*m.*(0.5.^m); % Generate signalx = s + b; % Impulse noise corrupted signal y = medfilt1(x,N); % Median filtering subplot(2,1,1) stem(m,x);xlabel('n');ylabel('Amplitude');title('Impulse Noise Corrupted Signal'); subplot(2,1,2) stem(m,y);xlabel('n');ylabel('Amplitude'); title('Output of Median Filter'); P4. 已知序列x1[n]={2.2,3,1.5,4.2,1.8}, x2[n]= {0.8,1,1.6,0.8},x[n]=x1[n]∗x2[n] (卷积),分别绘制序列x1[n] ,x2[n]和x [n]的波形。

MATLAB课后习题及练习7.6

MATLAB课后习题及练习7.6

7.6 课后作业1.分别绘制下列函数图形:(1) r=3(1-cosθ) (极坐标)>> t=0:.01:2*pi;>> figure>> polar(t,abs(3*(1-cos(t))));运行结果:(2) y (t)=1.25e-0.25t +cos(3t)>> x=0:pi/180:2*pi;>> y1=1.25*exp(-0.25*x);y2=cos(3*x);>> plot(x,y1+y2)运行结果:2 .绘制函数y (t)=1-2e-tsint (0 ≤t≤8)的图形,且在x 轴上标注“Time”,y 轴上标注“Amplitude”,图形的标题为“Decaying Oscillating Exponential”。

>> x=0:0.01:8;>> y=1-2*exp(-1*x).*sin(x);>> figure>> plot(x,y)>> xlabel('Time');ylabel('Amplitude');>> title('Decaying Oscillating Exponential');运行结果:3 .在同一图中绘制下列两条曲线(x ∈[0,25]内) :(1) y 1(t)=2.6e(-0.5x) cos(0.6x)+0.8 ;(2) y 2(t)=1.6cos(3x)+sin(x)要求用不同的颜色和线型分别表示y 1(t) 和y 2(t) ,并给图形加注解。

>> x=0:0.1:25;>> y1=2.6*exp(-0.5*x).*cos(0.6*x)+0.8;>> y2=1.6*cos(3*x)+sin(x);>> figure>> plot(x,y1,'r-X',x,y2,'b--.');>> legend('y1=2.6*exp(-0.5*x).*cos(0.6*x)+0.8','y2=1.6*cos(3*x)+sin(x)')4 .在一个图形窗口下绘制两个子图,分别显示下列曲线:(1)y =sin2xcos3x ;(2) y =0.4 x要求给x 轴、y 轴加标注,每个子图加标题。

matlab习题详解讲解学习

matlab习题详解讲解学习

实验二、矩阵的基本运算一、 问题已知矩阵A 、B 、b 如下:⎥⎥⎥⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎢⎢⎢⎣⎡-------------=031948118763812654286174116470561091143A ⎥⎥⎥⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎢⎢⎢⎣⎡------=503642237253619129113281510551201187851697236421B []1187531=b应用Matlab 软件进行矩阵输入及各种基本运算。

二、 实验目的:熟悉Matlab 软件中的关于矩阵运算的各种命令三、 预备知识1、线性代数中的矩阵运算。

2、本实验所用的Matlab 命令提示:(1)、矩阵输入格式:A =[a 11, a 12; a 21, a 22];b =初始值:步长:终值;(2)、求A 的转置:A';(3)、求A 加B :A +B ;(4)、求A 减B :A -B ;(5)、求数k 乘以A :k*A ;(6)、求A 乘以B :A*B ;(7)、求A 的行列式:det (A );(8)、求A 的秩:rank (A );(9)、求A 的逆:inv (A )或(A )-1;(10)、B 右乘A 的逆:B/A ;(11)、B 左乘A 的逆:A \B ;(12)、求A 的特征值:eig (A );(13)、求A 的特征向量矩阵X 及对角阵D :[X ,D ]=eig (A );(14)、求方阵A 的n 次幂:A ^n ;(15)、A与B的对应元素相乘:A.*B;(16)、存储工作空间变量:save '文件名' '变量名';(17)、列出工作空间的所有变量:whos;四、实验内容与要求1、输入矩阵A,B,b;2、作X21=A'、X22=A+B、X23=A-B、X24=AB;3、作X31=|A|、X32=|B|;4、作X41=R(A)、X42=R(B);5、作X5=A-1;6、求满足矩阵方程XA=C的解矩阵X6,其中C为A的第i列乘以列标i所得矩阵;7、求满足方程AX=b的解向量X7;8、作X6的特性向量X8、X6的特征向量组X及对角阵D;9、作X9=B2 (A-1)2;10、创建从2开始公差为4的等差数列前15项构成的行向量X10。

MATLAB精讲多练

MATLAB精讲多练

想象变量是一口箱子,在箱子上贴了标签,表明他的名字,箱子里
面放着他的值。箱子本来不存在,只要你的Matlab 的操作视窗里“呼 唤”他的名字,他就像阿拉丁神灯里的精灵一样,呼啦,他突然就出现
了。比如:foo
Matlab 可能回应Undefined function or variable ‘foo’,这就是 说 Matlab 沒有一个名叫 foo 的函数,也沒有一口名叫 foo 的箱子。 但是,只要说
x = A\b 就是前述联立方程式的一组解。 例如以下线性联立方程式
4 x1 6 x2 x3 1 5 x1 8 x2 3 x3 0 x 4x x 0 2 3 1
可以如此求解:令 A = [4 6 -1; 5 -8 3; 1 4 1] b = [1 0 0]’ x = A\b 得到一组数值解 0.1667 0.0167 -0.2333
商多项式和余数多项式两部分。计算多项式除法形如
[div,rest]=deconv() 4、微分 用函数polyder()来实现多项式的微分。 7 p( x) 2x 4 6x3 3x 2 0x 例如:求多项式 >>p=[2 -6 3 0 7]; >>q= polyder(p) 的微分。
或者
2^(1/2) 或者
2^(1.25)
a e xx
Matlab具备一般工程性计算器该有的基本功能——指数与对数。
科学与工程领域惯用「标准指数函数」,也就使以e为底的指数函数。
其中,e是一个无理数,大约等于2.71828。 Matlab并不提供e这个常数, 我们不能按幂指数的形式来写,比如: e^2 是非法的。 Matlab以函数exp( )来计算以e为底的指数函数。比如: exp(1) 得到常数e的近似值。

matlab习题及答案

matlab习题及答案

matlab习题及答案Matlab习题及答案Matlab是一种强大的数学计算软件,被广泛应用于科学计算、数据分析和工程设计等领域。

在学习和使用Matlab的过程中,习题是一种非常有效的学习方式。

本文将给出一些常见的Matlab习题及其答案,帮助读者更好地掌握Matlab的使用技巧。

一、基础习题1. 计算1到100之间所有奇数的和。

解答:```matlabsum = 0;for i = 1:2:100sum = sum + i;enddisp(sum);```2. 编写一个函数,计算任意两个数的最大公约数。

解答:```matlabfunction gcd = computeGCD(a, b)while b ~= 0temp = b;a = temp;endgcd = a;end```3. 编写一个程序,生成一个5×5的随机矩阵,并计算矩阵的行和列的平均值。

解答:```matlabmatrix = rand(5);row_average = mean(matrix, 2);col_average = mean(matrix);disp(row_average);disp(col_average);```二、进阶习题1. 编写一个程序,实现插入排序算法。

解答:```matlabfunction sorted_array = insertionSort(array)n = length(array);for i = 2:nj = i - 1;while j > 0 && array(j) > keyarray(j+1) = array(j);j = j - 1;endarray(j+1) = key;endsorted_array = array;end```2. 编写一个程序,实现矩阵的转置。

解答:```matlabfunction transposed_matrix = transposeMatrix(matrix) [m, n] = size(matrix);transposed_matrix = zeros(n, m);for i = 1:mfor j = 1:ntransposed_matrix(j, i) = matrix(i, j);endendend```3. 编写一个程序,实现二分查找算法。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第一章2、设A =1.2,B =-4.6,C =8.0,D =3.5,E =-4.0计算:22arctan E A BC T D ππ⎛⎫+ ⎪= ⎪ ⎪⎝⎭>> A=1、2; B=-4、6;C=8、0;D=3、5;E=-4、0;>> T=atan(2*pi*A+E/(2*pi*B*C))/DT =0、4112>> x=pi/180*45;>> (sin(x)+sqrt(35))/72^(1/5)ans =2、81583、设45x ︒=,计算>> x=pi/180*45;>> (sin(x)+sqrt(35))/72^(1/5)2、81584、设 5.67,7.811,a b ==计算: ()()10log a b a b e ++>> a=5、67;b=7、811;>> exp(a+b)/log10(a+b)ans =6、3351e+005 5、计算2316()( 3.2)/(7.7)y x x x x=-++-+在3x =时的值。

>> x=3;>> y=sqrt(x)-6*(x+1/x)+(x-3、2)^2/(x+7、7)^3y =-18、26796、已知圆的半径为15,求其直径,周长及面积。

>> r=15;d=2*r;>> c=pi*2*r94、2478>> s=pi*r*rs =706、85837已知某三角形的三个边的边长为8、5,14、6与18、4,求该三角形的面积。

提示:area =其中:()/2,s a b c =++a 、b 、c 分别为三角形三边边长。

>>a=8、5;b=14、6;c=18、4;>> s=(a+b+c)/2;>> area=sqrt(s*(s-a)*(s-b)*(s-c))area =60、6106第二章1、设矩阵311212123A ⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦ 111210111B -⎡⎤⎢⎥=-⎢⎥⎢⎥-⎣⎦求:22(1)2(2)43(3)(4)(5)A BA B ABBAAB BA +-->> A=[3 1 1;2 1 2;1 2 3];B=[1 1 -1;2 -1 0;1 -1 1];(1)>> 2*A+Bans =7 3 16 1 43 3 7(2)>> 4*A*A-3*B*Bans =42 21 3840 19 4640 33 56(3)>> A*Bans =6 1 -26 -1 08 -4 2(4)>> B*Aans =4 0 04 1 02 2 2(5)>> A*B-B*Aans =2 1 -22 -2 06 -6 02、设三阶矩阵A B-=+、,满足16,A BA A BA 其中100310041007A ⎡⎤⎢⎥⎢⎥⎢⎥=⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦求矩阵B 。

>> A=[1/3 0 0; 0 1/4 0;0 0 1/7];>> B=6*inv(inv(A)-eye(3))B =3 0 00 2 00 0 13、设11(2)T E C B A C ---=,其中E 就是4阶单位矩阵,T A 就是4阶矩阵A 的转置。

其中1232012300120001B --⎡⎤⎢⎥-⎢⎥=⎢⎥⎢⎥⎣⎦ 1201012000120001C ⎡⎤⎢⎥⎢⎥=⎢⎥⎢⎥⎣⎦求矩阵A 。

>> B=[1 2 -3 -2;0 1 2 -3;0 0 1 2;0 0 0 1];>> C=[1 2 0 1;0 1 2 0;0 0 1 2;0 0 0 1];>> A=(inv(2*eye(4)-inv(C)*B)*inv(C))'A =1 0 0 0-2 1 0 01 -2 1 00 1 -2 14、设二阶矩阵A 、B 、X ,满足2X A B X -=-,其中2112A -⎛⎫= ⎪-⎝⎭ 0220B -⎛⎫= ⎪-⎝⎭求矩阵X 。

>> A=[2 -1;-1 2];B=[0 2;-2 0];>> X=(2*A+B)/2X =2 0-2 25、求解线性方程组12412341234123423285223742212x x x x x x x x x x x x x x x -+=⎧⎪+++=⎪⎨-+-=⎪⎪+++=⎩>> A=[2 -3 0 2;1 5 2 1;3 -1 1 -1;4 1 2 2];>> B=[8;2;7;12];>> x=A\Bx =3、00000、0000-1、00001、00006、求解一元六次方程654331247810x x x x x +++++=的根。

>> A=[3 12 4 7 0 8 1];>> x=roots(A)x =-3、8230-0、5275 + 0、8497i-0、5275 - 0、8497i0、5007 + 0、6749i0、5007 - 0、6749i-0、12347、求多项式65433124781x x x x x +++++被3(3)(5)x x x -+除后的结果。

>> a=[3 12 4 7 0 8 1];A=[3];>> b=poly(A);>> c=[1 0 5 0];>> d=conv(b,c);>> [div,rest]=deconv(a,d)div =3 21 52rest =0 0 0 103 55 788 1第三章1.求极限()0lim cos x x π+→>>x=sym('x');>>limit((cos(sqrt(x)))^(pi/x),x,0,'right')ans =exp(-1/2*pi)2.求极限()()213sin x x cos lim 1cos ln 1x x x x →∞+++>>x=sym('x');>>f=sym('(3*sin(x)+x^2*cos(1/x))/((1+cos(x))*log(1+x))'); >>limit(f,x,0)ans =3/23.求极限lim x →∞>>x=sym('x');>>f=sym('(sqrt(4*x^2+x-1)+x+1)/sqrt(x^2+sin(x))');>>limit(f,x,-inf) ans = 1 4.求极限222200lim(x y )x y x y →→+>>syms x y;>>f=sym('(x^2+y^2)^(x^2*y^2)'); >>limit(limit(f,x,0),y,0) ans = 1 5、已知 求y ’ x=sym('x');y=sym('(tan(sqrt(x+sqrt(x+sqrt(2*x)))))^2'); diff(y,x)ans =2y tg =tan((x+(x+2^(1/2)*x^(1/2))^(1/2))^(1/2))*(1+tan((x+(x+2^(1/2)*x^(1/2))^(1/2))^(1/2))^2)/(x+(x+2^(1/2)*x^(1/2))^(1/2))^(1/2)*(1+1/2/(x+2^(1/2)*x^(1/2))^(1/2)*(1+1/2*2^(1/2)/x^(1/2))) 6.已知 求y ’>>x=sym('x');>>y=sym('cos(x^2)*sin(1/x)^2'); >>diff(y,x) ans =-2*sin(x^2)*x*sin(1/x)^2-2*cos(x^2)*sin(1/x)*cos(1/x)/x^2 7、求积分>>x=sym('x');>>y=sym('sqrt(sin(x)-sin(x)^3)'); >>int(y,x,0,pi) ans = 4/3221cos(x )siny x=0π⎰1x ⎰8.求积分 >>x=sym('x');>>y=sym('sqrt((x+1)/(x-1))/x'); >>int(y,x) ans =-((1+x)/(x-1))^(1/2)*(x-1)*(atan(1/(-1+x^2)^(1/2))-log(x+(-1+x^2)^(1/2)))/((1+x)*(x-1))^(1/2) 9、求下列微分方程的通解>>dsolve('D2y+4*Dy+4*y=exp(-2*x)','x') ans =1/2*exp(-2*x)*(2*C2+2*x*C1+x^2) 10.求微分方程>>dsolve('x^2*Dy+x*y=y^2','y(1)=1') ans =x*exp(1/x)/(exp(1/x)+exp(1/x*t)*x-exp(1/x*t))第四章1、已知向量[]1240510112131,请绘图表示。

2''4'4y e xy y -++=221',|1x x y xy y y =+==>> A=[1 2 4 0 5 10 11 21 3 1]; >> plot(A)123456789105101520252、绘制单位圆。

提示:使用axis(‘square’)命令保证图形的纵横坐标刻度比例相同。

>> ezplot('x^2+y^2=1') >> axis squarexyx 2+y 2=13、绘制伏安特性曲线:U RI ,假设R 分别为1,5,10与20。

>>R=[1 5 10 20]; >>I=0:0、1:10; >>U=I'*R; >>plot(I,U)>>legend R=1 R=5 R=10 R=20 >>grid on伏安特性曲线电流/A电压/V4、某地区一年中每月的平均气温与平均降雨量如下表4 - 5所示,请画出其图形,要求标注出坐标轴、数据点位置、数据点大小等。

相关文档
最新文档