




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、MATLAB程序设计与应用(第二版)实验参考答案MATLAB程序设计与应用第二版实验参考答案%实验一 MATLAB运算根底%第一题%1z1 2*sin 85*pi/180 / 1+exp 2 %2x 2,1+2i;-0.45,5;z2 0.5*log x+sqrt 1+x.2 % 3 a -3.0:0.1:3.0;z3 exp 0.3*a -exp -0.3*a /2.*sin a+0.3 +log 0.3+a /2 % 4 ;z4 t.2.* t 0&t 1 + t.2-1 .* t 1&t 2 + t.2-2*t+1 .* t 2&t 3 %第二题A 12 34 -
2、4;34 7 87;3 65 7;B 1 3 -1;2 0 3;3 -2 7;A+6*BA-B+eye size A A*BA.*BA3A.3A/BBAA,BA 1,3,: ;B2%第三题A 1 2 3 4 5;6 7 8 9 10;11 12 13 14 15;16 17 18 19 20;21 22 23 24 25B 3 0 16;17 -6 9;0 23 -4;9 7 0;4 13 11C A*BF size C D C F 1 -2:F 1 ,F 2 -1:F 2 whos%第四题% 1 :A 100:999;B rem A,21 ;C length find B 0 % 2 :A
3、'lsdhKSDLKklsdkl'k find A 'A'&A 'Z' ;A k %实验二第一题E eye 3 ;R rand 3,2 ;O zeros 2,3 ;S diag 2,3 ;A E,R;O,S;A2B E, R+R*S ;O,S2%第二题H hilb 5 P pascal 5 Hh det H Hp det P Th cond H Tp cond P %第三题:A fix 10*rand 5 H det A Trace trace A Rank rank A Norm norm A 第四题:A -29,6,18;20,5,1
4、2;-8,8,5V,D eig A %数学意义略第五题:% 1 :A 1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6;b 0.95,0.67,0.52'x inv A *b% 2 :B 0.95,0.67,0.53'x inv A *B% 3 :cond A %第五题:A hilb 4 A :,1 A 4,: B 0.95,0.67,0.52'X inv A *BB1 0.95,0.67,0.53'X1 inv A *B1N cond B N1 cond B1 Na cond A %矩阵A为病态矩阵第题A 1,4,9;16,25,36;4
5、9,64,81B sqrtm A C sqrt A %sqrtm函数是以矩阵为单位进行计算,sqrt函数是以矩阵中的元素进行计算%实验三 选择程序结构设计%第一题x -5.0,-3.0,1.0,2.0,2.5,3.0,5.0;y ; %建立存放所有y值的矩阵for x0 x if x0 0&x0 -3 y y,x0*x0+x0-6; elseif x0 0&x0 5&x0 2&x0 3 y y,x0*x0-5*x0+6; else y y,x0*x0-x0-1; endendx %输出所有xy %输出所有y%第一题程序二x -5,-3,1,2,2.5,3,5;y
6、 ;for a 1:7if x a 0&x a -3y y, x a 2+x a -6;elseif x a 0&x a 5&x a 2&x a 3y y, x a 2-5*x a +6;elsey y,x a *x a -x a -1;endend%第二题:x input '请输入一个百分制成绩:' ;if x 100|x 0disp '您输入的成绩不是百分制成绩,请重新输入。' ;elseif x 100&x 90disp 'A' ;elseif x 89&x 80disp 'B'
7、; ;elseif x 79&x 70disp 'C' ;elseif x 69&x 60disp 'D' ;elsedisp 'E' ;endend%第二题s input '请输入一个成绩0分到100分之间:' ; %s用于存放成绩while 1 %判断输入成绩的合理性 if s 0|s 100 disp '输入的成绩需在0到100之间,请重新输入:' s input '请输入一个成绩0分到100分之间:' ; else break; endendswitch fix s/10 %
8、对成绩做出等级判断 case 9,10 disp 'A' case 8 disp 'B' case 7 disp 'C' case 6 disp 'D' otherwise disp 'E' end%第三题n input '请输入员工工号:' ;h input '该员工工作时数是:' ;if h 120x h-120 *84* 1+0.15 +120*84;elseif h 60x h*84-700;elsex h*84;enddisp num2str n ,'号员工'
9、;'的应发工资为'x ;%第四题还可以用switch语句实现a fix 10+ 99-10 *rand 1,2 %产生两个随机整数x a 1 ;y a 2 ;t input '请输入运算符号:','s' ;if t '+' z x+y;elseif t '-' z x-y;elseif t '*' z x*y;elseif t '/' z x/y;enddisp num2str x ,t,num2str y ,' ',num2str z %输出运算结果%第五题a ra
10、nd 5,6 %产生5x6的随机矩阵n input '请输入您要输出矩阵的第几行:' ;if n 5 disp '超出了矩阵的行数,矩阵的最后一行为:' a 5,: else disp '矩阵的第',num2str n ,'行为:' a n,: end实验四第一题s 0;n input 'n ?' ;for i 1:n s s+1/i/i;endPI sqrt 6*s pi%第一题n input 'n ?' ;a 1:n;b 1./a.2;PI sqrt 6*sum b pi%第二题y 0;n 1;
11、while y 3 y y+1/ 2*n-1 ;n n+1;endy y-1/ 2* n-1 -1 n n-2%第题 input 'a ?' ;b input 'b ?' ;Xn 1;Xn1 a/ b+Xn ;n 0;while abs Xn1-Xn 1e-5 Xn Xn1; Xn1 a/ b+Xn ; n n+1; if n 500 break; endendnXn1r1 -b+sqrt b*b+4*a /2r2 -b-sqrt b*b+4*a /2%第四题for i 1:100if i 1f i 1;elseif i 2f i 0;elseif i 3f i
12、 1;elsef i f i-1 -2*f i-2 +f i-3 ;endend f min f sum f length find f 0 length find f 0 length find f 0 %第五题:s 0;n 0;for i :49b i* i+1 -1;m fix sqrt b ;for j 2:mif rem b,j 0breakendendif j mn n+1;s s+b;endendns%实验五 函数文件%第一题y exp x ,log x ,sin x ,cos x ;%在命令窗口调用上述函数文件:y mat1 1+i %第二题function a,b,N,M s
13、hiyanwu2 m,n,t A m*cos t*pi/180 ,-m,-sin t*pi/180 ,0;m*sin t*pi/180 ,0,cos t*pi/180 ,0;0,n,-sin t*pi/180 ,0;0,0,-cos t*pi/180 ,1;B 0,9.8*m,0,9.8*n;C inv A *B'a C 1 ;b C 2 ;N C 3 ;M C 4 ;%在命令窗口调用该函数文件m1 input 'm1 ' ;m2 input 'm2 ' ;theta input 'theta ' ;a1,a2,N1,N2 shiyanw
14、u2 m1,m2,theta %第二题function X mat2 m1,m2,t g 9.8;A m1*cos t*pi/180 ,-m1,-sin t*pi/180 ,0;m1*sin t*pi/180 ,0,cos t*pi/180 ,0;0,m2,-sin t*pi/180 ,0;0,0,-cos t*pi/180 ,1;B 0;m1*g;0;m2*g;X inv A *B;%在命令窗口调用该函数文件:X mat2 1,1,60 %第三题function flag mat3 x flag 1;for i 2:sqrt x if rem x,i 0 flag 0; break;ende
15、nd%在命令窗口调用该函数文件:for i 10:99j 10*rem i,10 +fix i/10 ;if mat3 i &mat3 j disp i endend第四题function y fx x y 1./ x-2 .2+0.1 +1./ x-3 .4+0.01 ;%在命令窗口调用该函数文件:y 2 a 1,2;3,4;y fx a %第五题 1 function f1 mat5 n f1 n+10*log n*n+5 ;%在命令窗口中调用该函数文件:y mat5 40 / mat5 30 +mat5 20 2 方法一function f2 mat6 n f2 0;for i
16、1:n f2 f2+i* i+1 ;end%在命令窗口中调用该函数文件如:y mat6 40 / mat6 30 +mat6 20 2 方法二function f2 mat7 n i 1:n;m i.* i+1 ;f2 sum m ;end%在命令窗口中调用该函数文件如:y mat 40 / mat7 30 +mat7 20 %实验六第一题:x linspace 0,2*pi,101 ;y 0.5+3*sin x ./ 1+x.2 .*cos x ;plot x,y 第二题:1x linspace -2*pi,2*pi,100 ;y1 x.2;y2 cos 2*x ;y3 y1.*y2;plo
17、t x,y1,'b-',x,y2,'r:',x,y3,'y-' ;text 4,16,'leftarrow y1 x2' ;text 6*pi/4,-1,'downarrow y2 cos 2*x ' ;text -1.5*pi,-2.25*pi*pi,'uparrow y3 y1*y2' ;%2x linspace -2*pi,2*pi,100 ;y1 x.2;y2 cos 2*x ;y3 .*y2;subplot 1,3,1 ;%分区plot x,y1 ;title 'y1 x2'
18、; ;%设置标题subplot 1,3,2 ;plot x,y2 ;title 'y2 cos 2*x ' ;subplot 1,3,3 ;plot x,y3 ;title 'y3 x2*cos 2*x ' ;%3x linspace -2*pi,2*pi,20 ;y1 x.2;subplot 2,2,1 ;%分区bar x,y1 ;title 'y1 x2的条形图' ;%设置标题subplot 2,2,2 ;stairs x,y1 ;title 'y1 x2的阶梯图' ;subplot 2,2,3 ;stem x,y1 ;tit
19、le 'y1 x2的杆图' ;subplot 2,2,4 ;fill x,y1,'r' ;%如果少了'r'那么会出错title 'y1 x2的填充图' ;%其他的函数照样做。%第三题x -5:0.01:5;y ;%起始设y为空向量for x0 xif x0 0 %不能写成x0 0y y, x0+sqrt pi /exp 2 ; %将x对应的函数值放到y中elsey y,0.5*log x0+sqrt 1+x02 ;endendplot x,y %第题:a input 'a ' ;b input 'b
20、9; ;n input 'n ' ;t -2*pi:0.01:2*pi;r a*sin b+n*t ;polar t,r %第五题x linspace -5,5,21 ;y linspace 0,10,31 ;x,y meshgrid x,y ;%在-5,5*0,10的范围内生成网格坐标z cos x .*cos y .*exp -sqrt x.2+y.2 /4 ;subplot 2,1,1 ;surf x,y,z ;subplot 2,1,2 ;contour3 x,y,z,50 ;%其中50为高度的等级数,越大越密第六题ezsurf 'cos s *cos t
21、39;,'cos s *sin t ','sin s ',0,0.5*pi,0,1.5*pi ; %利用ezsurf隐函数shading interp %进行插值着色处理%实验第一题h figure 'MenuBar','figure','color','r','WindowButtonDownFcn','disp ''Left Button Pressed'' ' %第二题x -2:0.01:2;y x.2.*exp 2*x ;h l
22、ine x,y ;set h,'color','r','linestyle',':','linewidth',2 text 1,exp 2 ,'y x2*exp 2*x ' %第三题t 0:0.00001:0.001;t,x meshgrid t ;v 10*exp -0.01*x .*sin 2000*pi*t-0.2*x+pi ;axes 'view',-37.5,30 ;h surface t,x,v ;title 'v 10*exp -0.01*x .*sin 200
23、0*pi*t-0.2*x+pi ' ;xlabel Ct' ,ylabel 'x' ,zlabel 'v' %第四题x 0:0.01:2*pi;y1 sin x ;y2 cos x ;y3 tan x ;y4 cot x ;subplot 2,2,1 ;plot x,y1 ;subplot 2,2,2 ;plot x,y2 ;subplot 2,2,3 ;plot x,y3 ;subplot 2,2,4 ;plot x,y4 ;%第五题cylinder 5 ;light 'Position',0,1,1 ;material shi
24、ny%实验八第一题 1 A rand 1,30000 ;b mean A std A,0,2 % 2 A min A % 3 n 0;for i 1:30000n n+1;endendp n/30000%第二题 1 A 45+51*rand 100,5 ;Y,U A a,b min A % 2 m mean A s std A % 3 sum A,2 Y,U ans a,b min ans % 4 zcj,xsxh sort ans %第三题h 6:2:18;x 6.5:2:17.5;t1 18,20,22,25,30,28,24;t2 15,19,24,28,34,32,30;T1 spli
25、ne h,t1,x T2 spline h,t2,x %第四题x 1:0.1:101;y1 log10 x ;p polyfit x,y1,5 y2 polyval p,x ;plot x,y1,':',x,y2,'-' %第五题 1 p1 1,2,4,0,5;p2 1,2;p3 1,2,3;p p1+0,conv p2,p3 %为使两向量大小相同,所以补0% 2 A roots p % 3 A -1,1.2,-1.4;0.75,2,3.5;0,5,2.5;polyval p,A % 4 polyvalm p,A 1XXXXXXXXXX 石虎 实验九2. 1 f
26、 inline sqrt 'cos t.2 +4*sin 4*t.2 +1' ;I1 quad f,0,2*pi 2 f2 inline 'log 1+x ./ 1+x.2 ' ;I2 quad f2,0,1 3. 1 A 6,5,-2,5;9,-1,4,-1;3,4,2,-2;3,-9,0,2;b -4,13,1,11'x Ab 2 A 6,5,-2,5;9,-1,4,-1;3,4,2,-2;3,-9,0,2;b -4,13,1,11'L,U lu A ;x U Lb 3 A 6,5,-2,5;9,-1,4,-1;3,4,2,-2;3,-9,0
27、,2;b -4,13,1,11'Q,R qr A ;x R Qb 4.functionx,y line_solution A,b m,n size A ;y ;if norm b 0 if rank A rank A,b if rank A n disp '原方程组有唯一解x' ; x Ab; else disp '原方程组有无穷个解,特解为x,其其次方程组的根底解系为y' ; x Ab; y null A,'r' ; end else disp '方程组无解' ; x ; endelse disp '方程组有零解
28、x' ; x zeros n,1 ; if rank A n disp '方程组有无穷个解,根底解系为y' ; y null A,'r' ; endendA 2,7,3,1;3,5,2,2;9,4,1,7;b 6,4,2'x,y line_solution A,b ;5.function f fz x f 3*x+sin x -exp x ;f1 fsolve 'fz',1.5 function F myfun X x X 1 ;y X 2 ;z X 3 ;F 1 sin x +y2+log z -7;F 2 3*x+2y-z3+
29、1;F 3 x+y+z-5;X fsolve 'myfun',1,1,1 6. 2 %建立函数文件 fx 1 x 2 .mfunction f fx1x2 u x1 u 1 ;x2 u 2 ;x3 u 3 ;f 2*x 1 .3+4*x 1 * x2 .3-10*x 1 *x 2 +x 2 .2;x,fmin fminsearch 'fx 1 x 2 ',0,0 7.function xdot sys x,y xdot 5*y 1 -y 2 /x;y 1 ;x0 0;xf 10;x,y ode45 'sys',x0,xf,0,0 ;x,yans
30、0 0 0 0.2500 NaN NaN 0.5000 NaN NaN 0.7500 NaN NaN 1.0000 NaN NaN 9.0000 NaN NaN 9.2500 NaN NaN 9.5000 NaN NaN 9.7500 NaN NaN 10.0000 NaN NaN实验十1XXXXXXXXXX 石虎1.x sym '6' ;y sym '5' ;z ' x+1 / sqrt 3+x -sqrt y 'eval z ans -7/ 5 1/2 - 3 2.1syms x y;A x4-y4;factor A ans x-y * x+
31、y * x2+y2 3.2syms x;s 4*x2+8*x+3 / 2*x+1 ;simplify s ans 2*x+35.2syms x;f sqrt pi -sqrt acos x /sqrt x+1 ;limit f,x,-1,'right' ans -Inf3syms x;y 1-cos 2*x /x;diff y,x diff y,x,2 ans 2*sin 2*x /x- 1-cos 2*x /x2ans 4*cos 2*x /x-4*sin 2*x /x2+2* 1-cos 2*x /x355syms x y f x2-2*x *exp -x2-y2-x*y
32、;f1 -diff f,x /diff f,y f2 diff diff f,x ,y ;x sym '0' ;y sym '1' ;eval f2 f1 - exp x2 + x*y + y2 * exp - x2 - x*y - y2 * 2*x - 2 + exp - x2 - x*y - y2 * - x2 + 2*x * 2*x + y / - x2 + 2*x * x + 2*y ans 4*exp -1 6.2syms x;f 1/ asin x .2*sqrt 1-x2 ;int f ans -1/asin x 4syms x;f exp x *
33、 1+exp x .2 ;int f,0,log 2 ;eval ans ans 实验十一1. 2 syms n xs1 symsum n2*x n-1 ,n,1,inf s2 symsum n2/5n,n,1,inf ;eval s2 s1 -x-1 / x-1 3ans 2.syms xf log x ;taylor f,6,1 ans x-1-1/2* x-1 2+1/3* x-1 3-1/4* x-1 4+1/5* x-1 53. 1 f sym 'log 1+x -5/ 1+sin x 2' ;x solve f 4x,y solve 'sqrt x2+y2
34、-100 0','3*x+5*y-8 0','x,y' x 12/17 - 10*21246 1/2 /17 10*21246 1/2 /17 + 12/17y 6*21246 1/2 /17 + 20/17 20/17 - 6*21246 1/2 /174.1符号解y dsolve 'D2y+4*Dy+29*y','y 0 0','Dy 0 15','x' y 3*sin 5*x *exp -2*x 2数值解function xdot sys x,y xdot -4*y 1 -29*y 2
35、 ;y 1 ;x0 0;xf 10;x,y ode45 'sys',x0,xf,15,0 ;x,yans 0 15.0000 0 5.x,y,z dsolve 'Dx 2*x-3*y+3*z','Dy 4*x-5*y+3*z','Dz 4*x-4*y+2*z','t' x exp -t * C2 + C1*exp 3*t y exp -2*t * C3 + C2*exp t + C1*exp 4*t z exp -2*t * C3 + C1*exp 4*t 实验十二1XXXXXXXXXX 石虎2.figure
36、39;Name','控制背景颜色和大小','NumberTitle','off','MenuBar','none' ;hc uicontextmenu;hsize uimenu hc,'Label','大小' ;hcor uimenu hc,'Label','颜色' ;uimenu hsize,'Label','变大','Call','set gcf,''Position&
37、#39;',1,1,800,600 ;' ;uimenu hsize,'Label','变小','Call','set gcf,''Position'',1,1,400,300 ;' ;uimenu hcor,'Label','红色','Call','set gcf,''Color'',''red'' ;' ;uimenu hcor,'Label
38、39;,'蓝色','Call','set gcf,''Color'',''blue'' ;' ;uimenu hcor,'Label','黄色','Call','set gcf,''Color'',''yellow'' ;' ;set gcf,'UIContextMenu',hc ;4.hf figure 'Position'
39、,100,200,300,200,. 'Name','数值增加','NumberTitle','off','MenuBar','none' ;AG uicontrol hf,'Style','edit','Position',106,10,30,50 ;AP 'n str2num get AG,''String'' ;','n n+5;','set AS,''str
40、ing'',n ;'uicontrol hf,'Style','push','Position',20,40,80,20,. 'String','Add Five','Call',AP ;AS uicontrol hf,'Style','text','Position',150,10,30,50 ;uicontrol hf,'Style','push','Position',20,
41、15,80,20,.'String','Quit','Call','close hf ' ;5.theta 0:pi/100:2*pi;a input '请输入a ' ;b input '请输入b ' ;n input '请输入c ' ;rho a*cos b+n*theta ;polar theta,rho,'m' %实验第题function zp fs z,n zp 0;z 0;for k 1:n;z 1+imag z -1.4*real z 2+i*real z *0.3;zp zp,z;end%在主窗口调用该函数:n 30000;z 0;zp fs z,n ;plo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 兑酒店合同范本
- 代价公司合同范本
- 医疗保密协议合同范本
- 医疗卫生中心项目目标与定位
- 全球中高端女装市场现状
- 农业机器人在精准农业中的应用
- 跨学科与复合型人才培养的必要性
- 国内外数字经济智慧谷发展现状
- 新型储能设备项目可行性研究报告
- 南京2025年江苏南京大学计算机学院准聘长聘岗位招聘笔试历年参考题库附带答案详解
- 深度解读DeepSeek技术体系
- 2024-2025年第二学期团委工作计划(二)
- 骆驼养殖开发项目可行性报告设计方案
- 物理-河南省郑州市2024-2025学年高二上学期期末考试试题和答案
- 《幼儿教育政策与法规》教案-单元3 幼儿园的开办与管理
- 南通市2025届高三第一次调研测试(一模)生物试卷(含答案 )
- 苏教版科学五年级下册全册教案(含反思)
- 新时代中国特色社会主义理论与实践2024版研究生教材课件全集6章
- 水下抛石施工方案
- 《由宋城集团的成功》课件
- (2024)湖北省公务员考试《行测》真题及答案解析
评论
0/150
提交评论