Le-(7)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习题及参考答案经典.doc

MATLAB习题及参考答案经典.doc

习题:1, 计算⎥⎦⎤⎢⎣⎡=572396a 与⎥⎦⎤⎢⎣⎡=864142b 的数组乘积。

2, 对于B AX =,如果⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=753467294A ,⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=282637B ,求解X 。

3, 已知:⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=987654321a ,分别计算a 的数组平方和矩阵平方,并观察其结果。

4, 角度[]604530=x ,求x 的正弦、余弦、正切和余切。

(应用sin,cos,tan.cot)5, 将矩阵⎥⎦⎤⎢⎣⎡=7524a 、⎥⎦⎤⎢⎣⎡=3817b 和⎥⎦⎤⎢⎣⎡=2695c 组合成两个新矩阵: (1)组合成一个4⨯3的矩阵,第一列为按列顺序排列的a 矩阵元素,第二列为按列顺序排列的b 矩阵元素,第三列为按列顺序排列的c 矩阵元素,即 ⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡237912685574(2)按照a 、b 、c 的列顺序组合成一个行矢量,即 []2965318772546, 将(x -6)(x -3)(x -8)展开为系数多项式的形式。

(应用poly,polyvalm)7, 求解多项式x 3-7x 2+2x +40的根。

(应用roots)8, 求解在x =8时多项式(x -1)(x -2) (x -3)(x -4)的值。

(应用poly,polyvalm)9, 计算多项式9514124234++--x x x x 的微分和积分。

(应用polyder,polyint ,poly2sym)10, 解方程组⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡66136221143092x 。

(应用x=a\b)11, 求欠定方程组⎥⎦⎤⎢⎣⎡=⎥⎦⎤⎢⎣⎡5865394742x 的最小范数解。

(应用pinv) 12, 矩阵⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡-=943457624a ,计算a 的行列式和逆矩阵。

(应用det,inv)13, y =sin(x ),x 从0到2π,∆x =0.02π,求y 的最大值、最小值、均值和标准差。

(完整word版)matlab经典习题及解答

(完整word版)matlab经典习题及解答

第1章 MATLAB概论1.1与其他计算机语言相比较,MATLAB语言突出的特点是什么?MATLAB具有功能强大、使用方便、输入简捷、库函数丰富、开放性强等特点.1.2 MATLAB系统由那些部分组成?MATLAB系统主要由开发环境、MATLAB数学函数库、MATLAB语言、图形功能和应用程序接口五个部分组成。

1.4 MATLAB操作桌面有几个窗口?如何使某个窗口脱离桌面成为独立窗口?又如何将脱离出去的窗口重新放置到桌面上?在MATLAB操作桌面上有五个窗口,在每个窗口的右上角有两个小按钮,一个是关闭窗口的Close按钮,一个是可以使窗口成为独立窗口的Undock按钮,点击Undock按钮就可以使该窗口脱离桌面成为独立窗口,在独立窗口的view菜单中选择Dock ……菜单项就可以将独立的窗口重新防止的桌面上。

1.5 如何启动M文件编辑/调试器?在操作桌面上选择“建立新文件”或“打开文件"操作时,M文件编辑/调试器将被启动.在命令窗口中键入edit命令时也可以启动M文件编辑/调试器.1.6 存储在工作空间中的数组能编辑吗?如何操作?存储在工作空间的数组可以通过数组编辑器进行编辑:在工作空间浏览器中双击要编辑的数组名打开数组编辑器,再选中要修改的数据单元,输入修改内容即可。

1。

7 命令历史窗口除了可以观察前面键入的命令外,还有什么用途?命令历史窗口除了用于查询以前键入的命令外,还可以直接执行命令历史窗口中选定的内容、将选定的内容拷贝到剪贴板中、将选定内容直接拷贝到M文件中。

1。

8 如何设置当前目录和搜索路径,在当前目录上的文件和在搜索路径上的文件有什么区别?当前目录可以在当前目录浏览器窗口左上方的输入栏中设置,搜索路径可以通过选择操作桌面的file菜单中的Set Path 菜单项来完成。

在没有特别说明的情况下,只有当前目录和搜索路径上的函数和文件能够被MATLAB运行和调用,如果在当前目录上有与搜索路径上相同文件名的文件时则优先执行当前目录上的文件,如果没有特别说明,数据文件将存储在当前目录上。

matlab 程序设计 例题

matlab 程序设计 例题

一、概述Matlab是一种强大的工程计算软件,在工程领域有着广泛的应用。

对于学习Matlab程序设计来说,通过例题学习是非常有效的方法。

通过实际的例题练习,可以帮助学生更深入地理解Matlab的基本原理和应用技巧。

