matlab programming for engineers dr. nidal farhat introduction to matlab matlab basics branching...
TRANSCRIPT
![Page 1: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/1.jpg)
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)
![Page 2: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/2.jpg)
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
![Page 3: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/3.jpg)
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.
![Page 4: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/4.jpg)
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)
![Page 5: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/5.jpg)
5
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
INTRODUCTIONINTRODUCTION
Example,
![Page 6: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/6.jpg)
6
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
INTRODUCTIONINTRODUCTION
Example,
![Page 7: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/7.jpg)
8
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
INTRODUCTIONINTRODUCTION
Exercise, program a function that sorts the data in ascending order.
![Page 8: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/8.jpg)
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
![Page 9: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/9.jpg)
10
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
OPTIONAL ARGUMENTSOPTIONAL ARGUMENTS
arguments
![Page 10: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/10.jpg)
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);
![Page 11: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/11.jpg)
12
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
OPTIONAL ARGUMENTSOPTIONAL ARGUMENTS
Example,
![Page 12: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/12.jpg)
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.
![Page 13: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/13.jpg)
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 …
![Page 14: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/14.jpg)
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.
![Page 15: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/15.jpg)
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,
![Page 16: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/16.jpg)
17
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
FUNCTION FUNCTIONS FUNCTION FUNCTIONS
![Page 17: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/17.jpg)
18
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
FUNCTION FUNCTIONS FUNCTION FUNCTIONS
, ,
![Page 18: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/18.jpg)
19
CH5: USER DEFINED FUNCTIONS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
FUNCTION FUNCTIONS FUNCTION FUNCTIONS
![Page 19: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/19.jpg)
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.
![Page 20: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/20.jpg)
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
![Page 21: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/21.jpg)
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 )
![Page 22: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/22.jpg)
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)
![Page 23: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/23.jpg)
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:
![Page 24: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/24.jpg)
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:
![Page 25: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/25.jpg)
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:
![Page 26: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/26.jpg)
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:
![Page 27: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data](https://reader036.vdocuments.net/reader036/viewer/2022062301/56649eb45503460f94bbd1e2/html5/thumbnails/27.jpg)
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: