第5講 Matlab程序設(shè)計_第1頁
第5講 Matlab程序設(shè)計_第2頁
第5講 Matlab程序設(shè)計_第3頁
第5講 Matlab程序設(shè)計_第4頁
第5講 Matlab程序設(shè)計_第5頁
已閱讀5頁,還剩76頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、第五講 MatlabMatlab程序設(shè)計課程提綱 引子引子 M M文件介紹文件介紹 控制語句控制語句 函數(shù)變量及變量作用域函數(shù)變量及變量作用域 程序設(shè)計的輔助函數(shù)程序設(shè)計的輔助函數(shù) 程序設(shè)計的優(yōu)化程序設(shè)計的優(yōu)化 程序調(diào)試程序調(diào)試 信息接口信息接口 小結(jié)小結(jié)page2引子華氏溫度和攝氏溫度的轉(zhuǎn)換Problem: Problem: 設(shè)計一個設(shè)計一個MATLAB MATLAB 程序,讀取一個華氏溫度的程序,讀取一個華氏溫度的輸入,輸出攝氏溫度。輸入,輸出攝氏溫度。Solution:Solution:Step 1: Step 1: 清晰地陳述出你要解決的問題;清晰地陳述出你要解決的問題; “將華氏溫度

2、轉(zhuǎn)換為攝氏溫度并輸出將華氏溫度轉(zhuǎn)換為攝氏溫度并輸出”Step 2: Step 2: 確定程序的輸入變量和輸出變量;確定程序的輸入變量和輸出變量; “輸入變量輸入變量:華氏溫度;:華氏溫度;輸出變量輸出變量:攝氏溫度:攝氏溫度”Step 3: Step 3: 設(shè)計程序偽代碼;設(shè)計程序偽代碼; % % 讀取華氏溫度:讀取華氏溫度:input input T T(F)(F) % % 溫度轉(zhuǎn)換:溫度轉(zhuǎn)換: % % 輸出攝氏溫度:輸出攝氏溫度:print print T T(C)(C)5(C)( (F)32)9TTpagepage3 3Step 4: Step 4: 將偽代碼轉(zhuǎn)換為將偽代碼轉(zhuǎn)換為Matl

3、abMatlab程序語句程序語句% Script file:temp_conversion.m% Script file:temp_conversion.m% % Purpose:% Purpose:% To convert an input temperature from degrees Fahrenheit to% To convert an input temperature from degrees Fahrenheit to% an output temperature in Celsius.% an output temperature in Celsius.% % Record

4、 of revisions:% Record of revisions:% Date Programmer Description of change% Date Programmer Description of change% = = =% = = =% 21/10/13 Y.Y.Guo Original code% 21/10/13 Y.Y.Guo Original code% % Define variables:% Define variables:% temp_f -Temperature in degrees Fahrenheit% temp_f -Temperature in

5、degrees Fahrenheit% temp_k -Temperature in Celsius% temp_k -Temperature in Celsius% Prompt the user for the input temperature.% Prompt the user for the input temperature.temp_f=input(Enter the temperature in degrees Fahrenheit:);temp_f=input(Enter the temperature in degrees Fahrenheit:);% Convert to

6、 Celsius.% Convert to Celsius.temp_k=(5/9)temp_k=(5/9)* *(temp_f-32);(temp_f-32);% Write out the result.% Write out the result.fprintf(%6.2f degrees Fahrenheit = %6.2f fprintf(%6.2f degrees Fahrenheit = %6.2f Celsius.n,temp_f,temp_k);Celsius.n,temp_f,temp_k);“程序信息程序信息”“名字名字”“用途用途”“修改記錄修改記錄”“變量定義變量定義

7、”“讀取華氏溫度讀取華氏溫度”“溫度轉(zhuǎn)換溫度轉(zhuǎn)換”“輸出攝氏溫度輸出攝氏溫度”pagepage4 4Step 5: Step 5: 調(diào)試程序調(diào)試程序 run(C:Program FilesMATLABR2010abintemp_conversion.m) run(C:Program FilesMATLABR2010abintemp_conversion.m)Enter the temperature in degrees Fahrenheit:120Enter the temperature in degrees Fahrenheit:120120.00 degrees Fahrenheit

