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