《MATLAB实习.doc》由会员分享,可在线阅读,更多相关《MATLAB实习.doc(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流MATLAB实习.精品文档. 1矩阵运算矩阵建立,转置、加、减、乘、求逆等运算。计算A+B,AB、|A|、AB. A+Bans = 0 4 3 5 6 9 3 4 2 det(A)ans = 12 A.-1ans = 1.0000 0.5000 0.3333 0.2500 0.2000 0.1667 1.0000 0.3333 1.0000 ABans = 1.1667 -1.8333 1.5000 0.6667 0.6667 0 -1.1667 0.8333 -0.5000 A*Bans = 7 7 9 13 19 21 4 6 102.编
2、写M文件的编写与运行输出闰年i=1;for (j=1000:3000) if(rem(j,100)=0) if(rem(j,400)=0) a(i)=j i=i+1; end else if(rem(j,4)=0) a(i)=j i=i+1; end end k=i-1end部分数据:Columns 463 through 469 2908 2912 2916 2920 2924 2928 2932 Columns 470 through 476 2936 2940 2944 2948 2952 2956 2960 Columns 477 through 483 2964 2968 2972
3、2976 2980 2984 2988 Columns 484 through 485 2992 29963数据文件的建立与读取例:建立班级一门课成绩数据库,编程读取并排序、另存。学号1 23456789成绩859685875665968575S.sc=85 96 85 87 56 65 96 85 75 ,S.num=1 2 3 4 5 6 7 8 9S = sc: 85 96 85 87 56 65 96 85 75S = sc: 85 96 85 87 56 65 96 85 75num: 1 2 3 4 5 6 7 8 9for i=1:9 for j=i:9 if S.sc(i)1)
4、=0; %在满足x2+y21的网格点上的Z值变成0mesh(X,Y,abs(Z) %画图title(球面x.2+y.2+z.2=1) %加标题axis(-1 2 -1 1 0 1) %设置X,Y,Z轴坐标范围5多项式运算例:多项式运算求值、求根。+x-3=0a=1 1 1 -3a = 1 1 1 -3 r=poly(a)r = 1 0 -6 8 -3 q1=poly2str(a,x)q1 = x3 + x2 + x 3f=inline( x3 + x2 + x - 3,x)f = Inline function: f(x) = x3 + x2 + x - 3 f(3)ans = 366曲线拟合
5、例:数据的多项式拟合X0.51.01.52.02.53.0y1.752.453.814.807.008.60使用了MATLAB中curve fitting,进行了该多形式的拟合。由图可知这些点是五阶多项式linear model Poly5: f(x) = p1*x5 + p2*x4 + p3*x3 + p4*x2 + p5*x + p6Coefficients: p1 = -1.6 p2 = 13.74 p3 = -44.07 p4 = 65.67 p5 = -42.63 p6 = 11.357.线性规划例:线性规划求解(如教材中)。Min -5x1-4x2-6x3s.t x1-x2+x3=
6、20; 3x1+2x2+4x3=42;3x1+2x2=0A=1 -1 1; 3 2 4;3 2 0A = 1 -1 1 3 2 4 3 2 0 b=20;42;30b = 20 42 30 x,fval, exitflag, output,lambda=linprog(f,A,b,lb)Optimization terminated.x = 0.0000 15.0000 3.0000fval = -78.0000exitflag = 1output = iterations: 6 algorithm: large-scale: interior point cgiterations: 0 me
7、ssage: Optimization terminated.lambda = ineqlin: 3x1 double eqlin: 0x1 double upper: 3x1 double lower: 3x1 double lambda.ineqlinans = 0.0000 1.5000 0.50008微分方程数值解例:常微分方程数值解。求解该微分方程X 精确解RK解误差 0110 1/5, 1.01873075, 1.07001364, .512828904e-1 2/5, 1.07032005, 1.15254655, .822265010e-1 3/5, 1.14881164, 1
8、.24639295, .975813184e-1 4/5, 1.24932896, 1.35046324, .101134274 1, 1.36787944, 1.46377272, .958932744e-1 6/5, 1.50119421, 1.58543154, .842373280e-1 7/5, 1.64659696, 1.71463557, .680386042e-1 8/5, 1.80189652, 1.85065811, .487615877e-1 9/5, 1.96529889, 1.99284245, .275435594e-1 2, 2.13533528, 2.14059
9、514, .525985426e-2 11/5, 2.31080316, 2.29337988, .174232823e-1 13/5, 2.67427358, 2.61215359, .621199906e-1 14/5, 2.86081006, 2.77730880, .835012599e-1 3, 3.04978707, 2.94581999, .1039670769插值问题例:样条插值。X1.02.03.04.05.0y3.54.65.53.22.6xx=1:1:5 yx=3.5 4.6 5.5 3.2 2xxi=1:0.5:5f0=interp1(xx ,yx, xxi)f1=in
10、terp1(xx,yx,xxi,linear)f2=interp1(xx,yx,xxi,cubic)f3=interp1(xx,yx,xxi,spline)f4=interp1(xx,yx,xxi,neareat)f0,f1, f2,f3,f4yx = 3.5000 4.6000 5.5000 3.2000 2.0000xxi = 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000f0 = 3.5000 4.0500 4.6000 5.0500 5.5000 4.3500 3.2000 2.6000 2.0000f1 =
11、 3.5000 4.0500 4.6000 5.0500 5.5000 4.3500 3.2000 2.6000 2.0000f2 = 3.5000 4.0762 4.6000 5.1738 5.5000 4.5471 3.2000 2.4841 2.0000f3 = 3.5000 3.7734 4.6000 5.3766 5.5000 4.5953 3.2000 2.0797 2.0000f4 = 3.5000 4.6000 4.6000 5.5000 5.5000 3.2000 3.2000 2.0000 2.0000f0 = 3.5000 4.0500 4.6000 5.0500 5.5
12、000 4.3500 3.2000 2.6000 2.0000f1 = 3.5000 4.0500 4.6000 5.0500 5.5000 4.3500 3.2000 2.6000 2.0000f2 = 3.5000 4.0762 4.6000 5.1738 5.5000 4.5471 3.2000 2.4841 2.0000f3 = 3.5000 3.7734 4.6000 5.3766 5.5000 4.5953 3.2000 2.0797 2.0000f4 = 3.5000 4.6000 4.6000 5.5000 5.5000 3.2000 3.2000 2.0000 2.0000p
13、lot(xxi,f1,xxi,f2,xxi,f3,xxi,f4)legend(线性插值, 三次插值,样条插值,最近区域差值)10综合练习:计算机模拟例1追逐问题:如图,正方形ABCD的四个顶点各有一人.在某一时刻,四人同时出发以匀速v=1米/秒按顺时针方向追逐下一人,如果他们始终保持对准目标,则最终按螺旋状曲线于中心点O.试求出这种情况下每个人的行进轨迹.)模拟步骤(1)建立平面直角坐标系:A(x1,y1),B(x2,y2),C(x3,y3),D(x4,y4)。(2)取时间间隔为t,在每一时刻t计算每人在下一时刻t+t的坐标。例如,甲的追逐目标是乙,在t时刻,甲的坐标为(x1,y1),乙的坐标
14、为(x2,y2),则甲在t+t时刻的坐标为(x1+vtcos a, y+vtsin a),其中cos a =(x2-x1)/d ,sin a = (y2-y1)/d,d=(3)选取足够小的,模拟到距离 d 0.1) x(5)=x(1); y(5)=y(1); for i=1:4 d=sqrt(x(i+1)-x(i)2+(y(i+1)-y(i)2); x(i)=x(i)+v*dt*(x(i+1)-x(i)/d; y(i)=y(i)+v*dt*(y(i+1)-y(i)/d; plot(x(i),y(i),.) hold on endendend轨迹如图 例2导弹追踪问题:设位于坐标原点的甲舰向位于
15、x轴上点A(1,0)处的乙舰发射导弹,导弹头始终对准乙舰.如果乙舰以最大的速度(是常数)沿平行于y轴的直线行驶,导弹的速度是5,模拟导弹运行的轨迹.又乙舰行驶多远时,导弹将它击中?查资料知道:舰艇的速度在每小时40节左右,即80kM/h,而导弹在2400km/h.他们速度之比为30:1.于此令乙减速度为a=0.167.导弹的速度v=5.在某一时刻导弹的水平速度为.(1)竖直速度(2)速度的积分路程,通过(1)(2)对时间t积分得到某一时刻的位置X=5*log(a2*t/(a2)(1/2)+(1+a2*t2)(1/2)/(a2)(1/2)Y= 5/a*(1+a2*t2)(1/2)-29.9401dt=0.05,i=1,a=0.167%a为舰艇速度for t=0:0.05:0.25 x(i)=5*log(a2*t/(a2)(1/2)+(1+a2*t2)(1/2)/(a2)(1/2) y(i)= 5/a*(1+a2*t2)(1/2)-29.9401 i=i+1endk=i; i=1:k-1plot(x(i),y(i),b:)v=0:0.001:0.25hold onv=0:0.0001:0.05plot(1,v,K-)由图可知舰艇在0.0173 处被击中
限制150内