8、= 48.89 Celsius.120.00 degrees Fahrenheit = 48.89 Celsius.Enter the temperature in degrees Fahrenheit:100Enter the temperature in degrees Fahrenheit:100100.00 degrees Fahrenheit = 37.78 Celsius.100.00 degrees Fahrenheit = 37.78 Celsius.MatlabMatlab初學(xué)者總是期望,拿到問題就能寫出漂亮的代碼與完初學(xué)者總是期望,拿到問題就能寫出漂亮的代碼與完美無誤的程序

9、,而實際上這是不可能的!美無誤的程序,而實際上這是不可能的!MatlabMatlab程序設(shè)計中,寫代碼只是簡單的一半,另一半則是程序設(shè)計中,寫代碼只是簡單的一半,另一半則是問題的分析與偽代碼的設(shè)計!問題的分析與偽代碼的設(shè)計!MatlabMatlab的變量、語句與語法只是的變量、語句與語法只是血肉血肉般的簡單存在,在任何般的簡單存在,在任何一種語言中都能找到,而一個良好的編程習(xí)慣則是一種語言中都能找到,而一個良好的編程習(xí)慣則是靈魂靈魂般的般的永恒存在!永恒存在!pagepage5 5M M文件介紹MatlabMatlab是一種高級計算機語言,因而也能夠編制一種以是一種高級計算機語言,因而也能夠編

10、制一種以.m.m為為擴展名的文本文件,簡稱擴展名的文本文件,簡稱M M文件文件。MatlabMatlab是一種解釋性語言,命令在運行時被翻譯為機器語言是一種解釋性語言,命令在運行時被翻譯為機器語言被執(zhí)行,被執(zhí)行,M M文件就是文件就是命令的集合命令的集合。M M文件是純文本(文件是純文本(ASCIIASCII碼)文件,因而便于進行編寫和修改。碼)文件,因而便于進行編寫和修改。擴展名必須為擴展名必須為.m.m。M M文件分類:文件分類:p 命令式(命令式(ScriptScript):命令行的簡單疊加):命令行的簡單疊加p 函數(shù)式(函數(shù)式(FunctionFunction):參數(shù)傳遞和函數(shù)調(diào)用):

11、參數(shù)傳遞和函數(shù)調(diào)用pagepage6 6M M文件建立與編輯M M文件建立:文件建立:p Matlab Matlab窗口中菜單項窗口中菜單項FileFile-OpenOpenp 單擊圖標單擊圖標 ,打開一個,打開一個M M文件文件p 找到找到M M文件,直接雙擊打開文件,直接雙擊打開M M文件編輯:文件編輯:p Matlab Matlab窗口中菜單項窗口中菜單項FileFile-NewNew-ScriptScript/ /FunctionFunctionp 單擊圖標單擊圖標 ,新建一個,新建一個M M文件文件pagepage7 7命令式文件(ScriptScript)命令式文件的運行相當于在命

