matlab譯文第4章.ppt_第1頁
matlab譯文第4章.ppt_第2頁
matlab譯文第4章.ppt_第3頁
matlab譯文第4章.ppt_第4頁
matlab譯文第4章.ppt_第5頁
已閱讀5頁,還剩38頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Chapter 4,loops,Function: execute a sequence of statements more than once Type: while loops , for loops 4.1 the while loops General form: while expression commands end The code in a while loop is repeated an indefinite number of times until some user-specified condition is satisfied. Example 4.1-sta

2、tistical analysis ex4.4,Spot the flaw! 4.2 the for loop Executes a block of statements a specified number of times. for index=expr commands end Index is the loop variable. The loop is executed once for each column in expr.,The for loop construct functions as follows. 1.At the beginning of the loop,

3、MATLAB generates the control expression. 2.The first time through the loop, the program assigns the first column of the expression to the loop variable index, and the program executes the statements within the body of the loop. 3.After the statements in the body of the loop have been executed, the p

4、rogram assigns the next column of the expression to the loop variable index, and the program executes the statements within the body of the loop again. 4.Step 3 is repeated over and over as long as there are additional columns in the control expression.,for ii=1:10 commands end,for ii=1:2:10 command

5、s end,for ii=5 9 7 commands end,for ii=1 2 3; 4 5 6 commands end,Quiz4.1 P162 1,2,3,4,5,6,7,Example4.3-calculating the day of year P145 Solution: for loop switch construct Good programming practice: 1.always indent the body of a for loop by 2 or more spaces to improve the readability of the code. 2.

6、never modify the value of a loop index with the body of the loop.,3.always preallocate all arrays used in a loop before executing the loop, this practice greatly increases the execution speed of the loop. The steps of extending an array: 1.Create a new array 2.Copy the contents of the old array to t

7、he new longer array 3.Add the new value to the array 4.Delete the old array,4. If it is possible to implement a calculation either with a for loop or using vectors, always implement the calculation with vectors, your program will be much faster.,Example 4.5-Comparing Loops and Vectorization Three wa

8、ys P152 Using the MATLAB functions tic and toc,The break and continue statements Break-terminates the execution of a loop and passes control to the next statement after the end of the loop. Continue-terminates the current pass through the loop and returns control to the top of the loop.,Quiz 4.1 P16

9、2 8,9,Nesting loops P156 Note: inner for loop executes completely before the index variable of the outer for loop is incremented. (seldom use, too slow) 4.3 Logical arrays and vectorization MATLAB contains a third “sort of” data type: logical, it is a standard numeric array with a special “l(fā)ogical”

10、property added. It is created by all relational and logic operators.,The significance of logical arrays- They can serve as a mask for arithmetic operations This is a very fast and very clever way of performing an operation on a subset of an array without needing loops and branches. P163 quiz4.1 10,i

11、t is also possible to apply a different set of operations to the unselected elements of the array by simply adding the not operator to the logical mask.,Example4.7-fitting a line to a set of nosiy measurements P163,Least squares,Summary while loop for loop break continue logical tic toc Exercises P1

12、79 4.1, 4.4, 4.5, 4.7, 4.8, 4.9, 4.19,M function files,Whats the difference between M script files and M function files?,The benefits include: 1.Independent testing of subtasks 2.Reusable code 3.Isolation from unintended side effects,1. M function files have a function declaration line beginning wit

13、h the key word function. 2. It is allowed to use fewer input or output arguments than standard form to call the function. 3. The variables is in the base workspace when M script files are executed, while the variables are pushed into temporary creating function workspace when M function files are ca

14、lled. After the call is finished, the function workspace will be deleted automatically.,4. The base workspace is created with the start-up of MATLAB and deleted with close-up of MATLAB. The function workspace is independent from the base workspace . 5. If some M script file is called in M function f

