第一章MATLAB入门4、求近似解解:x=-2:0.05:2;y=x.^4-2.^x两个近似解:y1=f(-0.85)=-0.0328;y2=f(1.250)=0.0630第二章MATLAB编程与作图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、求满足的最小m值100)1ln(0+∑=mnns=0;n=0;while(s=100)s=s+log(1+n);n=n+1;endn,s3、用循环语句形成Fibonacci数列,....4,3,,12121=+===−−kFFFFFkkk。并验证极限2511+→−kkFF(提示:计算至两边误差小于精度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,并考虑一种避免循环语句的程序设计,比较各种算法的运行时间。解:循环结构:M文件loop.mk=0;fori=1:10^6k=k+sqrt(3)*2^-i;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.5、作图描述气温变化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)6、作出下列函数图形(1))2sin(22−−=xxxy22≤≤−x(分别使用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)19422=+yx(椭圆提示:用参数方程)解: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,622232224−+−−−++=yxyxyxyxxz解: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,sin===ttztytx解:t=linspace(0,2,50);x=sin(t);y=cos(t);z=cos(2*t);plot3(x,y,z)(6)半球面oo900,3600,cos2,sinsin2,cossin2≤≤≤≤===θφφθφθφzyx解: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;7、作下列分段函数图⎪⎩⎪⎨⎧−≤−=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;9、用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);6、运行demo解:demo7、查询trapz的功能、用法、目录、程序结构、相同目录下其它文件解:helptrapz――功能用法typetrapz――程序结构,源码whichtrapz――所在目录helpC:\MATLAB6p5\toolbox\matlab\datafun――该目录下其它文件第三章矩阵代数2、求下列线性方程组的解(1)⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡−=⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡−−−129351623114321xxx解:a=[41-1;32-6;1-53];b=[9;-2;1];x=a\b%唯一解x=2.38301.48942.0213(2)⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡−−=⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡−−−121351623334321xxx解:a=[4-33;32-6;1-53];b=[-1;-2;1];x=a\b%唯一解x=-0.4706-0.29410(3)⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=⎥⎦⎤⎢⎣⎡⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡−11151231421xx解:a=[41;32;1-5];b=[1;1;1];x=a\b%最小二乘近似解x=0.3311-0.1219(4),求通解⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡−−3211211112111124321xxxx解:a=[21-11;121-1;1121];b=[1;2;3];rank(a),rank([a,b])ans=3ans=3%说明有无穷多解rref([a,b])%行最简化ans=1.0000001.50001.000001.00000-1.50000001.00000.50001.0000%通解为:,15.141+−=xx425.1xx=,15.043+−=xx4、(人口流动趋势)解:⎟⎟⎠⎞⎜⎜⎝⎛=95.001.005.099.0A⎟⎟⎠⎞⎜⎜⎝⎛=8.02.0B设年后城镇人口比例为,农村人口比例为,且k1x2x⎟⎟⎠⎞⎜⎜⎝⎛=21xxx则一年后:;BAx⋅=两年后:BAx⋅=2k年后:BAxk⋅=A=[0.990.05;0.010.95];b=[0.2;0.8];X1=A*b,X2=A*A*b,X10=A^(10)*b[P,J]=eig(A),%J=diag([1,0.94])X=P*diag([1,0])/P*[0.2;0.8]5、(经济预测)直接消耗矩阵:⎟⎟⎟⎟⎟⎟⎠⎞⎜⎜⎜⎜⎜⎜⎝⎛=208.152.0253202.0512525.220152256A;t年后外部需求:⎪⎩⎪⎨⎧=171717Yt年后各部门产出:YAIXYAXX\)(−=⇒+=clear;A=[6/252/51/20;2.25/251/50.2/20;3/250.2/51.8/20];Y=[17;17;17];X=(eye(3)-A)\YX=37.569625.786224.76906、求下列矩阵的行列式、逆、特征值和特征向量(1)⎟⎟⎟⎠⎞⎜⎜⎜⎝⎛−−−351623114解:a=[41-1;32-6;1-53];det(a),inv(a),[v,d]=eig(a)ans=-94行列式ans=0.2553-0.02130.04260.1596-0.1383-0.2234矩阵的逆0.1809-0.2234-0.0532v=0.0185-0.9009-0.3066-0.7693-0.1240-0.7248特征向量-0.6386-0.41580.6170d=-3.05270003.67600特征值008.3766(2)⎟⎟⎟⎠⎞⎜⎜⎜⎝⎛−−351533134解:a=[431;33-5;1-53];det(a),inv(a),[v,d]=eig(a)ans=-124行列式ans=0.12900.11290.