版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、掌握MATLAB程序设计的主要方法,熟练编写MATLAB函数。二.实验内容(1M文件的编辑。(2程序流程控制结构。(3子函数调用和参数传递。(4局部变量和全局变量。三.实验步骤1.M文件的编辑选择MATLAB的菜单FileNewM-file,打开新的M文件进行编辑,然后输入以下内容,并保存文件名为exp1.m。s=0;for n=1:100s=s+n;ends结果如下:>> exp1s =5050保存好文件后,在命令窗口输入exp1即可运行该脚本文件,注意观察变量空间。接着创建M函数文件,然后输入以下内容,并保存文件名为exp2.m。function s=exp2(xs=0;for
2、 n=1:xs=s+n;end保存好文件后,在命令窗口输入>> clear>> s=exp2(1002.程序流程控制结构1for循环结构for n=1:10nend另一种形式的for循环:n=10:-1:5for i=niend2while循环结构在命令窗口输入:>>clear,clc;x=1;while 1x=x*2end将会看到MATLAB进入死循环,因为while 判断的值恒为真,这时须按下Ctrl+C键来中断运行,并且可看到x的值为无穷大。3if-else-end分支结构if-else-end分支有如下3种形式。(a if 表达式语句组1end(b
3、if 表达式语句组1else语句组2end(c if 表达式A语句组1elseif 表达式B语句组2elseif语句组3.else语句组nend4switch-case结构创建M脚本文件exp3.m,输入以下内容并在命令窗口中运行。n=input('n'if isempty(nerror('please input n'endswitch mod(n,2case 1A='奇数'case 0A='偶数'end运行结果如下:>> exp3n 5A =奇数>> exp3n 10A =偶数3.子函数和参数传递例:f
4、unction g=exp4(xg=0;for n=1:xg=g+fact(n;endfunction y=fact(ky=1;for i=1:ky=y*i;end输入参数可以由函数nargin计算,下面的例子sinplot2(,当只输入一个参数w时,sinplot2(函数会给p赋予默认值0。function z=sinplot (w,pif nargin>2error('too many input'endif nargin=1p=0;endx=linspace(0,2*pi,500z=sin(x.*w+p;运行结果:>> sinplotx =Columns
5、 1 through 90 0.0126 0.0252 0.0378 0.0504 0.0630 0.0755 0.0881 0.1007Columns 10 through 180.1133 0.1259 0.1385 0.1511 0.1637 0.1763 0.1889 0.2015 0.2141Columns 19 through 270.2266 0.2392 0.2518 0.2644 0.2770 0.2896 0.3022 0.3148 0.3274Columns 28 through 360.3400 0.3526 0.3652 0.3777 0.3903 0.4029 0.
6、4155 0.4281 0.4407Columns 37 through 450.4533 0.4659 0.4785 0.4911 0.5037 0.5163 0.5288 0.5414 0.5540Columns 46 through 540.5666 0.5792 0.5918 0.6044 0.6170 0.6296 0.6422 0.6548 0.6674Columns 55 through 630.6799 0.6925 0.7051 0.7177 0.7303 0.7429 0.7555 0.7681 0.7807Columns 64 through 720.7933 0.805
7、9 0.8185 0.8310 0.8436 0.8562 0.8688 0.8814 0.8940Columns 73 through 810.9066 0.9192 0.9318 0.9444 0.9570 0.9695 0.9821 0.9947 1.0073Columns 82 through 901.0199 1.0325 1.0451 1.0577 1.0703 1.0829 1.0955 1.1081 1.1206Columns 91 through 991.1332 1.1458 1.1584 1.1710 1.1836 1.1962 1.2088 1.2214 1.2340C
8、olumns 100 through 1081.2466 1.2592 1.2717 1.2843 1.2969 1.3095 1.3221 1.3347 1.3473Columns 109 through 1171.3599 1.3725 1.3851 1.3977 1.4103 1.4228 1.4354 1.4480 1.4606Columns 118 through 1261.4732 1.4858 1.4984 1.5110 1.5236 1.5362 1.5488 1.5614 1.5739Columns 127 through 1351.5865 1.5991 1.6117 1.
9、6243 1.6369 1.6495 1.6621 1.6747 1.6873Columns 136 through 1441.6999 1.7125 1.7250 1.7376 1.7502 1.7628 1.7754 1.7880 1.8006Columns 145 through 1531.8132 1.8258 1.8384 1.8510 1.8635 1.8761 1.8887 1.9013 1.9139Columns 154 through 1621.9265 1.9391 1.9517 1.9643 1.9769 1.98952.0021 2.0146 2.0272Columns
10、 163 through 1712.0398 2.0524 2.0650 2.0776 2.0902 2.1028 2.1154 2.1280 2.1406Columns 172 through 1802.1532 2.1657 2.1783 2.1909 2.2035 2.2161 2.2287 2.2413 2.2539Columns 181 through 1892.2665 2.2791 2.2917 2.3043 2.3168 2.3294 2.3420 2.3546 2.3672Columns 190 through 1982.3798 2.3924 2.4050 2.4176 2
11、.4302 2.4428 2.4554 2.4679 2.4805Columns 199 through 2072.4931 2.5057 2.5183 2.5309 2.5435 2.5561 2.5687 2.5813 2.5939Columns 208 through 2162.6065 2.6190 2.6316 2.6442 2.6568 2.6694 2.6820 2.6946 2.7072Columns 217 through 2252.7198 2.7324 2.7450 2.7576 2.7701 2.7827 2.7953 2.8079 2.8205Columns 226
12、through 2342.8331 2.8457 2.8583 2.8709 2.8835 2.8961 2.9086 2.9212 2.9338Columns 235 through 2432.9464 2.9590 2.9716 2.9842 2.99683.00943.0220 3.0346 3.0472Columns 244 through 2523.0597 3.0723 3.0849 3.0975 3.1101 3.1227 3.1353 3.1479 3.1605Columns 253 through 2613.1731 3.1857 3.1983 3.2108 3.2234 3
13、.2360 3.2486 3.2612 3.2738Columns 262 through 2703.2864 3.2990 3.3116 3.3242 3.3368 3.3494 3.3619 3.3745 3.3871Columns 271 through 2793.3997 3.4123 3.4249 3.4375 3.4501 3.4627 3.4753 3.4879 3.5005Columns 280 through 2883.5130 3.5256 3.5382 3.5508 3.5634 3.5760 3.5886 3.6012 3.6138Columns 289 through
14、 2973.6264 3.6390 3.6516 3.6641 3.6767 3.6893 3.7019 3.7145 3.7271Columns 298 through 3063.7397 3.7523 3.7649 3.7775 3.7901 3.8026 3.8152 3.8278 3.8404Columns 307 through 3153.8530 3.8656 3.8782 3.8908 3.9034 3.9160 3.9286 3.9412 3.9537Columns 316 through 3243.9663 3.9789 3.99154.0041 4.0167 4.02934
15、.0419 4.0545 4.0671Columns 325 through 3334.0797 4.0923 4.1048 4.1174 4.1300 4.1426 4.1552 4.1678 4.1804Columns 334 through 3424.1930 4.2056 4.2182 4.2308 4.2434 4.2559 4.2685 4.2811 4.2937Columns 343 through 3514.3063 4.3189 4.3315 4.3441 4.3567 4.3693 4.3819 4.3945 4.4070Columns 352 through 3604.4
16、196 4.4322 4.4448 4.4574 4.4700 4.4826 4.4952 4.5078 4.5204Columns 361 through 3694.5330 4.5456 4.5581 4.5707 4.5833 4.5959 4.6085 4.6211 4.6337Columns 370 through 3784.6463 4.6589 4.6715 4.6841 4.6966 4.7092 4.7218 4.7344 4.7470Columns 379 through 3874.7596 4.7722 4.7848 4.7974 4.8100 4.8226 4.8352
17、 4.8477 4.8603Columns 388 through 3964.8729 4.8855 4.8981 4.9107 4.9233 4.9359 4.9485 4.9611 4.9737Columns 397 through 4054.9863 4.99885.0114 5.0240 5.0366 5.04925.0618 5.0744 5.0870Columns 406 through 4145.0996 5.1122 5.1248 5.1374 5.1499 5.1625 5.1751 5.1877 5.2003Columns 415 through 4235.2129 5.2
18、255 5.2381 5.2507 5.2633 5.2759 5.2885 5.3010 5.3136Columns 424 through 4325.3262 5.3388 5.3514 5.3640 5.3766 5.3892 5.4018 5.4144 5.4270Columns 433 through 4415.4396 5.4521 5.4647 5.4773 5.4899 5.5025 5.5151 5.5277 5.5403Columns 442 through 4505.5529 5.5655 5.5781 5.5906 5.6032 5.6158 5.6284 5.6410 5.6536Columns 451 through 4595.6662 5.6788 5.6914 5.7040 5.7166 5.7292 5.7417 5.7543 5.7669Columns 460 through 4685.7795 5.7921 5.8047 5.8173 5.8299 5.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年海南事业单位招聘考试综合类公共基础知识真题试卷权威点评
- 2025年农村管理专业真题及答案
- 2025年叉车理论考试题库及答案
- 2025年宜宾网格员考试题及答案
- 幼儿园科学活动教案奇妙的声音
- 方剂学治风剂课件
- 2025年全国共青团“新团员入团”应知应会知识考试题库检测试卷带答案详解(预热题)
- 2025工业用地买卖合同模板
- 2025关于船舶YY买卖合同范本
- 金融衍生工具题库及答案
- 湖北境内旅游合同模板
- 高中信息技术学业水平考试“必修知识点”讲义
- 边坡应力分布的一般特征
- 二年级上册 乘法口诀表1000题
- 中国丝绸智慧树知到答案2024年浙江理工大学
- DZ∕T 0372-2021 固体矿产选冶试验样品配制规范(正式版)
- 铝板幕墙技术交底
- 冬季运输公司安全教育培训
- 酒店it服务流程
- 劳动创造美好生活第四章第一课磨刀不误砍柴工
- 2024年北京农商银行招聘笔试参考题库含答案解析
评论
0/150
提交评论