12、令窗口(命令式文件的運行相當于在命令窗口(Command WindowCommand Window)中逐行輸入并運行命令。需注意一下幾點:中逐行輸入并運行命令。需注意一下幾點: 標點符號的運用要恰當好處,每行命令結(jié)束時用分號隔標點符號的運用要恰當好處,每行命令結(jié)束時用分號隔開,以免程序運行時數(shù)據(jù)輸出造成不便;開,以免程序運行時數(shù)據(jù)輸出造成不便; 建立良好的書寫風(fēng)格,保持程序的可讀性,比如程序的建立良好的書寫風(fēng)格,保持程序的可讀性,比如程序的層次感,注釋行的書寫;層次感,注釋行的書寫; 注釋行以符號注釋行以符號% %開頭,在程序運行中不被執(zhí)行,只起解開頭,在程序運行中不被執(zhí)行,只起解釋說明作用;

13、釋說明作用; 不需要用不需要用endend語句作為命令式文件的結(jié)束標志;語句作為命令式文件的結(jié)束標志; 運行此文件時,將其目錄設(shè)置為當前目錄。運行此文件時,將其目錄設(shè)置為當前目錄。pagepage8 8實例實例1 1:建立一命令式:建立一命令式M M文件繪制文件繪制MatlabMatlab的的LOGOLOGO圖圖Life is too short to spend writing DO loops.% Script file: logotu.m% Script file: logotu.m% % Purpose:% Purpose:% This file is to create the %

14、This file is to create the Logo of Logo of % Matlab software% Matlab softwareload logoload logosurf(L,R), colormap(M)surf(L,R), colormap(M)n=size(L,1)n=size(L,1)axis offaxis offaxis(1 n 1 n -.2 .8)axis(1 n 1 n -.2 .8)view(-37.5,30)view(-37.5,30)title(Life is too short to title(Life is too short to s

15、pend writing DO loops.)spend writing DO loops.)pagepage9 9函數(shù)式文件(FunctionFunction)函數(shù)式文件可以實現(xiàn)計算中的參數(shù)傳遞。函數(shù)式的標志是第函數(shù)式文件可以實現(xiàn)計算中的參數(shù)傳遞。函數(shù)式的標志是第1 1行為行為functionfunction語句。語句。 函數(shù)式可以有返回值,函數(shù)式可以有返回值,也可有無返回值;也可有無返回值; 函數(shù)式文件名與函數(shù)函數(shù)式文件名與函數(shù)名應(yīng)名應(yīng)一一對應(yīng)一一對應(yīng); 函數(shù)式中的變量僅在函數(shù)式中的變量僅在函數(shù)內(nèi)部起作用,為局部函數(shù)內(nèi)部起作用,為局部變量;變量; 養(yǎng)成良好的注釋習(xí)慣,養(yǎng)成良好的注釋習(xí)慣,方

16、便自己,方便他人。方便自己,方便他人。functionfunction outarg1, outarg1, outarg2, . = outarg2, . = fnamefname(inarg1, inarg2, .)(inarg1, inarg2, .)% H1 comment line% H1 comment line% Other comment lines% Other comment lines.( (Executable codeExecutable code) ).(return)(return)helphelp命令運行后將顯示命令運行后將顯示M M文件注釋語句中的第一個連續(xù)塊,

17、文件注釋語句中的第一個連續(xù)塊,被空行隔離的其它注釋語句,將被忽略;被空行隔離的其它注釋語句,將被忽略;lookforlookfor顯示第一個注釋行內(nèi)容,為了便于被搜索,第一顯示第一個注釋行內(nèi)容,為了便于被搜索,第一行注釋盡可能包含特征信息。行注釋盡可能包含特征信息。pagepage1010實例實例2 2:建立求一個向量元素平均值的函數(shù):建立求一個向量元素平均值的函數(shù)function y=average(x)function y=average(x)% AVERAGE % AVERAGE 求向量元素的平均值求向量元素的平均值% % 語法語法%y=average(x)%y=average(x)%

18、% 其中,其中,x x是向量,是向量,y y為計算得到向量元為計算得到向量元素的均值素的均值% % 若輸入?yún)?shù)為非向量時則出錯若輸入?yún)?shù)為非向量時則出錯% % 代碼行代碼行m,n=size(x);m,n=size(x);% % 判斷輸入?yún)?shù)是否為向量判斷輸入?yún)?shù)是否為向量if(m=1)|(n=1)|(m=1& n=1)if(m=1)|(n=1)|(m=1& n=1) % % 若輸入?yún)?shù)不是向量,則出錯若輸入?yún)?shù)不是向量,則出錯 error(Input must be a vector!)error(Input must be a vector!)endend% % 計算向量元素的平均值計算向量

19、元素的平均值y=sum(x)/length(x);y=sum(x)/length(x); x=1:10; x=1:10; y=average(x) y=average(x)y =y = 5.5000 5.5000 x=2 4 6 8 10; x=2 4 6 8 10; y=average(x) y=average(x)y =y = 6 6pagepage1111控制語句p 順序語句順序語句p 選擇語句選擇語句p 分支語句分支語句p 循環(huán)語句循環(huán)語句p 人機交互語句人機交互語句pagepage1212順序語句MatlabMatlab程序的命令語句按固定的順序一個接一個的執(zhí)行,這程序的命令語句按固

20、定的順序一個接一個的執(zhí)行,這樣的程序稱為樣的程序稱為順序語句順序語句。% Script file: % Script file: logotu.mlogotu.m% % Purpose:% Purpose:% This file is to create the % This file is to create the Logo of Logo of % Matlab software% Matlab softwareload logoload logosurf(L,R), surf(L,R), colormapcolormap(M)(M)n=size(L,1)n=size(L,1)axis

