MATLAB程序设计实验

上传人:痛*** 文档编号:64334315 上传时间:2022-03-21 格式:DOC 页数:25 大小:408KB
返回 下载 相关 举报
MATLAB程序设计实验_第1页
第1页 / 共25页
MATLAB程序设计实验_第2页
第2页 / 共25页
MATLAB程序设计实验_第3页
第3页 / 共25页
点击查看更多>>
资源描述
科学计算与数据处理实验报告学号姓名实验名称MATLAB程序设计实验实验目的1 .掌握M文件的编辑、调试和性能剖析方法2.掌握MATLAB脚本和函数的设计方法3.掌握MATLAB程序的基本语法、关系运算和控制结构4.掌握串演算函数的用法实验方案1 .编写两段简短的代码,演示MATLAB脚本和函数的不同:脚本和函数的不同点是:函数需要通过fun ction来声明,一般有输入参数和输出参数。脚本是按顺序一条一条命令逐行执行。编与一个函数文件myfun .m和一个脚本文件 myfun script.m.来演示脚本和函数的不冋。2.编写程序演示控制结构的用法:编写程序分别来演示循环(for、while )、分支(if-else-end 、switch-case )和异常处理(try-catch )控制结构的用法。3.编与程序分别演示以下命令的功能。return的返回功能,in put的输入功台匕 冃匕。keyboard 语句常用在程序调试和运行中的变量修改。用户在程序中使用keyboard语句,在系统执行此语句时,将停止文件的执行,显示提示符K ”并把控制权交给键盘,等待用户的输入。当pause语句执行时,系统暂停执行,等待用户按任意键继续执行。break终止本次循环,跳出最内层循环中剩下的语句。error输出错误信息。lasterr显示或返回上一条出错的信息。warning遇到错误需要给使用者必要的提示,warni ng语句是常见的警告提示语句。lastwar n存放最新的警告信息。4 串演算实验:设计实验演示以下函数的功能和用法。eval是执行包含MATLAB表达式/命令的字符串,feval是调用某个函数的另一个方法。5 程序调试实验:对程序错误进行调试,总结调试过程,指出并改正所给程 序的出错语句,记录程序出错时和调通后显示的图形。6 .程序性能剖析实验:利用剖析器(profile viewer )对relaxzzy(300)进行运行分析,找出relaxzzy函数中最耗时的语句,记录其执行时间、被调用次数, 并指出该语句是否可以被加速。实验记录1 .编写两段简短的代码,演示 MATLAB 脚本和函数的不同(1 )函数文件:文件名:myfun .mfun ctio n y =myfu n( a,b)disp(spri ntf(My first in put is %s,i nputin ame(1)disp(spri ntf(My sec ond in put is %s,i nput name(2)y=a+b;运行结果:x=5;y=6;y=m yfun(x,y)My first in put is xMy sec ond in put is yy =11(2 ) 脚本文件:文件名:myfun script.ma=2;b=2;clf;x=-a:0.2:a;y=-b:0.2:b;for i=1:le ngth(y)for j=1:le ngth(x)if x(j)+y(i)1z(i,j)=0.5457*exp(-0.75*y(i)A2-3.75*x(j)A2-1.5*x(j);elseif x(j)+y(i) syms n fact_n fact_ n=1; n=in put(please en ter a positive in teger:);please en ter a positive in teger:5 fact_ n=1; for ii=1: nfact_n=fact_n *ii;end fprintf(The factional function of %f is :%fn,n,fact_n);The factional function of 5.000000 is :120.000000(2) While 循环:clearsyms x n sum_x sum_x2 xbar std_devn=0;sum_x=0;sum_x2=0;x=in put(Please en ter the first sample:);while isnu meric(x)&isempty(x)=0n=n+1;xmat( n)=x;sum_x=sum_x+x;x=in put(Please en ter next sample);endx_bar=sum_x/ n;for ii=1: nsum_x2=sum_x2+(xmat(ii)-x_bar)A2;endstd_dev=sqrt(sum_x2/n);fprin tf(The nu mber of data points is :%fn, n); fprintf(The mean of this data set is :%fn,x_bar); fprin tf(The sta ndard deviati on is :%fn ,std_dev);运行结果:Please en ter the first sample:12Please en ter n ext sample24Please en ter n ext sample12Please en ter n ext sample23Please en ter n ext sample23Please en ter n ext samplelPlease en ter n ext sample45Please en ter n ext sample45Please en ter n ext sample45Please en ter n ext sample21Please en ter n ext sample12Please en ter n ext samplellPlease en ter n ext samplelllPlease en ter n ext samplellPlease en ter n ext sample45Please en ter n ext sample45Please en ter n ext sample45Please en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplelPlease en ter n ext samplellPlease en ter n ext sampleThe number of data poi nts is :53.000000The mean of this data set is :10.886792The standard deviation is :19.950562(3) if-else-end 分支:clcclearsyms nu mber1 nu mber2 sum2 nu mbers sub2 nu mbersnu mber仁 in put(please en ter nu mber1 as :);nu mber2=in put(please en ter nu mber2 as:);n=in put(if you want to execute sum calculati on,you should en ter1,otherwise en ter -1;);if n=1fprin tf(the sum of the two nu mbers %fn and %fnis%fn, nu mber1, nu mber2, nu mber1 + nu mber2 );elseif n=-1fprin tf(the sum of the two nu mbers %fn and %fn is %fn,nu mberl, nu mber2 , nu mberl- nu mber2 );end运行结果:please en ter nu mberl as :5please en ter nu mber2 as:7if you want to execute sum calculati on,you should en ter 1,otherwise en ter -1;1the sum of the two numbers 5.000000and 7.000000is 12.000000(4)switch 分支:clearsyms scorescore=in put(please en ter the score of the stude nt subject that you want to kon w,s);switch(score)case A,adisp(?这门课你做的非常好);case B,bdisp(良好);caseC,cdisp(中等);caseD,ddisp(及格);caseE,edisp(对不起,你需要重修!)otherwisedisp(对不起,输入错误!)end运行结果:please en ter the score of the stude nt subject that you want to know a该门课你做的非常好!(5) try-catch 异常处理:clearclcA=sym(i nput(please en ter matrix A=,s)B=sym(i nput(please en ter matrix B=,s)trydisp(The multiplicatio n of A and B is)C=A*Bcatchtrydisp(The dolt multiplicati on of A and B is)D=A.*Bcatchdisp(The two matrices A and B can not be executed matrix anddot multiplicati on.);endend运行结果:输入正确时的运行结果:please en ter matrix A=2 3 4 5A =2, 3, 4, 5please enter matrix B=3 4 5 6B =3, 4, 5, 6The multiplicati on of A and B isThe dolt multiplicati on of A and B isD =6, 12, 20, 30输入错误时的运行结果:please en ter matrix A=2 3 4 5A =2, 3, 4, 5please en ter matrix B=3 4B =3, 4The two matrices A and B can not be executed matrix and dot multiplicatio n.3.编写程序分别演示以下命令的功能:(1) return 的功能和用法:s=0;for i=1:50s=s+i;if s=500disp(您要的结果是:)disp(s)returnendend运行结果:您要的结果是:528in put的功能和用法:clccleara=input(请输入数字);if a0disp(输入的大于零)elseif a=0disp(输入的等于零)elsedisp(输入的小于零)end运行结果:请输入数字:1输入的大于零(3) keyboard的功能和用法:clearclcx=i nput(输入被除数:);y=i nput(输入除数:);if y=0errordlg(除数不能为O);keyboardn=x/y;disp(您要的结果是:) disp( n)elsen=Xy;disp(您要的结果是:) disp( n)end运行结果:输入被除数:5输入除数:0K 1ans =1(4) pause的功能和用法:x=-pi:pi/10:piy1=cos(x);subplot(1,2,1);plot(x,y1);pause(5)y2=si n( x);subplot(1,2,2);plot(x,y2);运行结果:110.8L0.8-0.6L-0.6-0.411110.4-1 |I0.2-|1110.2-111-0-s0-0.2-r-0.2-i-0.4J1111-0.4-111r1 i i-0.6L11-0.6-0.8-0.8- /-1_FifF-1LF4-202-11-2024(5) break的功能和用法for n=1:100if mod( n,6)=0con ti nueendbreakenddisp(小于100的自然数中第一个能被6整除的数是:)disp( n)运行结果:小于100的自然数中第一个能被6整除的数是:6warning的功能和用法:clearclcx=i nput(输入被除数:);y=i nput(输入除数:);if y=0warning(除数不能为O);elsen=x/y;disp(您要的结果是:) disp( n)end运行的结果:输入被除数:45输入除数:0Warning:除数不能为0!(7) error的功能和用法:clearclcx=i nput(输入被除数:);y=i nput(输入除数:);if y=0error(除数不能为0);elsen=Xy;disp(您要的结果是:)disp( n)end运行的结果:输入被除数:45输入除数:0?除数不能为0!(8) lasterr的功能和用法: lasterrans =除数不能为0(9) lastwarn的功能和用法: warni ng(被除数不能为0);Warni ng:被除数不能为0 warning(除数不能为0);Warning:除数不能为0 lastwar nans =除数不能为04.串演算实验:(1) eval的功能和用法:x=-pi:pi/10:pi;subplot(2,1,1);eval(plot(cos(x);subplot(2,1,2);eval(plot(s in (x)运行结果:-1-10.50-0.551015202500.50-0.55101520250(2) feval的功能和用法: temp=feval(feval2,3,5);My first in put is 3My seco nd in put is 5 temptemp =文件名:feval2.mfunction y = feval2(a,b)disp(spri ntf(My first in put is %s,i nput name(1) disp(spri ntf(My sec ond in put is %s,i nput name(2) y=a+b;5.程序调试实验:(1 )出错时:运行结果: randn( seed,1); x=ra ndn (1,100); Debug_Test(x);? Error using = plotVectors must be the same len gths.Error in = Debug_Testbarzzy0 at 18 plot(xxmu,yy,r,Li newidth,3)Error in = Debug_Test at 7barzzy0( nn ,xx,xmu,xstd)(2 )改正后:把原程序中的 xxmu=xmu*size(yy) 改为 xxmu=xmu*ones(size(yy)。运行结果: randn( seed,1),x=ra ndn (1,100);Debug_Test(x)ans =134162119209616.程序性能剖析实验relaxzr, i1 call. 2.640 sec)Sewraladf ushg 羊l.折湘.M-lunctia n n t Is F Uu4MbL*l 自工 rH #. hrCdPY H r阳冑 wind囹 fcif 匚口巾口可曲口 mubiplo2&亡启hEl 3hoxf parenl kindionE E Show busy Ines 3 Shnw chid LncliansFl Sh dw M-Liil rat ull s 回 Sh mw fik caiprqp VI Stow Innclion listingIPdi&iiit& (c-dillihq irjmztiQnG)Nto p刃e就Lin处 lias# 山。moi lima wa 耳畑inLne klumbErCodeCailTtftal Time% TimeTime PlkzdIBnwplate (j pk)B (pl r(JuLkL) /.3000000ojesr s2.7%2EdtanrniduGQ0jG4Gs621ti3Lace .i;1 63t =23 9%endLOOJQQO1124 s47K130elose1D.UGs4.3%1All ofther Ipnei0 474 s17 3%T&talg2j64O3100%Fim cllioii I hl h i gDolor Nghiighi code according to j“ vtimp FAlliB liwJ. fuzicLan c 1 apc 1 wscy (tc);卡 fa idi wininramE|.jK6A 01丄m 041 - 011Ol 631Q_ LIE1 0.011OL USiC 0,011 a+ oiIC 0l 0130DC 0.013005D D. 01300DDQ, 0Zgnnnnj.ir30000DO0,113000Q5D+6E3QOOQ9D3 se=-1D2;neTrpL ate=plate ; m-magE (p Jat-e I ;&3(L S 3itt_4 platE=maqi (az I/ (sz*3E);H se*(cjief r !DitLbJ.eJBuei:1 j, on i 处寸_rnrot *: tg-1) tal-h-l;11J-pLK+1;_空end药p Late netip Lite;4Lf (0=-ieu(l,5n列set (mF 1 data1 丄&t)2dl-CLlLUUand0.1230000030,01300000.013QD0.0130D0. OiD0,05SD0. 01&Dc001ODDa. oi10.11121end10 tic2D end聘 i* lspr:toe;JE “卩兀由上例图可知,relaxzzy函数中最耗时的语句是newplate(j,k)=(plateam1,km1)/2+plate(jm1,k)+.plate(jm1,kp1)/2+plate(j,km1)+plateQ,kp1)+plate(jp1,km1)/2+plate(jp1,k)+plateQp1,kp1)72)/6其执行时间为0.651 s ,被调用次数为3000000次。这个语句可以加速,用过 向量化改为矩阵的形式,就可以加速了。实验总结(1) MATLAB脚本和函数的异冋点:M脚本文件与 M函数文件都是由一系列的语句组成的相对独立的一个运行体。M脚本文件没有参数传递功能,但M函数文件有此功能。M函数文件的格式有严格规定,它必须以fun ctio n开头。函数文件与脚本文件的主要区别在于:函数文件一般都要带参数,都要有返回结果,而脚本文件没有参数与返回结果。函数文件的变量是局部变量,运行期间有效,运行完毕就自动被清除,而脚本文件的变量是全局变量,执行完毕后仍被保存在内存中。(2) MATLAB程序调试和性能剖析的方法:程序调试的基本方法有:设置或清除断点,单步执行,step in当遇见函数时进入函数内部,step out执行流程跳出函数,观察变量或表达式的值。性能剖析的方法有:简单计算程序 运行时间:tic , toc。全面分析程序运行时间Profiler 。(3) 提高MATLAB 程序运行速度的有效措施:要学好matlab,有效地使用matlab,能不用循环的地方,尽量不要使用。向量化,使用C-mex写核心。(4) 通过这次试验我掌握了M文件的编写,运行和调试。知道了函数文件和脚本文件的相冋点和不冋点。冋时也掌握了matlab程序设计中的基本语法,关系运算,以及for和while循环结构,if-else-end 和switch的分支结构 和异常处理的try-catch 结构。通过自己调试一个程序,明白了程序调试的基本方法。自己通过对一个程序的性能剖析,知道了性能剖析的基本方法。成绩评定注:实验方案要详略得当、条理清晰,实验记录数据详实,实验总结应做高度归纳和提炼。
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 成人自考


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!