例2.1>> muw0=1.785;>> a=0.03368;>> b=0.000221;>> t=0:20:80;>> muw=muw0./(1+a*t+b*t.^2)例2.2 数值数组和字符串的转换>> a=[1:5];>> b=num2str(a);>> a*2ans =2 4 6 8 10>> b*2ans =98 64 64 100 64 64 102 64 64 104 64 64 106例2.9比较左除和右除求解恰定方程>> rand('seed',12);>> a=rand(100)+1.e8;>> x=ones(100,1);>> b=a*x;>> cond(a)ans =5.0482e+011>> tic;x1=b'/a;t1=toct1 =0.4711>> er1=norm(x-x1')er1 =139.8326>> re1=norm(a*x1'-b)/norm(b)re1 =4.3095e-009>> tic;x1=a\b;t1=toct1 =0.0231>> tic;x1=a\b;t1=toct1 =0.0011>> er2=norm(x-x1)er2 =1.5893e-004>> re1=norm(a*x1-b)/norm(b)re1 =4.5257e-016例2.14:计算矩阵的指数>> b=magic(3);>> expm(b)ans =1.0e+006 *1.0898 1.0896 1.08971.0896 1.0897 1.08971.0896 1.0897 1.0897 例2.18:特征值条件数>> a=[-149 -50 -154;537 180 546; -27 -9 -25]a =-149 -50 -154537 180 546-27 -9 -25>> [V,D,s]=condeig(a)V =0.3162 -0.4041 -0.1391-0.9487 0.9091 0.9740-0.0000 0.1010 -0.1789D =1.0000 0 00 2.0000 00 0 3.0000例2.41 5阶多项式在【0,2pi】最小二乘拟合>> x=0:pi/20:pi/2;>> y=sin(x);>> a=polyfit(x,y,5);>> x1=0:pi/30:pi*2;>> y1=sin(x1);>> y2=a(1)*x1.^5+a(2)*x1.^4+a(3)*x1.^3+a(4)*x1.^2+a(5)*x1+a(6); >> plot(x1,y1,'b-',x1,y2,'r*')>> legend('原曲线','拟合曲线')>> axis([0,7,-1.2,4])例3.7 gradient绘制矢量图>> x=0:pi/20:pi/2;>> y=sin(x);>> a=polyfit(x,y,5);>> x1=0:pi/30:pi*2;>> y1=sin(x1);>> y2=a(1)*x1.^5+a(2)*x1.^4+a(3)*x1.^3+a(4)*x1.^2+a(5)*x1+a(6); >> plot(x1,y1,'b-',x1,y2,'r*')>> legend('原曲线','拟合曲线')>> axis([0,7,-1.2,4])>>>> [x,y]=meshgrid(-2:.2:2,-2:.2:2); >> z=x.*exp(-x.^2-y.^2);>> [px,py]=gradient(z,.2,.2);>> contour(z),>> hold on>> quiver(px,py)>> hold off例基本绘图命令rand(100,1);plot(y)例4.1 绘制如图>> x=1:0.1*pi:2*pi;>> y=sin(x);>> z=cos(x);>> plot(x,y,'--k',x,z,'-.rd')例4.5 绘制如图>> x=1:10;>> y=rand(10,1);>> bar(x,y);>> x=0:0.1*pi:2*pi;>> y=x.*sin(x);>> feather(x,y)例 4.6 绘制如图>> lim=[0,2*pi,-1,1];>> fplot('[sin(x),cos(x)]',lim)例4.7绘图如下>> x=[2,4,6,8];>> pie(x,{'math','english','chinese','music'}) 例4.9 绘图如下三维螺旋线>> x=0:pi/50:10*pi;>> y=sin(x);>> x=0:pi/50:10*pi;>> y=sin(x);>> z=cos(x);>> plot3(x,y,z);例4.10 绘图如下。
矩阵三维图>> [x,y]=meshgrid(-2:0.1:2,-2:0.1:2); >> z=x.*exp(-x.^2-y.^2);>> plot3(x,y,z)例4.13绘图如下>> [X,Y]=meshgrid([-4:0.5:4]);>> Z=sqrt(X.^2+Y.^2);>> meshc(Z)例4.19 绘制柱面图>> x=0:pi/20:pi*3;>> r=5+cos(x);>> [a,b,c]=cylinder(r,30);>> mesh(a,b,c)例4.20 地球表面气温分布示意图>> [a,b,c]=sphere(40);>> t=abs(c);>> surf(a,b,c,t);>> axis('equal')>> axis('square')>> colormap('hot')例4.24坐标标注函数应用示意图>> x=1:0.1*pi:2*pi;>> y=sin(x);>> plot(x,y)>> xlabel('x(0-2\pi)','fontweight','bold');>> ylabel('y=sin(x)','fontweight','bold');>> title('正弦函数','fontsize',12,'fontweight','bold','fontname','隶书') >>例4.30 同一张图绘制几个三角函数>> x=0:0.1*pi:2*pi;>> y=sin(x);>> z=cos(x);>> plot(x,y,'-*')>> hold on>> plot(x,z,'-o')>> plot(x,y+z,'-h')>> legend('sin(x)','cos(x)','sin(x)+cos(x)',0)>> hold off例4.31 4个子图中绘制不同的三角函数图>> x=0:0.1*pi:2*pi;>> subplot(2,2,1);>> plot(x,sin(x),'-*');>> title('sin(x)');>> subplot(2,2,2);>> plot(x,cos(x),'-o');>> title('cos(x)');>> subplot(2,2,3);>> plot(x,sin(x).*cos(x),'-x');>> title('sin(x)*cos(x)');>> subplot(2,2,4);>> plot(x,sin(x)+cos(x),'-h');>> title('sin(x)+cos(x)');例7.3正弦曲线插值示例>> x=0:0.1:10;>> y=sin(x);>> xi=0:.25:10;>> yi=interp1(x,y,xi);>> plot(x,y,'o',xi,yi)例7.7 x 0.5 1.0 2.0 2.5 3.0y 1.75 2.45 3.81 4.80 8.00 8.60 y=span{1,x,x^2},最小二乘法拟合>> x=[0.5 1.0 1.5 2.0 2.5 3.0];>> y=[1.75 2.45 3.81 4.80 8.00 8.60];>> a=polyfit(x,y,2)a =0.4900 1.2501 0.8560>> x1=[0.5:0.05:3.0];>> y1=a(3)+a(2)*x1+a(1)*x1.^2;>> plot(x,y,'*')>> hold on>> plot(x1,y1,'-r')例7.8最小二乘法求y=a+b*x^2的经验公式Xi 19 25 31 38 44Yi 19.0 32.3 49.0 73.3 98.8>> x=[19 25 31 38 44];>> y=[19.0 32.3 49.0 73.3 98.8];>> x1=x.^2x1 =361 625 961 1444 1936 >> x1=[ones(5,1),x1']x1 =1 3611 6251 9611 14441 1936>> ab=x1\y'ab =0.59370.0506>> x0=[19:0.2:44];>> y0=ab(1)+ab(2)*x0.^2;>>>> clf>> plot(x,y,'o')>> hold on>> plot(x0,y0,'-r')例7.10求积分function y=fun(t)y=exp(-0.5*t).*sin(t+pi/6);>> d=pi/1000;>> t=0:d:3*pi;>>>> nt=length(t);>> y=fun(t);>> sc=cumsum(y)*d;>> scf=sc(nt)scf =0.9016>> z=trapz(y)*dz =0.9008例7.12用Newton-cotes公式求积分Fun.mfunction f=fun(x)f=exp(-x/2);quad8('fun',1,3,1e-10)例微分函数>> x=sym('x');>> diff(sin(x^2))ans =2*x*cos(x^2)例题7-44 273-274页fun.mfunction f=fun(x,y)f=-2*y+2*x.^2+2*x;>> [x,y]=ode23('fun',[0,0.5],1);>> x'ans =Columns 1 through 70 0.0400 0.0900 0.1400 0.1900 0.2400 0.2900 Columns 8 through 120.3400 0.3900 0.4400 0.4900 0.5000>> y'ans =Columns 1 through 71.0000 0.9247 0.8434 0.7754 0.7199 0.6764 0.6440 Columns 8 through 120.6222 0.6105 0.6084 0.6154 0.6179例题7-45tic;p1=flops;[x,y]=ode23('fun',[0,0.5],1);p2=flops;t=toc;p=p2-p1;>> bj例题7-46function f=f(x,y)f=[-2 1;988 -999]*y+[2*sin(x);999*(cos(x)-sin(x))];>> ode23('f',[0,10],[2,3]);>> a=[-2 1;998 -999]; %求方程的刚性比>> b1=max(abs(real(eig(a))));>> b2=min(abs(real(eig(a))));>> s=b1/b2s =1000例7-17/18 246页>> a=[0.4096, 0.1234, 0.3678, 0.2943;0.2246, 0.3872, 0.4015, 0.1129;0.3645, 0.1920, 0.3781, 0.0643;0.1784, 0.4002, 0.2786, 0.3927]; >> aa =0.4096 0.1234 0.3678 0.29430.2246 0.3872 0.4015 0.11290.3645 0.1920 0.3781 0.06430.1784 0.4002 0.2786 0.3927>> b=[0.4043 0.1550 0.4240 -0.2557]';>> x=a\bx =-0.1819-1.66302.2172-0.4467265页,例7-39 (非线性方程组的符号解法)g.mfunction y=g(x)y(1)=0.7*sin(x(1))+0.2*cos(x(2));y(2)=0.7*cos(x(1))-0.2*sin(x(2));>> x0=[0.5 0.5];>> fsolve('g',x0)No solution found.fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by thedefault value of the function tolerance.<stopping criteria details>ans =-0.0493 1.5215307页,例9-21>> x=[0.236 0.238 0.248 0.245 0.243;0.257 0.253 0.255 0.254 0.261;0.258 0.264 0.259 0.267 0.262];>> anova1(x')ans =1.3431e-005308页,例9-22>> a=[58.2000 56.2000 65.3000;52.6000 41.2000 60.8000;49.1000 54.1000 51.6000;42.8000 50.5000 48.4000;60.1000 70.9000 39.2000;58.3000 73.2000 40.7000;75.8000 58.2000 48.7000;71.5000 51.0000 41.4000];>> anova2(a,2)ans =0.0035 0.0260 0.0001例9.23 (309页)。