21、offaxis offaxis(1 n 1 n -.2 .8)axis(1 n 1 n -.2 .8)view(-37.5,30)view(-37.5,30)title(Life is too short to title(Life is too short to spend writing DO loops.)spend writing DO loops.)順序語句只能實現(xiàn)簡單的功順序語句只能實現(xiàn)簡單的功能,而在解決實際問題時往能,而在解決實際問題時往往會面臨往會面臨選擇執(zhí)行特定命令選擇執(zhí)行特定命令及及重復(fù)性執(zhí)行特定命令重復(fù)性執(zhí)行特定命令的情的情形,因而需要學(xué)習(xí)選擇語句形,因而需要學(xué)習(xí)選擇語

22、句及循環(huán)語句。及循環(huán)語句。pagepage1313選擇語句選擇語句可以使選擇語句可以使MATLAB MATLAB 選擇性執(zhí)行指定區(qū)域內(nèi)的代碼選擇性執(zhí)行指定區(qū)域內(nèi)的代碼( (稱之為語句塊稱之為語句塊blocks)blocks),而跳過其他區(qū)域的代碼。,而跳過其他區(qū)域的代碼。ifif control_expr_1control_expr_1Statements (Statements (block block 1 1) )elseifelseif control_expr_2control_expr_2Statements (Statements (block block 2 2) )elseif

23、elseif control_expr_3control_expr_3Statements (Statements (block block 3 3) )elseelseStatements (Statements (block block 4 4) )endend 判斷表達式判斷表達式control_exprcontrol_expr緊跟在緊跟在ifif或或elseifelseif后面;后面; 若判斷表達式的結(jié)果為若判斷表達式的結(jié)果為1 1,則執(zhí)行其后的命令語句塊則執(zhí)行其后的命令語句塊(blockblock),若結(jié)果為),若結(jié)果為0 0,則,則跳過其后的命令語句塊;跳過其后的命令語句塊; 某一

24、命令語句塊被執(zhí)行后,某一命令語句塊被執(zhí)行后,程序跳至程序跳至endend語句后一可執(zhí)語句后一可執(zhí)行語句;行語句; elseifelseif語句可有任意個,語句可有任意個,elseelse語句最多只有一個。語句最多只有一個。pagepage1414實例實例3 3:輸入三角形三邊長,求面積:輸入三角形三邊長,求面積 % Script file: triarea.m % Script file: triarea.m% This program is to calculate the area % This program is to calculate the area of a triangula

