User defined Functions in MATLAB Part 1
-
Upload
shameer-ahmed-koya -
Category
Education
-
view
384 -
download
1
Transcript of User defined Functions in MATLAB Part 1
![Page 1: User defined Functions in MATLAB Part 1](https://reader036.fdocuments.us/reader036/viewer/2022082905/5871ea4f1a28ab6a7b8b7d17/html5/thumbnails/1.jpg)
1
User Defined Functions in MATLAB
Part 1
Shameer A Koya
![Page 2: User defined Functions in MATLAB Part 1](https://reader036.fdocuments.us/reader036/viewer/2022082905/5871ea4f1a28ab6a7b8b7d17/html5/thumbnails/2.jpg)
IntroductionO User-defined functions are similar to the
MATLAB pre-defined functionsO A function is a MATLAB program that can
accept inputs and produce outputs.O A function can be called or executed by
another program or function.O Code for a function is done in an Editor
window or any text editor same way as script and saved as m-file.
O The m-file must have the same name as the function.
2
![Page 3: User defined Functions in MATLAB Part 1](https://reader036.fdocuments.us/reader036/viewer/2022082905/5871ea4f1a28ab6a7b8b7d17/html5/thumbnails/3.jpg)
3
Introduction…OThe convention for naming functions is
the same as for variables.O It is important that you give meaningful
variable names to variables inside a function that you write, so that you and others can understand what the function does.
OThe function M-file must be saved in your current directory (otherwise it must be in the path)
![Page 4: User defined Functions in MATLAB Part 1](https://reader036.fdocuments.us/reader036/viewer/2022082905/5871ea4f1a28ab6a7b8b7d17/html5/thumbnails/4.jpg)
4
Why use functions?OFunctions provide reusable codeOUse same code in more than one place in program without rewriting code
OReuse code by calling in different programs
OMake debugging easier
![Page 5: User defined Functions in MATLAB Part 1](https://reader036.fdocuments.us/reader036/viewer/2022082905/5871ea4f1a28ab6a7b8b7d17/html5/thumbnails/5.jpg)
5
Before you start …O Identify the functionODecide the function nameODecide the input variablesODecide the output variablesOFile name must be the function name.
![Page 6: User defined Functions in MATLAB Part 1](https://reader036.fdocuments.us/reader036/viewer/2022082905/5871ea4f1a28ab6a7b8b7d17/html5/thumbnails/6.jpg)
6
Function Syntax
function[a, b, c]= basicmath(x,y)%BASICMATH Basic Mathematical function% basicmath(x,y) is a sample matlabfunction to perform% basic mathematical operations on input variables x & y% outputs of the function are sum, difference and product% of input arguments.
a = x + y;b = x – y;c = x * y;
Output arguments
Executable code
Comments about the function
Name of the function
Input arguments
declaration statement
![Page 7: User defined Functions in MATLAB Part 1](https://reader036.fdocuments.us/reader036/viewer/2022082905/5871ea4f1a28ab6a7b8b7d17/html5/thumbnails/7.jpg)
7
Syntax …O The declaration statement function is used to define
the file as a function.O It must be typed in lower case letters.O Input arguments
O Typed in side the parentheses ( )O Used to transfer data into function from calling program.O Can be zero or more input arguments.
O Output argumentsO Types inside the square brackets [ ]O Used to transfer data out of function to calling program.O Can be zero or more output arguments.
O Give a meaningful variable name.O Rules for giving function name is same as the rules for
variable names.
![Page 8: User defined Functions in MATLAB Part 1](https://reader036.fdocuments.us/reader036/viewer/2022082905/5871ea4f1a28ab6a7b8b7d17/html5/thumbnails/8.jpg)
More on Function Arguments
O Functions have private workspacesO Variables that are created inside of a user-defined
function are referred to as local variables.O They can only be accessed from inside of that
function.O After the function completes its operations, the
local variables are deleted from memory.O The only variable that appears in the workspace
is the output of the function.O Conversely, functions cannot access variables
from the workspace. (with the exception of any input parameters they might have or “global variables”---see MATLAB help on this matter).