上机练习题一班级:姓名:学号:1.建立起始值=3,增量值=5.5,终止值=44的一维数组x答案:x=(3:5.5:44)2.写出计算Sin(30o)的程序语句.答案:sin(pi*30/180)或sin(pi/6)3.矩阵187624323A,矩阵333222111B;分别求出BA及A与B中对应元素之间的乘积.答案:A=[3,2,3;4,2,6;7,8,1]B=[1,1,1;2,2,2;3,3,3]A*B;A.*B4计算行列式的值187624323A。答案:det(A)5对矩阵187624323A进行下述操作。(1)求秩。答案:rank(A)(2)求转置。答案:A'(3)对矩阵求逆,求伪逆。答案:inv(A),pinv(A)(4)左右反转,上下反转。答案:fliplr(A),flipud(A)(5)求矩阵的特征值.答案:[u,v]=eig(A)(6)取出上三角和下三角.答案:triu(a)tril(a)(7)以A为分块作一个3行2列的分块矩阵。答案:repmat(a)6计算矩阵897473535与638976242之和。a=[535;374;798];b=[242;679;836];a+b7计算572396a与864142b的数组乘积。a=[693;275];b=[241;468];a.*bans=12363842408已知:987654321a,分别计算a的数组平方和矩阵平方,并观察其结果。a=[123;456;789];a.^2ans=149162536496481a^2ans=303642668196102126150上机练习题二班级:姓名:学号:1对于BAX,如果753467294A,282637B,求解X。A=[492;764;357];B=[372628]’;X=A\BX=-0.51184.04271.33182角度604530x,求x的正弦、余弦、正切和余切。x=[304560];x1=x/180*pi;sin(x1)ans=0.50000.70710.8660cos(x1)ans=0.86600.70710.5000tan(x1)ans=0.57741.00001.7321cot(x1)ans=1.73211.00000.57743将矩阵7524a、3817b和2695c组合成两个新矩阵:(1)组合成一个43的矩阵,第一列为按列顺序排列的a矩阵元素,第二列为按列顺序排列的b矩阵元素,第三列为按列顺序排列的c矩阵元素,即237912685574(2)按照a、b、c的列顺序组合成一个行矢量,即296531877254答案:a=[42;57];b=[71;83];c=[59;62];%(1)d=[a(:)b(:)c(:)]d=475586219732%(2)e=[a(:);b(:);c(:)]'e=452778135692或利用(1)中产生的de=reshape(d,1,12)ans=4527781356924求解在x=8时多项式(x-1)(x-2)(x-3)(x-4)的值。p=poly([1234]);polyvalm(p,8)ans=8405求方程023973234xxx的全部根。p=[3,7,9,0,-23];%建立多项式系数向量x=roots(p)%求根上机练习题三班级:姓名:学号:1、设x是数组,求均值和方差解:函数文件如下:function[xx,s]=func1(x)n=length(x);xx=sum(x)/n;s=sqrt((sum(x.^2)-n*xx^2)/(n-1));命令窗口:x=[12345];[xx,s]=func1(x)2、求满足100)1ln(0mnn的最小m值s=0;n=0;while(s=100)s=s+log(1+n);n=n+1;endn,s3、用循环语句形成Fibonacci数列,....4,3,,12121kFFFFFkkk。并验证极限2511kkFF(提示:计算至两边误差小于精度1e-8为止)解:求Fibonacci数列的函数文件:functionf=fun(n)ifn=2f=1;elsef=fun(n-1)+fun(n-2);end验证极限的函数文件:function[k,a]=funTest(e)a=abs(1-(1+sqrt(5))/2);k=2;while(ae)k=k+1;a=abs(fun(k)/fun(k-1)-(1+sqrt(5))/2);end命令行:[k,a]=funTest(10^-8)k=21a=9.7719e-009或者M文件如下:clear;F(1)=1;F(2)=1;k=2;x=0;e=1e-8;a=(1+sqrt(5))/2;whileabs(x-a)ek=k+1;F(k)=F(k-1)+F(k-2);x=F(k)/F(k-1);enda,x,k4、分别用for和while循环结构编写程序,求出610123iiK,并考虑一种避免循环语句的程序设计,比较各种算法的运行时间。解:for循环结构:M文件loop.mk=0;fori=1:10^6k=k+sqrt(3)*2^-i;endkwhile循环结构:M文件loop1.mk=0;i=1;whilei=10^6k=k+sqrt(3)*2^(-i);i=i+1;endk非循环结构:M文件nonLoop.mi=1:10^6;x=sqrt(3)*(2.^-i);k=sum(x)速度比较:tic;loop;toc%循环结构的执行时间k=1.7321Elapsedtimeis1.813000seconds.tic;nonLoop;toc%非循环结构的执行时间k=1.7321Elapsedtimeis1.094000seconds.上机练习题四班级:姓名:学号:1、作图描述气温变化x=0:24;y=[15,14,14,14,14,15,16,18,20,22,23,25,28,31,32,31,29,27,25,24,22,20,18,17,16];plot(x,y)2、作出下列函数图形(1))2sin(22xxxy22x(分别使用plot和fplot完成)解:fplot('x^2*sin(x^2-x-2)',[-22])%fplot方法x=-2:0.1:2;y=x.^2.*sin(x.^2-x-2);plot(x,y)%plot方法如图(4.1)(2)19422yx(椭圆提示:用参数方程)解:r=-pi:0.1:pi;x=2*cos(r);y=3*sin(r);plot(x,y)%如图(4.2)解法二x=-2:1/100:2;y1=3*sqrt(1-x.^2/4);y2=-3*sqrt(1-x.^2/4);plot(x,y1,'r-',x,y2,'r-');axisequaltight;图(4.1)图(4.2)(3)22yxz(抛物面)3,3yx解:(错误)x=[-3:0.1:3];y=[-3:0.1:3];z=x.^2+y.^2;plot3(x,y,z)%如图(4.31)(正确)xa=-3:0.1:3;ya=-3:0.1:3;[x,y]=meshgrid(xa,ya);%如图(4.32)z=x.^2+y.^2;mesh(x,y,z);surf(x,y,z)图(4.31)error图(4.32)(4)曲面133,3,622232224yxyxyxyxxz解:xa=linspace(-3,3,100);ya=linspace(-3,13,100);[x,y]=meshgrid(xa,ya);z=x.^4+3*x.^2+y.^2-2*x-2*y-2*x.^2.*y+6;mesh(x,y,z)surf(x,y,z)(5)空间曲线20),2cos(,cos,sinttztytx解:t=linspace(0,2,50);x=sin(t);y=cos(t);z=cos(2*t);plot3(x,y,z)(6)半球面900,3600,cos2,sinsin2,cossin2zyx解:a=linspace(0,2*pi,50);b=linspace(0,pi/2,50);[a,b]=meshgrid(a,b);x=2*sin(a).*cos(b);y=2*sin(a).*sin(b);z=2*cos(a);surf(x,y,z)(7)三条曲线合成图xxyxtyxy0,sin),10sin(sin,sin321解:x=linspace(0,pi,50);y1=sin(x);plot(x,y1);holdon;y2=sin(x).*sin(10*x);plot(x,y2);y3=-sin(x);plot(x,y3);holdoff;3、作下列分段函数图1.11.11.11.11.1xxxxyx=-5:0.1:5;fori=1:length(x)ifx(i)1.1y(i)=1.1;elseifx(i)-1.1y(i)=-1.1;elsey(i)=x(i);endendplot(x,y);gridon;4、用MATLAB函数表示下列函数,并作图。1),5.175.375.0exp(5457.011),6exp(7575.01),5.175.375.0exp(5457.0),(222222yxxxyyxxyyxxxyyxp解:建立M文件pxy如下:xa=-2:0.05:2;ya=xa;nx=length(xa);ny=length(ya);[x,y]=meshgrid(xa,ya);z=zeros(nx,ny);[a1,b1]=find(x+y1);%第a1列b1行对应的x+y1(x对应列;y对应行)%第a1列对应的x值是xa(a1);第b1行对应的y值是ya(b1)z((a1-1)*ny+b1)=0.5457*exp(-0.75*ya(b1).^2-3.75*xa(a1).^2-1.5*xa(a1));[a2,b2]=find(x+y=1&x+y-1);z((a2-1)*ny+b2)=0.7575*exp(-ya(b2).^2-6*xa(a2).^2);[a3,b3]=find(x+y=-1);z((a3-1)*ny+b3)=0.5457*exp(-0.75*ya(b3).^2-3.75*xa(a3).^2+1.5*xa(a3));surf(x,y,z);命令窗口:pxy运行结果如右图:或者M文件如下:clear;close;xa=-2:0.1:2;ya=-2:0.1:2;[x,y]=meshgrid(xa,ya);z=zeros(size(x));k1=find(x+y1);z(k1)=0.5457*exp(-0.75*y(k1).^2-3.75*x(k1).^2-1.5*x(k1));k2=find(x+y=1&x+y-1);z(k2)=0.7575*exp(-y(k2).^2-6*x(k2).^2);k3=find(x+y-1);z(k3)=0.5457*exp(-0.75*y(k3).^2-3.75*x(k3).^2+1.5*x(k3));mesh(x,y,z);上机练习题五班级:姓名:学号:1、运行demo解:demo2、查询trapz的功能、用法、目录、程