同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 06 User-defined Functions

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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 05 Plotting.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 04 Branches and Loops.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 03Top-down and bottom-up design.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 02 MATLAB Basics(负责人:陈明).ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 01 MATLAB Programming for Mechanical Engineering(Introduction to MATLAB).ppt
- 同济大学:《Matlab在机械设计中的应用》课程教学资源(试卷习题)Final Examination(B)The First Semester(2013-2014).pdf
- 同济大学:《Matlab在机械设计中的应用》课程教学资源(试卷习题)Final Examination(A)The First Semester(2013-2014).pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第6章 齿轮.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第2章 平面机构的结构分析.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第8章 其他常见机构.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第9章 机械中的摩擦与效率.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第5章 凸轮机构.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第10章 机械的平衡.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第7章 轮系(Gear Train).pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第3章 平面机构的运动分析(Kinematic Analysis).pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第4章 平面四杆机构.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第1章 绪论(负责人:倪娟).pdf
- 运城学院:《机械原理》课程教学资源(教案讲义)第1章 绪论(打印版).pdf
- 运城学院:《机械原理》课程教学资源(教案讲义)第9章 机械中的摩擦和效率.doc
- 运城学院:《机械原理》课程教学资源(教案讲义)第8章 其他常用机构.doc
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 07 Sparse Arrays, Cell Arrays, and Structures.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 08 Advanced Mathematics.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 09 Probability and statistics.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 10 Graphical User Interface.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 11 Simulink.pptx
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第一章 内燃机工作原理及总体构造 The Working Principles and Overall Structure of Internal Combustion Engines.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第二章 机体组及曲柄连杆机构 Engine Block, Crank and Connecting Rod Mechanism.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第三章 配气机构 Valve Trains(负责人:李理光).pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第四章 汽油机燃油供给系统 Fuel Supply System For Gasoline Engine.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第六章 进气、排气及增压系统 Intake, Exhaust and Boost Systems.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第五章 柴油机燃油供给系统 Fuel Supply System for Diesel Engines.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第七章 发动机冷却系 Cooling System.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第九章 起动系统 Starting System.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第八章 发动机润滑系 Lubrication System for Automotive Engines.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第十章 发动机点火系统 Engine Ignition System.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第十二章 发动机有害排放物的控制系统 Control System of Harmful Emissions in Engine Exhaust.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)Automobile structure(Types of modern automobiles).pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)Chapter 13 传动系统 Overview of automobile drivetrain.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)Chapter 14 离合器 Clutch.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)Chapter 15 变速箱 Transmission and transfer case.pdf