




已閱讀5頁,還剩47頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(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檔是一個簡易繪圖程式做為示范使用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多項式函數(shù),多項式常被用來模擬一個物理現(xiàn)象的解析函數(shù),之所以采用多項式,是因為它很容易計算。在這里我們將說明如何做多項式的計算及解多項式的根。,令p(x) 代表一個多項式如下 MATLAB 以一最簡便方式代表上述的多項式 p=1 4 -7 -10,其中的數(shù)值是多項式的各階項(從高到低)的 各個系數(shù),其實p 也是一個陣列不過是用以代表這個多項式。 有了多項式的表示式后,我們即可來計算其函數(shù)值。假設(shè)要計算一組數(shù)據(jù)x對應(yīng)的多項式值,依照一般的函數(shù) 計算須以下列式子計算:, p=x.3+4*x.2-7*x-10 為了能直接運用多項式,可以用函數(shù) polyval直接做運算,語法為 polyval(p,x),其中p 即是代表多項式各階系數(shù) 的陣列。因此 x=linspace(-1,3,N); p=1 4 7 -10; v=polyval(p,x);,我們接著說明如何對二個多項式做加減乘除運算。當(dāng)二個多項式間要做加減乘除時,加 減運算可以直接進行。假設(shè)有二個多項式 a(x) 和 b(x) 定義如下:,如果多項式 c(x) 為上述二多項式相加,即 c(x) = a(x) + b(x), 因此,如果是二多項式相減得到的多項式為 d(x) = a(x) - b(x), 則,以下就介紹相關(guā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,而將兩個多項式相乘可以得到一新的多項式 e(x) = a(x) b(x),如果是兩個多項式相除,即 :,上述二個運算式不能直接運算,須要另外定義函數(shù)conv做乘法運算以及函數(shù)deconv做除法運算。當(dāng)二多項式相乘,在數(shù)學(xué)上等于二個陣列做卷積(convolution)運算(因為我們是以陣列來代表一個多項式的各階系數(shù)), 因此可利用conv函數(shù)做乘法運算,其語法為conv(a,b),其中a, b代表二個多項式的陣列。而二多項式相除就相 當(dāng)于反卷積(de-convolution) 運算,因此有 deconv 函數(shù),其語法稍有不同 q,r=deconv(a,b),其中q,r分別代表整 除多項式及余數(shù)多項式。如果直接使用q=deconv(a,b)則只求出整除多項式。,以下就介紹相關(guā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 % 因為是整除所以余數(shù)多項式的各系數(shù)皆為零, h,r=deconv(g,a) h = 1 4 9 18 r = 0 0 0 0 2 6 12 % 余數(shù)多項式為 2*x2 + 6*x + 12,多項式的根,一個多項式視其階數(shù)而定,它的根可以有一個到數(shù)個,可能為實數(shù)也可能是復(fù)數(shù)。要求一高階多項式的根往 往須借助數(shù)值方法,所幸MATLAB已將這些數(shù)值方法寫成一函數(shù)roots(p),我們只要輸入多項式的各階系數(shù)( 以 p 代表)即可求解到對應(yīng)的根, p=1 3 2; r=roots(p) r = -2 -1 p=1 -12 0 25 116; % 注意二階項系數(shù)為零須要輸入,否則多項式的階數(shù)就不對 r=roots(p) % 有實數(shù)根及復(fù)數(shù)根 r = 11.7473 2.7028 -1.2251 + 1.4672i -1.2251 - 1.4672i,與 roots 相關(guān)的函數(shù)尚有 poly,real,這二個函數(shù)的用途是要驗算求解的根展開能求得原多項式。 例如有一個二次方程式的根為-2, -1,則以下式計算原多項式 p(x)=(x+2)(x+1)=x2+3x+2 poly 函數(shù)就是在求出多項式的各階系數(shù),其語法為 poly(r),其中 r 是代表根的陣列。而 real 則是用來去除因計算時產(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) % 這個多項式的系數(shù)與原多項式 p 相同 pp = 1 -4 6 -4, pp=1 7 12 9; % 再看另一個多項式 r=roots(pp) r = -4.9395 -1.0303 + 0.8721i -1.0303 - 0.8721i pp=poly(r) % 注意因計算的誤差會有假虛部產(chǎn)生 pp = 1.0000 7.0000 12.0000 9.0000 + 0.0000i pp=real(pp) % 可以real將假虛部去除,將原多項式還原 pp = 1.0000 7.0000 12.0000 9.0000,非線性方程的實根,如果求根的方程不為多項式的形式, 就不能用 roots 函數(shù)。而這類的方程多半是非線性方程, 其函數(shù)形式變化很大。對于解這類方程的根,可以用 fzero函數(shù),它其實是用來找一函數(shù) f(x) 的 x 值代入時,會使該函數(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ù)分布圖看出根不只一 個,則須再代入另一個在根附近的 x0,再求出下一個根。 以下分別介紹幾數(shù)個方程式,來說明如何求解它們的根。,例一、方程為 sin(x)=0 我們知道上式的根有 ,求根方式如下: r=fzero(sin,3) % 因為sin(x)是內(nèi)建函數(shù),其名稱為sin, %因此無須定義它選擇 x=3 附近求根 r = 3.1416 r=fzero(sin,6) % 選擇 x=6 附近求根 r = 6.2832,例二、方程為MATLAB 內(nèi)建函數(shù) humps,我們不須要知道這個方程的形態(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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年絲素防曬蜜項目投資價值分析報告
- 2025年集體勞動合同規(guī)定-中外合資企業(yè)
- 2025技術(shù)與資本合作項目合同模板
- 2025海外工程承包合同范本
- 貴州地理奧賽試題及答案
- 組織內(nèi)面試試題及答案
- 駕校運管測試題及答案
- 面試加試試題及答案
- 銀行決策面試題及答案
- 金融測試面試題及答案
- 高血壓與青光眼的關(guān)系
- 專題03 根據(jù)音標(biāo)寫單詞常考易錯100題-譯林版七年級上學(xué)期英語期末考點復(fù)習(xí)專項訓(xùn)練
- 數(shù)字經(jīng)濟對廣東省經(jīng)濟影響研究
- 編制氣候可行性論證報告
- 2024年上海銀聯(lián)數(shù)據(jù)服務(wù)有限公司招聘筆試參考題庫含答案解析
- 工業(yè)園區(qū)規(guī)劃環(huán)評報告書
- 養(yǎng)老院項目組織結(jié)構(gòu)方案
- 士兵軍考模擬卷(化學(xué))
- 2023-2024年版中國運動康復(fù)產(chǎn)業(yè)白皮書-運動康復(fù)產(chǎn)業(yè)聯(lián)盟-202310
- 小升初成語運用題有答案
- 無倉儲經(jīng)營企業(yè)管理制度、責(zé)任制、操作規(guī)程匯編
評論
0/150
提交評論