本文将介绍几个常见的Matlab程序设计例题,并进行详细的分析和讲解,希望能够帮助读者加深对Matlab程序设计的理解。

二、例题一:矩阵运算题目:编写一个Matlab程序,实现两个任意大小的矩阵相加的运算。

1.我们需要定义两个任意大小的矩阵A和矩阵B,可以通过rand函数生成随机矩阵,也可以手动输入矩阵的元素。

2.编写Matlab程序,使用矩阵的加法运算符“+”进行矩阵相加操作。

3.输出矩阵相加的结果,并进行验证和检查。

通过这个例题,可以加深对Matlab矩阵运算的理解,包括矩阵的定义、矩阵相加的操作和结果输出的方法。

三、例题二:函数绘图题目:编写一个Matlab程序,绘制sin函数的曲线图像。

1.我们需要定义sin函数的自变量范围和取值步长,可以使用linspace函数生成自变量的取值范围。

2.编写Matlab程序,调用sin函数计算自变量对应的函数值。

3.接下来,使用plot函数绘制sin函数的曲线图像,并设置图像的标题、坐标轴标签等其他参数。

4.输出绘制好的sin函数曲线图像,并进行观察和分析。

通过这个例题,可以了解Matlab中函数绘图的基本方法,包括自变量的定义、函数值的计算和图像的绘制等操作。

四、例题三:数值积分题目:编写一个Matlab程序,实现对指定函数在指定区间上的数值积分。

1.我们需要定义积分的目标函数和积分区间,在Matlab中可以使用function定义目标函数,也可以手动输入目标函数和积分区间。

2.编写Matlab程序,调用Matlab内置的数值积分函数(如quad、quadl等)进行数值积分的计算。

3.输出数值积分的结果,并与解析解进行比较和分析。

通过这个例题,可以了解Matlab中数值积分的计算方法,包括目标函数的定义、积分区间的设定和数值积分函数的调用。

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. 编写一个程序,实现二分查找算法。

matlab教程习题答案

matlab教程习题答案

matlab教程习题答案MATLAB教程习题答案MATLAB是一种广泛应用于科学计算和工程领域的高级编程语言和环境。

它的强大功能和灵活性使得许多人对其感兴趣,但对于初学者来说,掌握MATLAB 可能会有一些困难。

为了帮助大家更好地理解和掌握MATLAB,下面将给出一些常见的MATLAB教程习题的答案,希望能对大家有所帮助。

1. 编写一个MATLAB程序,计算1到100之间所有偶数的和。

答案:```matlabsum = 0;for i = 2:2:100sum = sum + i;enddisp(sum);```2. 编写一个MATLAB程序,生成一个10行10列的随机矩阵,并计算该矩阵的行和列的和。

答案:```matlabmatrix = rand(10, 10);row_sum = sum(matrix, 2);col_sum = sum(matrix);disp(col_sum);```3. 编写一个MATLAB程序,计算斐波那契数列的前20个数。

答案:```matlabfibonacci = zeros(1, 20);fibonacci(1) = 1;fibonacci(2) = 1;for i = 3:20fibonacci(i) = fibonacci(i-1) + fibonacci(i-2);enddisp(fibonacci);```4. 编写一个MATLAB程序,计算一个正整数的阶乘。

答案:```matlabnum = input('请输入一个正整数:');factorial = 1;for i = 1:numfactorial = factorial * i;end```5. 编写一个MATLAB程序,求解一个二次方程的根。

假设二次方程的形式为ax^2 + bx + c = 0,其中a、b、c为用户输入的系数。

答案:```matlaba = input('请输入二次方程的系数a:');b = input('请输入二次方程的系数b:');c = input('请输入二次方程的系数c:');delta = b^2 - 4*a*c;if delta < 0disp('该二次方程无实根。

MATLAB计算与编程练习题参考答案

MATLAB计算与编程练习题参考答案

MATLAB计算与编程练习题参考答案以下是MATLAB计算与编程练习题的参考答案,希望能对您的学习有所帮助。

1. 编写一个MATLAB程序,计算并输出1到100之间所有奇数的和。

```matlabsum = 0;for i = 1:2:100sum = sum + i;enddisp(sum);```2. 编写一个MATLAB函数,接受一个参数n,计算并返回1到n之间所有偶数的平方和。

```matlabfunction result = even_squared_sum(n)result = 0;for i = 2:2:nresult = result + i^2;end```3. 编写一个MATLAB程序,生成并输出一个5x5的随机矩阵,然后计算并输出矩阵的转置。

```matlabmatrix = randi([1, 10], 5, 5);disp(matrix);transposed_matrix = matrix';disp(transposed_matrix);```4. 编写一个MATLAB函数,接受两个参数a和b,计算并返回a和b的最大公约数。

