Matlab Programming for Engineers Introduction to Matlab Basics
Matlab Programming for Engineers Introduction to Matlab Basics Branching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available) Dr. Bashir NOURI
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS CONTENTS § Introduction to Matlab Functions § Optional Arguments § Sharing Data Using Global Memory § Preserving Data Between Calls to a Function § Functions § Subfunctions Mechanical Engineering Department 2
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS INTRODUCTION Advantages: § 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. Mechanical Engineering Department 3
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS INTRODUCTION (end) Optional Dummy arguments or placeholders Displayed if a text is found by lookfor command Displayed if the function is found by help command The name of the m. file must be the same as the fname. The function can be called directly in the command window, or by any other program or function. Important: if the input variables are changed in the functions they will not change in the main program (PASS-BY-VALUE SCHEME) Mechanical Engineering Department 4
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS INTRODUCTION Example, Mechanical Engineering Department 5
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS INTRODUCTION Example, Mechanical Engineering Department 6
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS INTRODUCTION Exercise, program a function that sorts the data in ascending order. Mechanical Engineering Department 8
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS OPTIONAL ARGUMENTS (Input and Output) Optional output Mechanical Engineering Department Optional input 9
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS OPTIONAL ARGUMENTS arguments Mechanical Engineering Department 10
CH 5: USER DEFINED FUNCTIONS MATLAB PROGRAMMING FOR ENGINEERS OPTIONAL ARGUMENTS nin = nargin; nout = nargout; message = nargchk(min_arg, max_arg, num_arg); message empty if OK, otherwise standard error message is returned error ('message') warning ('message') name = inputname(argno); Mechanical Engineering Department 11
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS OPTIONAL ARGUMENTS Example, Mechanical Engineering Department 12
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS SHARING DATA USING GLOBAL MEMORY Global memory is a special memory that can be accessed from any workspace. Syntax: global var 1 var 2 var 3 … 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. Mechanical Engineering Department 13
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS PRESERVING DATA BETWEEN CALLS TO A FUNCTION Each time the function is finished data in the workspace is destroyed. To make some variable 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 var 1 var 2 var 3 … Mechanical Engineering Department 14
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS 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 value. Each time he enters a value the program calls the function and then displays the current output (average and standard deviation). Use this table to test your program. Mechanical Engineering Department 15
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS Input variables of this type of function is the name of other functions. Example, Mechanical Engineering Department 16
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS Mechanical Engineering Department 17
CH 5: USER DEFINED FUNCTIONS MATLAB PROGRAMMING FOR ENGINEERS FUNCTIONS , , Mechanical Engineering Department 18
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS Mechanical Engineering Department 19
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS Example, make a function that plots directly any function providing the function name and interval. Mechanical Engineering Department 20
CH 5: USER DEFINED FUNCTIONS MATLAB PROGRAMMING FOR ENGINEERS SUBFUNCTIONS Normal/principle function: The m-file name should be mystats Functions in the same file Subfunction: Accessible only by the other functions in the same file Mechanical Engineering Department 21
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional line plots: plot 3(x, y, z); Example: (2 dimensional ) Mechanical Engineering Department 22
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional line plots: plot 3(x, y, z); Example: (3 dimensional) Mechanical Engineering Department 23
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional surface, mesh, and contour plots: Mechanical Engineering Department 24
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional surface, mesh, and contour plots: Mechanical Engineering Department 25
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional surface, mesh, and contour plots: Mechanical Engineering Department 26
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional surface, mesh, and contour plots: Mechanical Engineering Department 27
MATLAB PROGRAMMING FOR ENGINEERS CH 5: USER DEFINED FUNCTIONS ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional surface, mesh, and contour plots: Mechanical Engineering Department 28
- Slides: 27