Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

Similar presentations


Presentation on theme: "Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data."— Presentation transcript:

1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available) Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available)

2 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 2 CONTENTS  Introduction to Matlab Functions  Optional Arguments  Sharing Data Using Global Memory  Preserving Data Between Calls to a Function  Function Functions  Subfunctions

3 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 3 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.

4 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 4 INTRODUCTION Dummy arguments or placeholders 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. Displayed if a text is found by lookfor command Displayed if the function is found by help command Optional (end) Important: if the input variables are changed in the functions they will not change in the main program (PASS-BY-VALUE SCHEME)

5 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 5 INTRODUCTION Example,

6 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 6 INTRODUCTION Example,

7 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 7 INTRODUCTION Exercise, make two functions that converts polar coordinates (r,theta) to rectangular coordinates (x,y) and vise versa. (theta in degrees). Use the following relations for conversion:

8 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 8 INTRODUCTION Exercise, program a function that sorts the data in ascending order.

9 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 9 OPTIONAL ARGUMENTS (Input and Output) Optional output Optional input

10 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 10 OPTIONAL ARGUMENTS arguments

11 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 11 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);

12 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 12 OPTIONAL ARGUMENTS Example,

13 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 13 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.

14 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 14 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 var1 var2 var3 …

15 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 15 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.

16 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 16 FUNCTION FUNCTIONS Input variables of this type of function is the name of other functions. Example,

17 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 17 FUNCTION FUNCTIONS

18 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 18 FUNCTION FUNCTIONS,,

19 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 19 FUNCTION FUNCTIONS

20 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 20 FUNCTION FUNCTIONS Example, make a function that plots directly any function providing the function name and interval.

21 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 21 SUBFUNCTIONS Functions in the same file Normal/principle function: The m-file name should be mystats Subfunction: Accessible only by the other functions in the same file

22 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 22 ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional line plots: plot3(x,y,z); Example: (2 dimensional )

23 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 23 ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional line plots: plot3(x,y,z); Example: (3 dimensional)

24 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 24 ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional surface, mesh, and contour plots:

25 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 25 ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional surface, mesh, and contour plots:

26 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 26 ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional surface, mesh, and contour plots:

27 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 27 ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional surface, mesh, and contour plots:

28 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 28 ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) Three dimensional surface, mesh, and contour plots:


Download ppt "Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data."

Similar presentations


Ads by Google