25、rof a triangularA=input(A=input(請輸入三角形的三條邊請輸入三角形的三條邊( (數(shù)組形式數(shù)組形式) ):);); if A(1)+A(2)A(3) & A(1)+A(3)A(2) & if A(1)+A(2)A(3) & A(1)+A(3)A(2) & A(2)+A(3)A(1)A(2)+A(3)A(1) p=(A(1)+A(2)+A(3)/2; p=(A(1)+A(2)+A(3)/2; s=sqrt(p s=sqrt(p* *(p-A(1)(p-A(1)* *(p-A(2)(p-A(2)* *(p-(p-A(3);A(3); disp(s); disp(s);

26、else else disp( disp(不能構(gòu)成一個三角形。不能構(gòu)成一個三角形。) end end run(C:Program run(C:Program FilesMATLABR2010abintriarea.FilesMATLABR2010abintriarea.m)m)請輸入三角形的三條邊請輸入三角形的三條邊( (數(shù)組形式數(shù)組形式) ):3 4 53 4 5 6 6 run(C:Program run(C:Program FilesMATLABR2010abintriarea.FilesMATLABR2010abintriarea.m)m)請輸入三角形的三條邊請輸入三角形的三條邊( (

27、數(shù)組形式數(shù)組形式) ):1 2 31 2 3不能構(gòu)成一個三角形。不能構(gòu)成一個三角形。()()()Sp papbpc三角形面積三角形面積海倫公式海倫公式:1()2pabc其中:其中:pagepage1515實例實例4 4:求一元二次方程的根:求一元二次方程的根20(0)axbxcaStep 1: Step 1: 清晰地陳述出你要解決的問題;清晰地陳述出你要解決的問題; “求一個一元二次方程的根求一個一元二次方程的根”Step 2: Step 2: 確定程序的輸入變量和輸出變量;確定程序的輸入變量和輸出變量; “輸入變量輸入變量:方程系數(shù):方程系數(shù)a a, , b b, , c c;輸出變量輸出變

28、量:兩根:兩根x x1 1, , x x2 2”Step 3: Step 3: 設(shè)計程序偽代碼;設(shè)計程序偽代碼;% % 輸入方程系數(shù)輸入方程系數(shù) input input a a, , b b, , c c% % 求根過程求根過程:% % 求根公式求根公式242bbacxa 偽代碼整體框架:偽代碼整體框架: 輸入方程系數(shù);輸入方程系數(shù); 求根過程;求根過程; 輸出求根結(jié)果。輸出求根結(jié)果。pagepage1616Step 3: Step 3: 設(shè)計程序偽代碼設(shè)計程序偽代碼- -續(xù)續(xù)% % ifif% % % else ifelse if% % elseelse ( (對對應(yīng)應(yīng) ) )% % end

29、 end% % 輸出結(jié)果輸出結(jié)果 print print x x1 1, , x x2 2240bac21224242bbacxabbacxa 240bac122bxxa 240bac2122422422bacbxiaabacbxiaa TipsTips: 偽代碼的設(shè)計偽代碼的設(shè)計由上由上而下而下,先分析總體框架,先分析總體框架,再局部細化;再局部細化; 偽代碼不是真實的偽代碼不是真實的MatlabMatlab代碼,其書寫類代碼,其書寫類似于數(shù)學(xué)表達式;似于數(shù)學(xué)表達式; 偽代碼只需在草稿偽代碼只需在草稿紙上書寫,力求簡潔、紙上書寫,力求簡潔、整齊、清晰。整齊、清晰。pagepage1717St

30、ep 4: Step 4: 將偽代碼轉(zhuǎn)換為將偽代碼轉(zhuǎn)換為MatlabMatlab程序語句程序語句% % Script fileScript file: calc_roots.m: calc_roots.m% % % PurposePurpose: :% This program solves for the roots of a quadratic equation% This program solves for the roots of a quadratic equation% of the form a% of the form a* *x2 + bx2 + b* *x + c = 0

31、. It calculates the answersx + c = 0. It calculates the answers% regardless of the type of roots that the equation possesses.% regardless of the type of roots that the equation possesses.% % % Record of revisionsRecord of revisions: :% Date Programmer Description of change% Date Programmer Descripti

32、on of change% = = =% = = =% 21/10/13 Y.Y.Guo Original code% 21/10/13 Y.Y.Guo Original code% % % Define variablesDefine variables: :% a -Coefficient of x2 term of equation% a -Coefficient of x2 term of equation% b -Coefficient of x term of equation% b -Coefficient of x term of equation% c -Constant t

33、erm of equation% c -Constant term of equation% discriminant -Discriminant of the equation% discriminant -Discriminant of the equation% imag_part -Imag part of equation (for complex roots)% imag_part -Imag part of equation (for complex roots)% real_part -Real part of equation (for complex roots)% rea

34、l_part -Real part of equation (for complex roots)% x1 -First solution of equation (for real roots)% x1 -First solution of equation (for real roots)% x2 -Second solution of equation (for real roots)% x2 -Second solution of equation (for real roots)“名字名字”“用途用途”“修改記錄修改記錄”“變量定義變量定義”“程序信息程序信息”TipsTips: 一

35、般而言,變一般而言,變量的定義可以在量的定義可以在后續(xù)程序編寫過后續(xù)程序編寫過程中進行,每出程中進行,每出現(xiàn)一個新的變量現(xiàn)一個新的變量時,則在程序段時,則在程序段前面進行定義;前面進行定義; 若為物理變量,若為物理變量,則最好將其單位則最好將其單位列出。列出。pagepage1818Step 4: Step 4: 將偽代碼轉(zhuǎn)換為將偽代碼轉(zhuǎn)換為MatlabMatlab程序語句程序語句- -續(xù)續(xù)% Prompt the user for the coefficients of the equation% Prompt the user for the coefficients of the equ

36、ationdisp (This program solves for the roots of a quadratic );disp (This program solves for the roots of a quadratic );disp (equation of the form Adisp (equation of the form A* *X2 + BX2 + B* *X + C = 0.);X + C = 0.);a = input(Enter the coefficient A: );a = input(Enter the coefficient A: );b = input

37、(Enter the coefficient B: );b = input(Enter the coefficient B: );c = input(Enter the coefficient C: );c = input(Enter the coefficient C: );% % Calculate discriminantCalculate discriminantdiscriminant = b2 - 4 discriminant = b2 - 4 * * a a * * c; c;% Solve for the roots, depending on the value of the

38、 % Solve for the roots, depending on the value of the discriminant.discriminant.ifif discriminant 0 discriminant 0 % there are two real roots, so .% there are two real roots, so .x1 = (-b + sqrt(discriminant) / (2x1 = (-b + sqrt(discriminant) / (2* *a);a);x2 = (-b - sqrt(discriminant) / (2x2 = (-b -

39、 sqrt(discriminant) / (2* *a);a);disp(This equation has two real roots:);disp(This equation has two real roots:);fprintf(x1 = %fn, x1);fprintf(x1 = %fn, x1);fprintf(x2 = %fn, x2);fprintf(x2 = %fn, x2);elseifelseif discriminant = 0 discriminant = 0 % there is one repeated root, so .% there is one rep

40、eated root, so .x1 = ( -b ) / (2x1 = ( -b ) / (2* *a);a);disp(This equation has two identical real roots:);disp(This equation has two identical real roots:);fprintf(x1 = x2 = %fn, x1);fprintf(x1 = x2 = %fn, x1);“輸入方程系數(shù)輸入方程系數(shù)”“計算方程判別式計算方程判別式”“方程有兩不等方程有兩不等 實根的情況實根的情況”“方程有兩相等方程有兩相等 實根的情況實根的情況”pagepage1

41、919Step 4: Step 4: 將偽代碼轉(zhuǎn)換為將偽代碼轉(zhuǎn)換為MatlabMatlab程序語句程序語句- -續(xù)續(xù)elseelse % there are complex roots, % there are complex roots, so .so .real_part = (-b) / (2real_part = (-b) / (2* *a);a);imag_part = sqrt( abs(discriminant) imag_part = sqrt( abs(discriminant) / (2/ (2* *a);a);disp(This equation has complex

42、disp(This equation has complex roots:);roots:);fprintf(x1 = %f + i %f fprintf(x1 = %f + i %f n,real_part, imag_part);n,real_part, imag_part);fprintf(x1 + %f - i %f n, fprintf(x1 + %f - i %f n, real_part, imag_part);real_part, imag_part);endend“方程有兩共軛方程有兩共軛復(fù)根的情況復(fù)根的情況”Step 5: Step 5: 調(diào)試程序調(diào)試程序 run(C:Pr

43、ogram run(C:Program FilesMATLABR2010abincalc_roots.m)FilesMATLABR2010abincalc_roots.m)This program solves for the roots of a quadratic This program solves for the roots of a quadratic equation of the form Aequation of the form A* *X2 + BX2 + B* *X + C = 0.X + C = 0.Enter the coefficient A: 1Enter th

44、e coefficient A: 1Enter the coefficient B: 4Enter the coefficient B: 4Enter the coefficient C: 3Enter the coefficient C: 3This equation has two real roots:This equation has two real roots:x1 = -1.000000 x2 = -3.000000 x1 = -1.000000 x2 = -3.000000212430(1,3)xxxx pagepage2020Step 5: Step 5: 調(diào)試程序調(diào)試程序-

45、 -續(xù)續(xù) run(C:Program run(C:Program FilesMATLABR2010abincalc_roots.m)FilesMATLABR2010abincalc_roots.m)This program solves for the roots of a quadratic This program solves for the roots of a quadratic equation of the form Aequation of the form A* *X2 + BX2 + B* *X + C = 0.X + C = 0.Enter the coefficient

46、 A: 1Enter the coefficient A: 1Enter the coefficient B: 2Enter the coefficient B: 2Enter the coefficient C: 1Enter the coefficient C: 1This equation has two identical real roots:This equation has two identical real roots:x1 = x2 = -1.000000 x1 = x2 = -1.000000212210(1)xxxx run(C:Program run(C:Progra

47、m FilesMATLABR2010abincalc_roots.m)FilesMATLABR2010abincalc_roots.m)This program solves for the roots of a quadratic This program solves for the roots of a quadratic equation of the form Aequation of the form A* *X2 + BX2 + B* *X + C = 0.X + C = 0.Enter the coefficient A: 1Enter the coefficient A: 1

48、Enter the coefficient B: 1Enter the coefficient B: 1Enter the coefficient C: 1Enter the coefficient C: 1This equation has complex roots:This equation has complex roots:x1 = -0.500000 + i 0.866025 x1 = -0.500000 + i 0.866025 x1 = -0.500000 - i 0.866025x1 = -0.500000 - i 0.866025212101313(,)2222xxxixi

49、 pagepage2121分支語句switchswitch ( (switch_exprswitch_expr) )casecase case_expr_1, case_expr_1,Statements (Statements (block 1block 1) )casecase case_expr_2, case_expr_2,Statements (Statements (block 2block 2) )otherwiseotherwise, ,Statements (Statements (block 3block 3) )endend分支語句是一種特殊的選擇語句,可以實現(xiàn)多種情況下

50、分支語句是一種特殊的選擇語句,可以實現(xiàn)多種情況下的開關(guān)控制。的開關(guān)控制。 switch_exprswitch_expr 可以是數(shù)可以是數(shù)字、字符串或者邏輯變量;字、字符串或者邏輯變量; 當當case_exprcase_expr表達式與表達式與switch_exprswitch_expr相符時,其后相符時,其后的命令語句塊的命令語句塊(block)(block)將被將被執(zhí)行,不符時,其后的命令執(zhí)行,不符時,其后的命令語句塊將被跳過;語句塊將被跳過; 當某一命令語句塊被執(zhí)當某一命令語句塊被執(zhí)行后,程序跳至行后,程序跳至endend語句后語句后一可執(zhí)行語句。一可執(zhí)行語句。pagepage2222實例

51、實例5 5:分支語句的簡單應(yīng)用:分支語句的簡單應(yīng)用num=input(num=input(請輸入一個數(shù):請輸入一個數(shù):););switch numswitch numcase -1case -1disp(I am a teacher.);disp(I am a teacher.);case 0case 0disp(I am a student.);disp(I am a student.);case 1case 1disp(You are a teacher.);disp(You are a teacher.);otherwiseotherwisedisp(You are a student.

52、);disp(You are a student.);endendpagepage2323循環(huán)語句實際問題中,需要反復(fù)執(zhí)行某些語句,這時就需要用到實際問題中,需要反復(fù)執(zhí)行某些語句,這時就需要用到循環(huán)語句。循環(huán)語句。在循環(huán)語句中,一組被重復(fù)執(zhí)行的語句稱為在循環(huán)語句中,一組被重復(fù)執(zhí)行的語句稱為循環(huán)體循環(huán)體,每,每循環(huán)一次,都必須做出判斷,是繼續(xù)循環(huán)執(zhí)行還是終止循環(huán)一次,都必須做出判斷,是繼續(xù)循環(huán)執(zhí)行還是終止執(zhí)行跳出循環(huán),這個判斷的依據(jù)稱為執(zhí)行跳出循環(huán),這個判斷的依據(jù)稱為循環(huán)的終止條件循環(huán)的終止條件。循環(huán)語句分類:循環(huán)語句分類:p forfor循環(huán):循環(huán)前循環(huán)次數(shù)已知;循環(huán):循環(huán)前循環(huán)次數(shù)已知;p

53、whilewhile循環(huán):循環(huán)前循環(huán)次數(shù)未知。循環(huán):循環(huán)前循環(huán)次數(shù)未知。pagepage2424循環(huán)語句之“forfor循環(huán)”forfor循環(huán)以指定的數(shù)目重復(fù)地執(zhí)行特定的語句塊。循環(huán)以指定的數(shù)目重復(fù)地執(zhí)行特定的語句塊。for for index=index=exprexpr1:1:exprexpr2:2:expexpr r3 3 statement 1statement 1 statement statement n nendend exprexpr1 1為循環(huán)變量為循環(huán)變量indexindex的初始值,的初始值,exprexpr2 2為為indexindex的步長,的步長,exprexpr3

54、 3為為indexindex的終值;的終值; 當步長當步長exprexpr2=12=1時,時,可以省略不寫。可以省略不寫。Example 1Example 1:for for i=1:2:9i=1:2:9 endendExample 2Example 2:for for i=1:9i=1:9 endendpagepage2525實例實例6 6:已知向量:已知向量t=-1 0 13 5t=-1 0 13 5,生成其,生成其VandermondeVandermonde矩陣矩陣 t=-1 0 1 3 5; t=-1 0 1 3 5; n=max(size(t); n=max(size(t); for

55、for j=1:n j=1:nforfor i=1:n i=1:na(i,j)=t(i)(n-j);a(i,j)=t(i)(n-j);endendendend a aa =a = 1 -1 1 -1 1 -1 1 -1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 81 27 9 3 81 27 9 3 1 1 625 125 25 5 625 125 25 5 1 1 a(:,n)=ones(n,1); a(:,n)=ones(n,1); for for j=n-1:-1:1j=n-1:-1:1a(:,j)=t.a(:,j)=t.* *a(:,j+1

56、);a(:,j+1);endend a aa =a = 1 -1 1 -1 1 1 -1 1 -1 1 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 81 27 9 3 1 81 27 9 3 1 625 125 25 5 1 625 125 25 5 1另一種方案另一種方案pagepage2626實例實例7 7:計算給定日期是一年中的第幾天:計算給定日期是一年中的第幾天Step 1: Step 1: 清晰地陳述出你要解決的問題;清晰地陳述出你要解決的問題; “計算給定的一天是這一年中的第幾天計算給定的一天是這一年中的第幾天”Step 2: Step 2: 確

57、定程序的輸入變量和輸出變量;確定程序的輸入變量和輸出變量; “輸入變量輸入變量:年:年year, year, 月月month, month, 日日dayday; 輸出變量輸出變量:一年中的第幾天:一年中的第幾天day_of_yearday_of_year”Step 3: Step 3: 設(shè)計程序偽代碼;設(shè)計程序偽代碼;偽代碼整體框架:偽代碼整體框架: 輸入年、月、日;輸入年、月、日; 計算第幾天計算第幾天: 先加上當月的天數(shù);先加上當月的天數(shù); 加上這一年往月的加上這一年往月的天數(shù),若包括二月,天數(shù),若包括二月,則需考慮是否為閏年;則需考慮是否為閏年; 輸出計算結(jié)果。輸出計算結(jié)果。% % 輸入

58、年、月、日輸入年、月、日input year, month, dayinput year, month, day% % 計算第幾天計算第幾天% % 首先判斷年份是否為閏年首先判斷年份是否為閏年ifif year/400 year/400余余0 0 是閏年是閏年 leap_day=1leap_day=1else if else if year/100year/100余余0 0 不是閏不是閏年年 leap_day=0leap_day=0pagepage2727Step 3: Step 3: 設(shè)計程序偽代碼設(shè)計程序偽代碼- -續(xù)續(xù)elseifelseif year/4 year/4余余0 0 是閏年

59、是閏年 leap_day=1leap_day=1elseelse 不是閏年不是閏年 leap_day=0 %leap_day=0 %四年一閏;百年不閏四年一閏;百年不閏, ,四百年再閏四百年再閏endend% % 開始計算第幾天開始計算第幾天% % 首先加上當月的天數(shù)首先加上當月的天數(shù)day_of_year=dayday_of_year=day% % 加上這一年往月的天數(shù)加上這一年往月的天數(shù)forfor i=1:month-1 i=1:month-1 switchswitch (i) (i) case 1, 3, 5, 7, 8,10 case 1, 3, 5, 7, 8,10 day_of_

60、year=day_of_year+31day_of_year=day_of_year+31 case 4, 6, 9, 11 case 4, 6, 9, 11 day_of_year=day_of_year+30day_of_year=day_of_year+30 case 2 case 2 day_of_year=day_of_year+28+leap_dayday_of_year=day_of_year+28+leap_day endendendend% % 輸出結(jié)果輸出結(jié)果 print day_of_yearprint day_of_yearpagepage2828Step 4: Ste

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論