




已閱讀5頁,還剩47頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
What is an m-file?,An m-file is a file with extension .m It can be used to store commands Matlab reads and executes the commands in an m-file if you type the name of the file in the command window,Use an ordinary text editor and save the file as filename.m Choose new file under the File Menu,Different ways of creating m-files,In the first case the function inv(A) is used to find the inverse. This is then multiplied by b. The Matlab code for this operation is,In the second case left- division is performed straight away with the command,X= 9.2500 4.2500 2.7500,X= 9.2500 4.2500 2.7500,Example,Example solvetime.m A = rand(1000,1000); % Creates a random % matrix A b = rand(1000,1); % Creates a random % vector b det(A) % Calculates the determinant of A tic, % Starts the time-watch x = inv(A)*b; % Solves the system toc % Stops the watch tic, y = Ab; toc % Solves and times the %system with left division,lapsed_time = 1.2820 elapsed_time = 0.5310,以下的tutex1.m檔是一個(gè)簡易繪圖程式做為示范使用M-file,% M-file, tutex1.m % Simple plot for illustration of using M-file. % 簡易繪圖以做為示范使用M-file x=linspace(0,2*pi,20); y=sin(x); plot(x,y,r+) xlabel(x-value) ylabel(y-value) title(2D plot),When you write something in Matlabs command window, the following things are checked in turn: 1. Is there such a variable in the workspace? 2. Is there an m-file called that thing in the current directory? 3. Is there an m-file called that somewhere else? When Matlab searches for the m-file it looks in specific folders, its search path. If the folder is not added to the path Matlab can not run your script.,How to make sure Matlab finds your m-file,Change current directory using the cd command or by clicking in the Directory panel Select the Set Path option in the File menu Type one of the following commands in the command window: addpath C:ProgramMatlab6k5myfiles path(path,C:ProgramMatlab6k5myfiles), cd wufilemy_work % 切換至目錄wufilemy_work cd % 如果只用 cd 則會顯示目前的目錄 c:WUFILEMY_WORK dir % 列出目錄下的檔案 . tutex1.m tutex2.m test.txt delete test.txt % 刪除 test.txt, path(path,c:wufilemy_work) % 將自己的目錄 wufilemy_work 加在 % MATLAB的搜尋路徑之后 path(c:wufilemy_work,path) % 將自己的目錄 wufilemy_work 加在 % MATLAB的搜尋路徑之前,What is a function A function is an m-file beginning with the word function A function has a user-specified input and output A function has its own local workspace. Variables defined in the function are not stored in the ordinary workspace. Nor are your workspace variables altered if given a new value in the local workspace.,myfile.m function c = myfile(a,b) c =sqrt(a.2)+ (b.2);,myfun.m function y = myfun(t) y = t * sin(t);,Example,a=4 b=3 c = myfile(a,b) c= 5.0000,A function does not assign any values to anything. Exception to this rule is if you define a global variable. Type help global for more information You can have several functions stored in one file. The subfunctions work exactly as a normal function, but can not be accessed directly from the command window (unlike other languages).,Example spir.m function x,y = spir(t) x = cos(20*t).*exp(-t.2); y = sin(20*t).*exp(-t.2); spirplot.m function x,y = spirplot(t) x = cos(20*t).*exp(-t.2); y = sin(20*t).*exp(-t.2); plot(x,y),spir3.m function x,y,z = spir3(t) x = cos(20*t).*exp(-t.2); y = sin(20*t).*exp(-t.2); z = exp(-t.2); plot3(x,y,z),Loops A for-loop assigns a number of different values to a parameter, then stops performing the task. Begins with the word for, ends with the word end. for n = 1:N a(n) = 1/n; end,You can write the loops directly in the command window: for n = 1:N, t(n) = 1/n; end for r = linspace(0,1,20), q = sin(r); end for a = A, plot(a), end Commas separate several commands on a line. If is a matrix the loop variable becomes a vector of the separate columns in A,clear % Clears the workspace A = peaks; % A is the test-matrix peaks for a = A plot(a) % a obtains the value of % each column in A drawnow % Updates the figure window % immediately pause end,Example,ApBloop.m A = ones(1000,1000); % Creates matrices A and B B = zeros(1000,1000); C = zeros(1000,1000); tic, % Starts the stop-watch for n = 1:1000 % Start of the first for-loop for m = 1:1000 % Start of the second for -loop C(n,m) = A(n,m)+B(n,m); end % Ends the second loop end % Ends the second loop toc tic, C = A+B; toc,elapsed_time = 0.1250 elapsed_time = 0.0150,elapsed_time = 0 elapsed_time = 0.0310,resizeloop.m A = ones(500,500); % Creates matrices A, B and C B = zeros(500,500); tic, C = A+B; toc tic, % Starts the stop-watch for n = 1:500 % Start of the first for-loop for m = 1:500 % Start of the second for -loop C(n,m) = A(n,m)+B(n,m); end % Ends the first loop end % Ends the second loop toc,A while-loop performs a task until a logical test proves false. Begins with the word while, ends with the word end while t tmax, a = sin(t); t = t + dt; end,while-loops,Save time Use Matlab! Document your scripts and functions: Add comments and help. If the first lines in an m-file are commented, these will be displayed in the command window when you type help filename Indents makes it easier to read the program Include error possibilities in your functions and scripts: error(error message),Use matrices rather than loops whenever possible. Include matrix arguments in your functions. vectorize.m % Compares the computational speed % of vectorized code and a for-loop clear tic, A = sin(1:1e6).*1:1e6; % Vectorized code toc tic, for n = 1:1e6 A(n) = sin(n)*n; % For-loop end toc,Matlab多項(xiàng)式函數(shù),多項(xiàng)式常被用來模擬一個(gè)物理現(xiàn)象的解析函數(shù),之所以采用多項(xiàng)式,是因?yàn)樗苋菀子?jì)算。在這里我們將說明如何做多項(xiàng)式的計(jì)算及解多項(xiàng)式的根。,令p(x) 代表一個(gè)多項(xiàng)式如下 MATLAB 以一最簡便方式代表上述的多項(xiàng)式 p=1 4 -7 -10,其中的數(shù)值是多項(xiàng)式的各階項(xiàng)(從高到低)的 各個(gè)系數(shù),其實(shí)p 也是一個(gè)陣列不過是用以代表這個(gè)多項(xiàng)式。 有了多項(xiàng)式的表示式后,我們即可來計(jì)算其函數(shù)值。假設(shè)要計(jì)算一組數(shù)據(jù)x對應(yīng)的多項(xiàng)式值,依照一般的函數(shù) 計(jì)算須以下列式子計(jì)算:, p=x.3+4*x.2-7*x-10 為了能直接運(yùn)用多項(xiàng)式,可以用函數(shù) polyval直接做運(yùn)算,語法為 polyval(p,x),其中p 即是代表多項(xiàng)式各階系數(shù) 的陣列。因此 x=linspace(-1,3,N); p=1 4 7 -10; v=polyval(p,x);,我們接著說明如何對二個(gè)多項(xiàng)式做加減乘除運(yùn)算。當(dāng)二個(gè)多項(xiàng)式間要做加減乘除時(shí),加 減運(yùn)算可以直接進(jìn)行。假設(shè)有二個(gè)多項(xiàng)式 a(x) 和 b(x) 定義如下:,如果多項(xiàng)式 c(x) 為上述二多項(xiàng)式相加,即 c(x) = a(x) + b(x), 因此,如果是二多項(xiàng)式相減得到的多項(xiàng)式為 d(x) = a(x) - b(x), 則,以下就介紹相關(guān)范例,來說明二個(gè)多項(xiàng)式的加減運(yùn)算:, a=1 2 3 4; b=1 4 9 16; c=a+b c = 2 6 12 20 d=a-b d = 0 -2 -6 -12,而將兩個(gè)多項(xiàng)式相乘可以得到一新的多項(xiàng)式 e(x) = a(x) b(x),如果是兩個(gè)多項(xiàng)式相除,即 :,上述二個(gè)運(yùn)算式不能直接運(yùn)算,須要另外定義函數(shù)conv做乘法運(yùn)算以及函數(shù)deconv做除法運(yùn)算。當(dāng)二多項(xiàng)式相乘,在數(shù)學(xué)上等于二個(gè)陣列做卷積(convolution)運(yùn)算(因?yàn)槲覀兪且躁嚵衼泶硪粋€(gè)多項(xiàng)式的各階系數(shù)), 因此可利用conv函數(shù)做乘法運(yùn)算,其語法為conv(a,b),其中a, b代表二個(gè)多項(xiàng)式的陣列。而二多項(xiàng)式相除就相 當(dāng)于反卷積(de-convolution) 運(yùn)算,因此有 deconv 函數(shù),其語法稍有不同 q,r=deconv(a,b),其中q,r分別代表整 除多項(xiàng)式及余數(shù)多項(xiàng)式。如果直接使用q=deconv(a,b)則只求出整除多項(xiàng)式。,以下就介紹相關(guān)范例,來說明二個(gè)多項(xiàng)式的乘除運(yùn)算: a=1 2 3 4; b=1 4 9 16;, e=conv(a,b) e = 1 6 20 50 75 84 64 g=e+0 0 0 c g = 1 6 20 52 81 96 84,(c = 2 6 12 20), f,r=deconv(e,b) f = 1 2 3 4 r = 0 0 0 0 0 0 0 % 因?yàn)槭钦杂鄶?shù)多項(xiàng)式的各系數(shù)皆為零, h,r=deconv(g,a) h = 1 4 9 18 r = 0 0 0 0 2 6 12 % 余數(shù)多項(xiàng)式為 2*x2 + 6*x + 12,多項(xiàng)式的根,一個(gè)多項(xiàng)式視其階數(shù)而定,它的根可以有一個(gè)到數(shù)個(gè),可能為實(shí)數(shù)也可能是復(fù)數(shù)。要求一高階多項(xiàng)式的根往 往須借助數(shù)值方法,所幸MATLAB已將這些數(shù)值方法寫成一函數(shù)roots(p),我們只要輸入多項(xiàng)式的各階系數(shù)( 以 p 代表)即可求解到對應(yīng)的根, p=1 3 2; r=roots(p) r = -2 -1 p=1 -12 0 25 116; % 注意二階項(xiàng)系數(shù)為零須要輸入,否則多項(xiàng)式的階數(shù)就不對 r=roots(p) % 有實(shí)數(shù)根及復(fù)數(shù)根 r = 11.7473 2.7028 -1.2251 + 1.4672i -1.2251 - 1.4672i,與 roots 相關(guān)的函數(shù)尚有 poly,real,這二個(gè)函數(shù)的用途是要驗(yàn)算求解的根展開能求得原多項(xiàng)式。 例如有一個(gè)二次方程式的根為-2, -1,則以下式計(jì)算原多項(xiàng)式 p(x)=(x+2)(x+1)=x2+3x+2 poly 函數(shù)就是在求出多項(xiàng)式的各階系數(shù),其語法為 poly(r),其中 r 是代表根的陣列。而 real 則是用來去除因計(jì)算時(shí)產(chǎn)生的假虛部系數(shù),為何會有此種情形請參考以下的例子。, r=-2 -1; pp=poly(r) % pp=(x+2)(x+1)=x2+3x+2 pp = 1 3 2 p=1 -4 6 -4; r=roots(p) r = 2.0000 1.0000 + 1.0000i 1.0000 - 1.0000i pp=poly(r) % 這個(gè)多項(xiàng)式的系數(shù)與原多項(xiàng)式 p 相同 pp = 1 -4 6 -4, pp=1 7 12 9; % 再看另一個(gè)多項(xiàng)式 r=roots(pp) r = -4.9395 -1.0303 + 0.8721i -1.0303 - 0.8721i pp=poly(r) % 注意因計(jì)算的誤差會有假虛部產(chǎn)生 pp = 1.0000 7.0000 12.0000 9.0000 + 0.0000i pp=real(pp) % 可以real將假虛部去除,將原多項(xiàng)式還原 pp = 1.0000 7.0000 12.0000 9.0000,非線性方程的實(shí)根,如果求根的方程不為多項(xiàng)式的形式, 就不能用 roots 函數(shù)。而這類的方程多半是非線性方程, 其函數(shù)形式變化很大。對于解這類方程的根,可以用 fzero函數(shù),它其實(shí)是用來找一函數(shù) f(x) 的 x 值代入時(shí),會使該函數(shù)值為零 (f(x)=0);而這也就是根的特性,因此我們可以用 fzero求根。,要求任一方程的根有三步驟: (1)先定義方程。要注意必須將方程安排成 f(x)=0 的形式,例如一方程為sin(x)=3, 則該方程式應(yīng)表示為 f(x)=sin(x)-3??梢?用m-file 定義方程。 (2)代入適當(dāng)范圍的 x, y(x) 值,將該函數(shù)的分布圖畫出,藉以了解該方程的長相。,(3)由圖中決定y(x)在何處附近(x0)與 x 軸相交,以fzero的語法fzero(function,x0) 即可求出在 x0附近的根,其中 function 是先前已定義的函數(shù)名稱。如果從函數(shù)分布圖看出根不只一 個(gè),則須再代入另一個(gè)在根附近的 x0,再求出下一個(gè)根。 以下分別介紹幾數(shù)個(gè)方程式,來說明如何求解它們的根。,例一、方程為 sin(x)=0 我們知道上式的根有 ,求根方式如下: r=fzero(sin,3) % 因?yàn)閟in(x)是內(nèi)建函數(shù),其名稱為sin, %因此無須定義它選擇 x=3 附近求根 r = 3.1416 r=fzero(sin,6) % 選擇 x=6 附近求根 r = 6.2832,例二、方程為MATLAB 內(nèi)建函數(shù) humps,我們不須要知道這個(gè)方程的形態(tài)為何,不過我們可以將它畫出來,再找出根的位置。求根方式如下: x=linspace(-2,3); y=
溫馨提示
- 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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 刀剪產(chǎn)品的品牌推廣策略與執(zhí)行計(jì)劃考核試卷
- 豬的飼養(yǎng)智能化管理考核試卷
- 竹材加工過程中的質(zhì)量控制考核試卷
- 皮革制品的消費(fèi)心理與購買決策考核試卷
- 盾構(gòu)機(jī)施工中的隧道工程地質(zhì)環(huán)境保護(hù)策略考核試卷
- 廈門醫(yī)學(xué)院《心理測量學(xué)》2023-2024學(xué)年第一學(xué)期期末試卷
- 無錫科技職業(yè)學(xué)院《人體工程學(xué)》2023-2024學(xué)年第二學(xué)期期末試卷
- 西昌民族幼兒師范高等專科學(xué)?!赌X癱兒童功能評估》2023-2024學(xué)年第一學(xué)期期末試卷
- 沈陽化工大學(xué)《語文》2023-2024學(xué)年第一學(xué)期期末試卷
- 沈陽職業(yè)技術(shù)學(xué)院《外國文學(xué)作品欣賞》2023-2024學(xué)年第二學(xué)期期末試卷
- 3.2工業(yè)區(qū)位因素及其變化以大疆無人機(jī)為例課件高一地理人教版
- 2024年陜西省中考數(shù)學(xué)試卷(A卷)附答案
- DL-T5190.1-2022電力建設(shè)施工技術(shù)規(guī)范第1部分:土建結(jié)構(gòu)工程
- 財(cái)務(wù)預(yù)算分析表模板
- 中國高清熒光腹腔鏡行業(yè)市場現(xiàn)狀分析及競爭格局與投資發(fā)展研究報(bào)告2024-2034版
- 2024年高考體育單招考試政治重點(diǎn)知識點(diǎn)歸納總結(jié)(復(fù)習(xí)必背)
- MOOC 大數(shù)據(jù)技術(shù)原理與應(yīng)用-廈門大學(xué) 中國大學(xué)慕課答案
- 國企管理人員招聘考試題庫
- 高血壓與青光眼的關(guān)系
- (2024版)小學(xué)二年級孩子如何高效復(fù)習(xí)語文知識點(diǎn)
- asme焊接工藝評定
評論
0/150
提交評論