15、ile, the variables produced by M script file are deposited in the function workspace, not the base workspace.,Formation function output argument=functionname(input argument) The first comment line after the function statement is called the H1 comment line. It should always contain a one-line summary

16、 of the purpose of the function Sharing Data Using Global Memory Global statement global var1 var2 var3 ,Declare global variables in all capital letters to make them easy to distinguish from local variables. The variables in M function file are local variables , they only influence the function itse

17、lf and could not be used by outer MATLAB program. While the variables in the M script file are global variables, which can be used again when the program is exited.,The definition name and saving name of a function keep the same. When they are different, saving name is considered first. The actual a

18、rguments should correspond to dummy arguments in a fix order. Exm060201.m,Example2: Write a MATLAB program to evaluate a function p(x1 , x2 ) for any two user-specified values x and y. The function p(x1 , x2 ) is defined as follows,Refer to scriptandfunction.m,Subfunctions and private functions If m

19、ore than one function is present in a file, The top function is a normal function, while the ones below it are subfunctions. Subfunctions look just like ordinary functions, but they are only accessible to the other functions within the same file.,Private functions are functions that reside in subdir

20、ectories with the special name private. They are only visible to functions in the parent directory. Anonymous functions FH=(arglist) expr FH(arglist) or feval(FH,arglist),Function handle The establishment: 1)Use the symbol 2)Use transfer function str2func The advantages: 1)Easy to call functions, li

21、ke use of variables; 2)Call subfunctions and private functions in larger range, make them more reusable,3)Speed up the call of functions Formation argout1,argout2,argoutn=feval(Hfun,argin1,argin2,arginn) Exm060301.m,Polynomial operation,1)The expression of polynomial A=a1 a2 a3 an an+1,2)A function

22、used to solve the roots of a polynomial x=roots(A),Example1: write a program to solve for the roots of a polynomial, A=1 8 0 0 -10; x=roots(A),x = -8.0194 -0.5075 + 0.9736i -0.5075 - 0.9736i 1.0344,3)The reconstruction of polynomial,A=poly(x),roots and poly are a pair of inverse functions, x = -8.01

23、94 -0.5075 + 0.9736i -0.5075 - 0.9736i 1.0344 ; z=poly(x),z = 1.0000 8.0000 0.0000 0.0000 -9.9996,4) The arithmetic of polynomial Addition subtract like the same operations of vectors, notes that padding zeros on the high bits A=1 2 3 4 5 6 C=A+B=1 2 5 8 6 10 B=0 0 2 4 1 4 Multiplication,C=conv(A,B)

24、,Example2: compute the product of polynomial and polynomial,A=1 8 0 0 -10; B=2 -1 3; C=conv(A,B) C = 2 15 -5 24 -20 10 -30,Division Q,r=deconv(A,B) Where, Q is quotient, and r is residue,deconv is the inverse function of conv,that is A=conv(B,Q)+r,A=1 8 0 0 -10; B=2 -1 3; P,r=deconv(A,B) P = 0.5000

25、3.7500 2.6250 r = 0 0 0 -8.6250 -2.1250 quotient P is 0.5x2+3.75x+2.625, residue r is -8.625x-2.125。,5) Get the value of polynomial,Y=polyval(A,x),A=1 8 0 0 -10; x=1.2; y1=polyval(A,x) y1 = 5.8976,x=-1 1.2 -1.4 ; 2 -1.8 1.6; y2= polyval(A,x) y2= -3.0000 3.2480 2.9360 30.0000 10.0880 14.5760,6)The differential of polynomial,dpolyder(A),Example3: get the differential of polynomial,A=1 0 3 5 12 0 36 1 55; b=polyder(A) b 8 0 18 25 48 0 72 1,Notes that the dimension of b !,7)Rational fraction and partial fraction,b=b1, b2, b3, ., bm, bm+1 a=a1, a2, a3, ., an, an+1,r, p, k= residue(b, a),Example

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論