```matlabfunction gcd = compute_gcd(a, b)while b ~= 0temp = b;b = mod(a, b);a = temp;gcd = a;end```5. 编写一个MATLAB程序,读取一个文本文件,统计并输出文件中每个字符的出现次数。

```matlabfilename = 'text.txt';file = fopen(filename, 'r');content = fscanf(file, '%c');fclose(file);characters = unique(content);count = zeros(size(characters));for i = 1:length(characters)count(i) = sum(content == characters(i));enddisp([characters; count]);```6. 编写一个MATLAB程序,生成一个100个元素的斐波那契数列,并绘制该数列的图形。

MATLAB程序设计例题及答案

MATLAB程序设计例题及答案

1.编写程序:计算1/3+2/5+3/7+……+10/21法一:s=0;for i=1:10s=s+i/(2*i+1);endss=4.4096法二:sum((1:10)./(3:2:21))ans=4.40962.编写程序:计算1~100中即能被3整除,又能被7整除的所有数之和。

s=0;for i=1:100if mod(i,3)==0&&mod(i,7)==0s=s+i;end,endss=2103.画出y=n!的图(1<=n<=10),阶乘的函数自己编写,禁用MATLAB自带的阶乘函数。

x=1:10;for i=1:10try y(i)=y(i-1)*i;catch y(i)=1;end,endplot(x,y)1234567891000.511.522.533.5464.一个数恰好等于它的因子之和,这个数就称为完数。

例如,6的因子为1,2,3,而6=1+2+3,因此6就是一个完数。

编程找出2000以内的所有完数。

g=[];for n=2:2000s=0;for r=1:n-1if mod(n,r)==0s=s+r;end end if s==n g=[g n];end end g g =6284965.编写一个函数,模拟numel函数的功能,函数中调用size函数。

function y=numelnumel(x)m=size(x);y=m(1)*m(2);numelnumel([123;456])ans=66.编写一个函数,模拟length函数的功能,函数中调用size函数。

function y=lengthlength(x)m=size(x);y=max(m(1),m(2));lengthlength([123;456])ans=37.求矩阵rand(5)的所有元素和及各行平均值,各列平均值。

s=rand(5);sum=sum(sum(s))mean2=mean(s,2)mean1=mean(s)sum=13.8469mean2=0.55830.55360.65540.49310.5090mean1=0.63310.50060.64870.71240.27458.编程判断1001,1003,1007,1009,1011是否为素数,若不是,输出其约数。

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

