版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 “logical”
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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- T/SZS 1610-2023供深食品 食用植物油质量安全基础要求
- 轻度认知障碍老年人运动干预团体标准考试试题及答案
- 工业污染治理方案
- 前厅培训文案工作方案
- 不负教育重托演讲稿
- 2026年秋招:陕西法士特汽车传动集团试题及答案
- 2026年贵州司法局预算核算招聘考试练习试卷(含答案)
- 2026年江苏幼儿园教师会计招聘考试练习试卷(含答案)
- 考研政治(马克思主义基本原理概论)模拟试卷148
- 酒泉航天科技商业化发展的市场分析
- 造船厂安全制度
- DB32T4061-2021空气源热泵热水系统建筑应用技术规程
- 外汇收支申报系统用户手册
- 2023年12月高等学校英语应用能力考试B级真题及解析
- 中国农业银行笔试题库(含答案)
- 珠海农村商业银行2017年年报告简析
- YY/T 0148-2006医用胶带 通用要求
- WS/T 453-2014丙型病毒性肝炎筛查及管理
- GB/T 39673.52-2020住宅和楼宇电子系统(HBES)及楼宇自动化和控制系统(BACS)第52部分:用于住宅、商业和轻工业环境下的HBES和BACS的EMC要求
- GB/T 3323.1-2019焊缝无损检测射线检测第1部分:X和伽玛射线的胶片技术
- 马工程教材《公共财政概论》PPT-绪论
评论
0/150
提交评论