已阅读5页,还剩35页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1,Matlab与科学计算,计算机学院 刘咏梅 Email:,2,第四章 MATLAB编程(预定义函数),Introduction to MATLAB MATLAB Basics of Numerical Computing MATLAB Programming(branching and Loops) MATLAB Programming(Predefined Functions) MATLAB Graphic and Image Processing Scientific Computing using MATLAB,3,本章学习的目标:,Introduction to Matlab Functions built in functions and user defined functions Optional Arguments Sharing Data Using Global Memory Preserving Data Between Calls to a Function Function Functions Subfunctions,4,Functions,Functions are more complex than scripts Functions have their own local variables Functions return output as specified, and can accept input as specified,5,Why we use functions?,Independent Testing of Sub-Tasks: Each task is an independent unit that can be tested separately (unit testing). Reusable Code: Within the same program (the code is not repeated). It can be used in another program. Isolation from unintended side effects: Isolation of function variables from the main program variables. The input and the output variables are specified clearly.,6,Syntax of Matlab Functions,User defined functions behave just like built in functions. All functions have a similar syntax, whether they are built-in functions or user-defined functions. Name Input Output,A=cos(x),7,How to call functions?,The functions can be called directly in the command window, or by any other program or function. Very important: if the input variables are changed in the functions they will not change in the main program (PASS-BY-VALUE SCHEME),8,Built in Functions,Built in functions allow us to reuse computer code for calculations that are performed frequently. For example, suppose there were no built in sine function in MATLAB and you will do a lot of trigonometric calculations involving the sine of an angle. Weve already become familiar with a number of MATLAB built in functions like sin (x) or plot (x,y) for example.,9,Some examples of Built in Functions,Exponential(指数) exp(x) exponential of x (ex) sqrt(x) square root of x Logarithmic(对数) log(x) natural logarithm( ln x ) log10(x) log2(x),10,Some examples of Built in Functions,Numeric(数值) fix(x) round to nearest integer towards 0 round(x) round to nearest integer sign(x) +1, 0 or -1 rem(x,y) Finds remainder of x/y Complex(复数) abs(x) absolute value angle(x) phase angle in complex plane conj(x) conjugate of x imag(x) imaginary part of x real(x) real part of x,11,Builtin Functions for vectors,max(x) returns largest value in vector x a,b = max(x) returns largest value in a and index where found in b max(x,y) x and y arrays of same size, returns vector of same length with larger value from corresponding positions in x and y max(3 5 2,6 4 8) ans= 6 5 8 same type of functions are available for min,12,Builtin Functions for vectors,sort(x) sort in ascending or descending order mean(x) average or mean value median(x) median value sum(x) sum of elements of x prod(x) product of elements of x cumsum(x) returns vector of same size with cumulative sum of x i.e. x=4,2,3 returns 4,6,9 cumprod(x) returns vector of same size with cumulative products in vector of same size i.e. x=4,2,3 returns 4,8,24,13,Builtin functions applied to matrices,Matrices (arrays) are stored in column major form When builtin functions for vectors are applied to a matrix function operates on columns and returns a row vector,14,Builtin functions applied to matrices,sum(0 1 2;3 4 5) ans= 3 5 7 prod(0 1 2;3 4 5) ans= 0 4 10 cumsum(0 1 2;3 4 5) ans= 0 1 2 3 5 7,cumprod(0 1 2;3 4 5) ans= 0 1 2 0 4 10 sort(9 1 2;3 2 4) ans= 3 1 2 9 2 4,15,User-defined Functions,You can use user defined functions in your programs just as you would use MATLABs built in functions. User defined functions help you write more compact programs and write complex programs into shorter ones that are easier to debug and get running.,16,User-defined Functions,User-defined functions must start with a function definition line. The definition line contains The word function A variable(variables) that defines the function output A function name A variable(variables) used for the input argument,function output = poly(x),The above line of codes defines a function named poly.,17,A simple example of user-defined functions,18,You can use your user defined functions from the Command Window or from a M-file program.,19,When x is a vector,Consider the following, A = 1, 2, 5; % A is a 3-element row vector y = 2 * poly(A) y = 14 82 982 In the statement y = 2* poly(A) above, the user defined function poly is called which evaluates the polynomial at each point in the input row vector. Then, each value is multiplied by 2, resulting in the row vector called y having the values 14, 82, 982.,20,Comments for Functions,The comment lines immediately after the first line are returned when you query the help function,21,22,User defined functions may,Have more than one input variable, function s = f (x, y, z). This function has three input variables. Have more than one output variable, function out1, out2 = f (x) is a function with one input variable and two output variables. Input and output variables can be scalars, vectors or matrices. Within the body of the function, there needs to be one assignment statement for each output variable.,23,Local Variables,Any variables used within a function are called local variables. These variables do not appear in the Workspace Window and are not available for the main program to access. The output variable names used in the function statement and in the body of the function are also local variables. The input variables are local variables also.,24,Local Variables,Variables defined in an M-file function, only have meaning inside that program The only way to communicate between functions and the workspace, is through the function input and output arguments,25,x, y, a, and output are local variables to the g function,When the g function is executed, the only variable created is determined in the command window (or script M-file used to execute a program),ans is the only variable created,26,Optional Arguments,For example: plot function x=0:pi/100:2*pi; y1=sin(2*x); y2=2*cos(2*x); plot(x,y1,x,y2);,x=0:pi/100:2*pi; y1=sin(2*x); y2=2*cos(2*x); plot(x,y1); hold on; plot(x,y2);,27,Optional Arguments,max(x) returns largest value in vector x a,b = max(x) returns largest value in a and index where found in b max(x,y) x and y arrays of same size, returns vector of same length with larger value from corresponding positions in x and y same type of functions are available for min,28,Optional Arguments(Input and Output),Optional output,Optional input,29,Determine the number of input and output variables,Two built in functions let you determine the number of input and output variables in any function, for function s = f (x) nargin (f) returns in our case, ans =1 since f has one input argument, and nargout(f) also returns ans = 1 in our case because f has only one output variable.,nargin:determines the number of input arguments nargout:determines the number of output arguments,30,Global Variables,Remember variable names used within a function are local variables, they do not appear in the Workspace window of the main program. It is possible to define a Global Variable that is then common to both the main program and the function. It is recommended not to do this. Although it is possible to define global variables, it is a bad idea!,31,Sharing Data Using Global Memory,Global memory is a special memory that can be accessed from any workspace. Syntax: global var1 var2 var3 Remark: Each global variable must be declared to be global before it is used for the first time in a function. Better to be the first statement in the function after the initial comments.,32,Preserving Data Between Calls to a Function,Each time the function is finished, data in the workspace is destroyed. To make some variables persistent for the next function calls, use the persist command. Special type of memory that can be accessed only from the same function. Syntax: persistent var1 var2 var3 ,33,Preserving Data Between Calls to a Function,Exercise: running averages and standard deviations. User must enter the number of values in data set, and then enter value by v
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 心血管疾病甲基化预防策略
- 心血管疾病个体化治疗的伦理考量
- 心脏移植供体分配的替代治疗资源整合
- 心脏瓣膜介入术后患者生活质量改善策略
- 心脏康复危险分层:分子标志物与功能管理
- 微生物组与肠道健康的干预策略
- 微创术中气体栓塞的麻醉管理策略
- 微创技术在器官移植中的特殊人文考量
- 微创手术中的医学人文会诊机制建设
- 微创入路下颅底肿瘤手术的适应症筛选
- 燃油导热油锅炉施工方案
- 检验检测机构质量培训
- 2026四川农商银行校园招聘1065人考试笔试备考试题及答案解析
- 2025年Q2无人机航拍服务定价及市场竞争力提升工作总结
- 液化气站员工安全培训大纲
- 考调工作人员(综合知识)历年参考题库含答案详解(5套)
- 2025-2026学年度第一学期第二次检测九年级道德与法治考试试题
- 漂流滑道施工方案
- 安全管理不足之处及整改方案解析
- 安全生产培训包括哪些内容
- 赊销业务与企业财务风险控制-洞察及研究
评论
0/150
提交评论