Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching...
-
Upload
marian-cole -
Category
Documents
-
view
238 -
download
4
Transcript of Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching...
1
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
Matlab Programming for Engineers Matlab Programming for Engineers Matlab Programming for Engineers Matlab Programming for Engineers
Dr. Nidal Farhat
Introduction to MatlabIntroduction to Matlab Matlab Basics Matlab Basics Branching Branching Statements Statements Loops Loops User Defined FunctionsUser Defined Functions Additional Data Types Additional Data Types Input/Output Functions Input/Output Functions Simulink Toolbox Simulink Toolbox Important Toolboxes (if time is available) Important Toolboxes (if time is available)
Introduction to MatlabIntroduction to Matlab Matlab Basics Matlab Basics Branching Branching Statements Statements Loops Loops User Defined FunctionsUser Defined Functions Additional Data Types Additional Data Types Input/Output Functions Input/Output Functions Simulink Toolbox Simulink Toolbox Important Toolboxes (if time is available) Important Toolboxes (if time is available)
2
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
CONTENTS 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
INTRODUCTION 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
INTRODUCTION 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
INTRODUCTIONINTRODUCTION
Example,
6
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
INTRODUCTIONINTRODUCTION
Example,
8
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
INTRODUCTIONINTRODUCTION
Exercise, program a function that sorts the data in ascending order.
9
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
OPTIONAL ARGUMENTS (Input and Output)OPTIONAL ARGUMENTS (Input and Output)
Optional output Optional input
10
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
OPTIONAL ARGUMENTSOPTIONAL ARGUMENTS
arguments
11
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
OPTIONAL ARGUMENTSOPTIONAL 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
OPTIONAL ARGUMENTSOPTIONAL ARGUMENTS
Example,
13
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
SHARING DATA USING GLOBAL MEMORYSHARING 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
PRESERVING DATA BETWEEN CALLS TO A FUNCTION 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
PRESERVING DATA BETWEEN CALLS TO A FUNCTION 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
FUNCTION FUNCTIONS 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
FUNCTION FUNCTIONS FUNCTION FUNCTIONS
18
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
FUNCTION FUNCTIONS FUNCTION FUNCTIONS
, ,
19
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
FUNCTION FUNCTIONS FUNCTION FUNCTIONS
20
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
FUNCTION FUNCTIONS 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
SUBFUNCTIONS 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
ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) 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
ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) 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
ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL)
Three dimensional surface, mesh, and contour plots:
25
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL)
Three dimensional surface, mesh, and contour plots:
26
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL)
Three dimensional surface, mesh, and contour plots:
27
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL)
Three dimensional surface, mesh, and contour plots:
28
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL)
Three dimensional surface, mesh, and contour plots: