User-defined Functions (1/28) Scripts In chapter 3 we have already learned the top-down design Programmers break the algorithm down into logical subdivisions called subtasks and finally the whole subtasks are turned into Matlab code. Problem:there is no way to code,verify and test each subtask independently before combining the subtask into the final program... Solution:Matlab has a special mechanism designed to make subtasks easy to develop and debug independently by using a separate function before building the fi同僑k学 AW program TONGJI UNIVERSITY
User-defined Functions (1/28) Scripts In chapter 3 we have already learned the top-down design. Programmers break the algorithm down into logical subdivisions called subtasks and finally the whole subtasks are turned into Matlab code. Problem: there is no way to code, verify and test each subtask independently before combining the subtask into the final program… Solution: Matlab has a special mechanism designed to make subtasks easy to develop and debug independently by using a separate function before building the final program
User-defined Functions (2/28) Scripts All of the M-files that we have seen so far have been script files Editor-C\MATLAB7\work\distance.m File Edit Text Cell Tools Debug Desktop Window He D三■品意口~昌的天.自超习 1-xl=input ('the first point x'); 2- yl=input('the first point y'): 3- x2=input('the second point x'); 4- y2=input ('the second point y); 5- d=sqrt(x1-x2)2+(y1-y2)2): 6 disp(['the distance is'num2str(d)]); A script is just a collection of MATLAB statements Running a script is the same as running the statements in the command window A special type of M-file ---Function 同濟大学 AW TONGJI UNIVERSITY
All of the M –files that we have seen so far have been script files A script is just a collection of MATLAB statements Running a script is the same as running the statements in the command window A special type of M-file --- Function User-defined Functions (2/28) Scripts
User-defined Functions (3/28) Scripts A function is a black box that gets some input and produces some output We do not care about the inner workings of a function Functions provide reusable code Functions have private workspaces The only variables in the calling program that can be seen by the function are those in the input list The only variables in the function that can be seen by the calling program are those in the output list 同停大学 TONGJI UNIVERSITY
A function is a black box that gets some input and produces some output We do not care about the inner workings of a function Functions provide reusable code Functions have private workspaces ✓ The only variables in the calling program that can be seen by the function are those in the input list ✓ The only variables in the function that can be seen by the calling program are those in the output list User-defined Functions (3/28) Scripts
User-defined Functions (4/28) output argument Scripts name of the function input argument function distance dist2(x1,y1,x2,y2) DIST2 Calculate the distance between two points Function DIST2 calculates the distance between two points (xi,yl)and (x2,y2)in a Cartesian 号 coordinate system. Define variables: H1 comment line x1 x-position of point 1 % yl y position of point 1 2 x-position of point 2 other comment lines 号 y2 y-position of point 2 号 distance - Distance between points executable code % Record of revisions: Date Programmer Description of change 号 ==== ≤左=======三 ==================== 号 12/15198 S.J.Chapman Original code CaIculate distance. fdistance sgrt((x2-x1).^2+(y2-y1).^2); 同济大学 TONGJI UNIVERSITY
output argument input argument other comment lines executable code H1 comment line name of the function function distance = dist2(x1, y1, x2, y2) %DIST2 Calculate the distance between two points % Function DIST2 calculates the distance between % two points (x1,y1) and (x2,y2) in a Cartesian % coordinate system. % Define variables: % x1 -- x-position of point 1 % y1 -- y-position of point 1 % x2 -- x-position of point 2 % y2 -- y-position of point 2 % distance -- Distance between points % Record of revisions: % Date Programmer Description of change % ==== ========== ===================== % 12/15/98 S. J. Chapman Original code % Calculate distance. distance = sqrt((x2-x1).^2 + (y2-y1).^2); User-defined Functions (4/28) Scripts
User-defined Functions (5/28) Scripts The function statement marks the beginning of a function The name of the function must be the same as the name of the m-file The lookfor command searches functions according to the HI comment line The help command displays the comment lines from the H1 line until the first non-comment line 细月济大学 TONGJI UNIVERSITY
The function statement marks the beginning of a function The name of the function must be the same as the name of the m-file The lookfor command searches functions according to the H1 comment line The help command displays the comment lines from the H1 line until the first non-comment line User-defined Functions (5/28) Scripts
User-defined Functions (6/28) Pass-By-Value Example 1 ▣Command window: ▣my script..m: X=2; disp(Hello')方 my_script x=5; Hello! y=x+2 y= 7 @日济大学 TONGJIUNIVERSITY
Command window: x = 2; my_script Hello! y = x + 2 y = 7 my_script.m: disp( 'Hello' ); x = 5; Example 1 User-defined Functions (6/28) Pass-By-Value
User-defined Functions (7/28) Pass-By-Value Example 2 1 function out=sample(a,b) 2- fprintf('In sample:a=%f,b=%f %f\n',a,b) 3- a=b(1)+2*a: 4- b=a.*b; 5- out=a+b(1); 6- fprintf('In sample:a=%f,b=%f %f\n',a,b) 1- a=2:b=[64]: 2- fprintf('Before sample:a=%f,b=%f %f\n',a,b); 3- out=sample(a,b); 4- fprintf('After sample:a=%f,b=%f %f\n',a,b); 5- fprintf('After sample:out=%f\n',out); By execute the second M.file,what will appear in the command window? 翻凡济大学 TONGJI UNIVERSITY
Example 2 By execute the second M.file ,what will appear in the command window? User-defined Functions (7/28) Pass-By-Value
User-defined Functions (8/28) Pass-By-Value The answer: Before sample:a=2.000000,b=6.000000 4.000000 n sample:a=2.000000,b=6.0000004.000000 n sample:a=10.000000,b=60.00000040.000000 After sample:a=2.00000o,b=6.0000004.000000 After sample:out=70.000000 @日济大学 TONGJI UNIVERSITY
The answer: User-defined Functions (8/28) Pass-By-Value
User-defined Functions (9/28) Pass-By-Value MATLAB programs communicate with their functions using a Pass- by-value scheme. When a function calls occurs,Matlab makes a copy of the actual arguments and passes them to the function. Maybe the function modifies the copy,but it will not affect the original data in the caller. This feature helps to prevent unintended side effects in which an error in the function might unintentionally modify variables in the calling program. PHAW 细月济大学 TONGJI UNIVERSITY
MATLAB programs communicate with their functions using a Passby-value scheme. When a function calls occurs, Matlab makes a copy of the actual arguments and passes them to the function. Maybe the function modifies the copy ,but it will not affect the original data in the caller. This feature helps to prevent unintended side effects in which an error in the function might unintentionally modify variables in the calling program. User-defined Functions (9/28) Pass-By-Value
User-defined Functions (10/28) Optional Arguments Many MATLAB functions support optional input arguments and output arguments,such as plot,max. How do MATLAB functions know how many input and output arguments are present,and how do they adjust their behavior accordingly? There are 8 special functions that can be used by MATLAB functions to deal with the optional arguments and to report errors in those arguments.Here we will introduce6 of them and the remaining 2 will be introduced in the following lessons. 同濟大学 AW TONGJI UNIVERSITY
Many MATLAB functions support optional input arguments and output arguments, such as plot, max. How do MATLAB functions know how many input and output arguments are present, and how do they adjust their behavior accordingly? There are 8 special functions that can be used by MATLAB functions to deal with the optional arguments and to report errors in those arguments. Here we will introduce 6 of them and the remaining 2 will be introduced in the following lessons. User-defined Functions (10/28) Optional Arguments