CH 7 MATLAB
CH 7 MATLAB
CHAPTER 7
3. The input and the output can be one or several variables, and each
can be a scalar, a vector, or an array of any size.
Lecture By: Dr Taimoor Iqbal
Department of Mathematics, UET, Lahore
7.1 Creating a Function File
Function body
(computer program)
Department of Mathematics, UET, Lahore
7.2.1 Function definition Line
The input command can be used to input data interactively, and the
disp, fprintf, and plot commands can be used to display information on
the screen, save to a file, or plot figures just as in a script file.
Department of Mathematics, UET, Lahore
7.2.2 Input & output Arguments
Department of Mathematics, UET, Lahore
7.2.3 H1 & help text lines
global variable_name
Department of Mathematics, UET, Lahore
7.3 Local & Global Variable
❑ Several variables can be declared global by listing them, separated with spaces, in the
global command. For example:
global GRAVITY_CONST FrictionCoefficient
❑ The variable has to be declared global in every function file that the user wants it to
be recognized in. The variable is then common only to these files.
❑ The global command must appear before the variable is used. It is recommended to
enter the global command at the top of the file.
❑ The global command has to be entered in the Command Window, or in a script file,
for the variable to be recognized in the workspace.
❑ The variable can be assigned, or reassigned, a value in any of the locations in which
it is declared common.
❑ The use of long descriptive names (or all capital letters) is recommended for global
variables in order to distinguish them from regular variables.
Department of Mathematics, UET, Lahore
7.4 Saving a function file
Function files are saved with the extension .m.
Department of Mathematics, UET, Lahore
7.5 Using a USER-DEFINED function
A user-defined function is used in the same way as a built-in function.
Department of Mathematics, UET, Lahore
7.6 Examples:
Solution
Lets Go to MATLAB
Department of Mathematics, UET, Lahore
7.6 Examples:
Solution
Lets Go to MATLAB
Department of Mathematics, UET, Lahore
7.7 Comparison between script files & function files
1. Both script and function files are saved with the extension .m (that is
why they are sometimes called M-files).
2. The first executable line in a function file is (must be) the function
definition line.
3. The variables in a function file are local. The variables in a script file are
recognized in the Command Window.
4. Script files can use variables that have been defined in the workspace.
5. Script files contain a sequence of MATLAB commands (statements).
6. Function files can accept data through input arguments and can return
data through output arguments.
7. When a function file is saved, the name of the file should be the same
as the name of the function.
8. A user-defined function is used in the same way as a built-in function.
It can be used (called) in the Command Window, in a script file, or in
another function.
Department of Mathematics, UET, Lahore
7.8 Anonymus functions
❖ An anonymous function is a user-defined function that is defined
and written within the computer code (not in a separate function
file) and is then used in the code.
❖ Anonymous functions can be defined in any part of MATLAB (in
the Command Window, in script files, and inside regular user defined
functions).
Department of Mathematics, UET, Lahore
7.8 Anonymus functions
1. The command creates the anonymous function and assigns a handle for the
function to the variable name on the left-hand side of the = sign.
2. The expr consists of a single valid mathematical MATLAB expression.
3. The mathematical expression can have one or several independent variables. The
independent variable(s) is (are) entered in the (arglist). Multiple independent
variables are separated with commas. (for example: circle = @ (x,y)
16*x^2+9*y^2.)
4. The mathematical expression can include any built-in or user-defined functions.
5. The expression must be written according to the dimensions of the arguments.
(element-by-element or linear algebra calculations).
6. The expression can include variables that are already defined when the anonymous
function is defined. For example, if three variables a, b, and c are defined (have
assigned numerical values), then they can be used in the expression of the
anonymous function parabola = @ (x) a*x^2+b*x+c.
Department of Mathematics, UET, Lahore
7.8 Anonymus functions
Solution
Lets Go to MATLAB
Department of Mathematics, UET, Lahore
Example of using anonymous function:
Solution
Lets Go to MATLAB
Department of Mathematics, UET, Lahore
Example of using anonymous function:
Lets Go to MATLAB
Department of Mathematics, UET, Lahore
7.9 Function functions
A function that accepts another function is called in
MATLAB a function function.
Function handle:
❑ A function handle is a MATLAB value that is associated with a
function. It is a MATLAB data type and can be passed as an
argument into another function.
❑ Once passed, the function handle provides means for calling (using)
the function it is associated with.
❑ Function handles can be used with any kind of MATLAB function.
This includes built-in functions, user-defined functions (written in
function files), and anonymous functions.
Department of Mathematics, UET, Lahore
7.9.1 Using Function Handles for Passing a Function into a
Function Function
A Simple Example:
first argument and plot the function at the point specified in the
second argument. Moreover, add the plot title and the axes
titles.
Lets Go to MATLAB
Department of Mathematics, UET, Lahore
7.9.1 Using Function Handles for Passing a Function into a
Function Function
Department of Mathematics, UET, Lahore
7.9.1 Using Function Handles for Passing a Function into a
Function Function
Lets Go to MATLAB
Department of Mathematics, UET, Lahore
7.9.1 Using Function Handles for Passing a Function into a
Function Function
Lets Go to MATLAB
Department of Mathematics, UET, Lahore
7.10 Subfunctions
Lets Go to MATLAB
Department of Mathematics, UET, Lahore
7.10 Subfunctions
Example:
Lets Go to MATLAB
Department of Mathematics, UET, Lahore
7.11 Nested Functions
Lets Go to MATLAB