實(shí)驗(yàn)五 MATLAB程序設(shè)計(jì)-_第1頁
實(shí)驗(yàn)五 MATLAB程序設(shè)計(jì)-_第2頁
實(shí)驗(yàn)五 MATLAB程序設(shè)計(jì)-_第3頁
實(shí)驗(yàn)五 MATLAB程序設(shè)計(jì)-_第4頁
實(shí)驗(yàn)五 MATLAB程序設(shè)計(jì)-_第5頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、掌握MATLAB程序設(shè)計(jì)的主要方法,熟練編寫MATLAB函數(shù)。二.實(shí)驗(yàn)內(nèi)容(1M文件的編輯。(2程序流程控制結(jié)構(gòu)。(3子函數(shù)調(diào)用和參數(shù)傳遞。(4局部變量和全局變量。三.實(shí)驗(yàn)步驟1.M文件的編輯選擇MATLAB的菜單FileNewM-file,打開新的M文件進(jìn)行編輯,然后輸入以下內(nèi)容,并保存文件名為exp1.m。s=0;for n=1:100s=s+n;ends結(jié)果如下:>> exp1s =5050保存好文件后,在命令窗口輸入exp1即可運(yùn)行該腳本文件,注意觀察變量空間。接著創(chuàng)建M函數(shù)文件,然后輸入以下內(nèi)容,并保存文件名為exp2.m。function s=exp2(xs=0;for

2、 n=1:xs=s+n;end保存好文件后,在命令窗口輸入>> clear>> s=exp2(1002.程序流程控制結(jié)構(gòu)1for循環(huán)結(jié)構(gòu)for n=1:10nend另一種形式的for循環(huán):n=10:-1:5for i=niend2while循環(huán)結(jié)構(gòu)在命令窗口輸入:>>clear,clc;x=1;while 1x=x*2end將會看到MATLAB進(jìn)入死循環(huán),因?yàn)閣hile 判斷的值恒為真,這時須按下Ctrl+C鍵來中斷運(yùn)行,并且可看到x的值為無窮大。3if-else-end分支結(jié)構(gòu)if-else-end分支有如下3種形式。(a if 表達(dá)式語句組1end(b

3、if 表達(dá)式語句組1else語句組2end(c if 表達(dá)式A語句組1elseif 表達(dá)式B語句組2elseif語句組3.else語句組nend4switch-case結(jié)構(gòu)創(chuàng)建M腳本文件exp3.m,輸入以下內(nèi)容并在命令窗口中運(yùn)行。n=input('n'if isempty(nerror('please input n'endswitch mod(n,2case 1A='奇數(shù)'case 0A='偶數(shù)'end運(yùn)行結(jié)果如下:>> exp3n 5A =奇數(shù)>> exp3n 10A =偶數(shù)3.子函數(shù)和參數(shù)傳遞例: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輸入?yún)?shù)可以由函數(shù)nargin計(jì)算,下面的例子sinplot2(,當(dāng)只輸入一個參數(shù)w時,sinplot2(函數(shù)會給p賦予默認(rèn)值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;運(yùn)行結(jié)果:>> 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論