2009-4-13
11
Computer Simulation & Application
5、sinc函数 与 Sa(t)=sin(t)/t t=linspace(-5,5); y1=sinc(t); Subplot(121) plot(t,y1); y2=sin (t)./t; Subplot(122) plot(t,y2);
2009-4-13
调用方法: Sum=mysum(0,15) %【s=65535】
用while循环 sum=0,i=n1; while i<=n2 sum=sum+2^i; i=i+1; end
5
Computer Simulation & Application
3、对一个变量 x 自动赋值。当从键盘输入 y 或 Y 时(表示是),x 自动赋为 1; 当从键盘输入 n 或 N 时(表示否),x 自动赋为 0;输入其他字符 时显示’wrong character’。编写函数文件。
2009-4-13
2
Hale Waihona Puke Computer Simulation & Application
一、程序文件
主程序文件 函数文件
2009-4-13
3
Computer Simulation & Application
1、编写主程序(命令)文件:计算 1+2+…+n<2000 时的最大 n 值 分析:累加,故只 能用循环 n=1; s=1; while s<2000 n=n+1; s=s+n; end n-1
2009-4-13 18
Computer Simulation & Application
4.4 函数功能和数值积分函数库(funfun) 3、求函数零点 fzero
X = FZERO(FUN,X0) tries to find a zero of the function FUN near X0, if X0 is a scalar. 例: X = fzero(@sin,3) X = fzero(@(x) sin(3*x),2) Or function f = myfun(x,c) f=cos(c*x); c = 2; % define parameter first x = fzero(@(x) myfun(x,c),0.1)
2009-4-13
验证程序: s=1; for i=2:62; s=s+i; end s % 【s=1953】 若改成函数文件呢? 例:任意参数SUM,编写函数 文件my_max_sum,求出使 1+2+…+n<SUM的n值
4
Computer Simulation & Application
2、编写函数文件:分别用 for 和 while 循环编写 程序,求 2 的 n1 到 n2 次幂的和
调用方法: x=autox
disp('wrong character')
2009-4-13
6
Computer Simulation & Application
4、编写分段函数 的函数文件,存放于文件ff.m 中,计算出 f (−3), f ( 2), f (∞) 的值。 function y=f(x) if x>=0 & x<1 y=x; y=2-x;
2009-4-13 19
Computer Simulation & Application
4.4 函数功能和数值积分函数库(funfun) 4、求定积分 quad (or quad8)
Q = QUAD(FUN,A,B) tries to approximate the integral of scalar-valued function FUN from A to B to within an error of 1.e-6 using recursive adaptive Simpson quadrature. Example: Q = quad(@myfun, 0, 2 ); where myfun.m is the M-file function: %-------------------% function y = myfun(x) y = 1./(x.^3-2*x-5); %-------------------%
2009-4-13
8
Computer Simulation & Application
二、常见信号的产生和绘制
例:连续时间信号 f(t)=Sa(t)=sin(t)/t t= -10:1.5:10;f=sin(t)./t; plot(t,f); title(‘f(t)=Sa(t)’); xlabel(‘t’); axis([-10,10,-0.4,1.1]);
2009-4-13 17
Computer Simulation & Application
4.4 函数功能和数值积分函数库(funfun) 2、求函数极值 fminbnd
X = FMINBND(FUN,x1,x2) attempts to find a local minimizer X of the function FUN in the interval x1 < X < x2. 例: X = fminbnd(@cos,3,4) x = fminbnd(@(x) sin(x)+3,2,5) Or function f = myfun(x,c) f = (x - c)^2; c = 1.5; % define parameter first x = fminbnd(@(x) myfun(x,c),0,1)
2009-4-13
14
Computer Simulation & Application
任意函数的定义方法(一)
例:f=@(t)(sin(pi/4*t)); % f= sin(pi/4*t) 例: a=1;b=-2;c=1; f=@(x)(a*x.^2+b*x+c); ezplot(f) 例:a=pi,b=15; f=@(x,y)(a*x+b*y); ezsurf(f)
Computer Simulation & Application
计算机仿真及应用
MATLAB编程
主讲教师:魏广芬 山东工商学院信息与电子工程学院 eegingkowei@
2009-4-13 1
Computer Simulation & Application
主要内容
一、程序文件 二、常见信号的产生和绘制
2009-4-13 12
% y1 = sin(pi*t)/(pi*t)
Computer Simulation & Application
一、向量表示法
对于连续时间信号f(t),可以用两个行向量f和t来表 示,其中向量t是形如t=t1:p:t2的MATLAB命令定义 的时间范围向量,t1为信号起始时间,t2为信号终止 时间,p为时间间隔。向量f为连续信号f(t)在向量t所 定义的时间点上的样值。 时间间隔p取得小,信号波形光滑
2009-4-13
10
Computer Simulation & Application
3、周期方波
t=0:0.001:2.5; y=square(2*pi*30*t); plot(t(1:50),y(1:50));
4、周期锯齿波
t=0:0.001:2.5; y=sawtooth(2*pi*30*t); plot(t,y); axis([0 0.2 –1 1]);
调用方法: ff(-3) %0
⎧ x, ⎪ f ( x ) = ⎨ 2 − x, ⎪ 0, ⎩
0 ≤ x <1 1≤ x < 2 others
elseif x>=1 & x<2 else end y=0;
ff(sqrt(2)) % 0.5858 ff(inf)
2009-4-13
%0
7
Computer Simulation & Application
function [sum]=mysum(n1,n2) %%% sum=mysum(n1,n2) %%% n1: the first number, %%% n2: the last number %%% sum=2^n1+2^(n1+1)+...+2^ (n2) sum=0; for i=n1:n2 sum=sum+2^i; end
2009-4-13 20
Computer Simulation & Application
4.4 函数功能和数值积分函数库(funfun) 5、求不定积分 利用定积分函数求不定积分 例: s=quad(‘humps’,1,2) %[1,2]之间的定积分 求humps函数以x=0为下限的不定积分
for i=1:20 x(i)=0.1*i; y(i)=quad('humps',0,x(i)); end plot(x,y)
2009-4-13
13
Computer Simulation & Application
二、 ezplot
如果信号可以用一个符号表达式来表示它,则我们 可用ezplot命令(缺省的区间为[-2*pi,2*pi])绘制出 信号的波形,例如对于连续信号f(t)=sin(πt/4) ezplot(‘sin(pi/4*t)’); ezplot('sin(pi/4*t)',[-16,16]);
function x=autox %%% x is 1, if input y or Y; x is 0, if input n or N;%%% break if input other characters a=input('input a character: ','s'); if a=='y' | a=='Y' elseif a=='n' | a=='N' else end x=1; x=0;
相关文档
最新文档