0% found this document useful (0 votes)
425 views

CH 7 MATLAB

1. The document describes a lecture on numerical methods in computing given by Dr. Taimoor Iqbal at the Department of Mathematics, UET Lahore. 2. The chapter discusses creating user-defined functions and function files in MATLAB, including their structure, local and global variables, and how to define and use anonymous functions. 3. Examples are provided on defining, saving, and calling user-defined functions from the command window or other files, as well as using anonymous functions defined within code.

Uploaded by

Aqib Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
425 views

CH 7 MATLAB

1. The document describes a lecture on numerical methods in computing given by Dr. Taimoor Iqbal at the Department of Mathematics, UET Lahore. 2. The chapter discusses creating user-defined functions and function files in MATLAB, including their structure, local and global variables, and how to define and use anonymous functions. 3. Examples are provided on defining, saving, and calling user-defined functions from the command window or other files, as well as using anonymous functions defined within code.

Uploaded by

Aqib Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Department of Mathematics, UET, Lahore

Numerical Methods in Computing

Lectures By: Dr Taimoor Iqbal


Email: [email protected]
Department of Mathematics, UET, Lahore

Course title: Numerical Methods in Computing


Credit hours: 3
Recommended book:

MATLAB An Introduction with Applications, 5th


Edition, Amos Gilat
Lecture By: Dr Taimoor Iqbal
Department of Mathematics, UET, Lahore

CHAPTER 7

User-Defined Functions and Function Files

Lecture By: Dr Taimoor Iqbal


1
Department of Mathematics, UET, Lahore

THIS CHAPTER DESCRIBES:


❑ Creating a function file
❑ Structure of a function file
❑ Local & global variables
❑ Using a user-defined function
❑ Anonymous functions
❑ Function functions
❑ Function handles
❑ Subfunctions
Lecture By: Dr Taimoor Iqbal
Department of Mathematics, UET, Lahore
Introduction
❖ A MATLAB function is a special type of .m-file that runs in its own
independent workspace

1. A user-defined function is a MATLAB program that is created by the


user, saved as a function file, and then used like a built-in function.
2. The function can be a simple single mathematical expression or a
complicated and involved series of calculations.

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

The first line in a function file must be the


function definition line.
Department of Mathematics, UET, Lahore
7.2 Structure of a Function File

Function definition line


Help text

Function body
(computer program)
Department of Mathematics, UET, Lahore
7.2.1 Function definition Line

The function definition line:


• Defines the file as a function file
• Defines the name of the function
• Defines the number and order of the input and output
arguments
Department of Mathematics, UET, Lahore
7.2.1 Function definition Line

❑The word “function,” typed in lowercase letters, must be the first


word in the function definition line.

❑The function name is typed following the equal sign.

❑The name can be made up of letters, digits, and the underscore


character (the name cannot include a space).

❑It is good practice to avoid names of built-in functions and names of


variables already defined by the user or predefined by MATLAB.
Department of Mathematics, UET, Lahore
7.2.2 Input & output Arguments
➢ The input arguments are listed inside parentheses following the
function name.
➢ If there are more than one, the input arguments are separated with
commas.
➢ The mathematical expressions in the function file must be written
according to the dimensions of the arguments, since the arguments
can be scalars, vectors, or arrays.

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

The H1 line and help text lines are


comment lines (lines that begin
with the percent, (%, sign)
following the function definition
line.
They are optional but are
frequently used to provide
information about the function.
Department of Mathematics, UET, Lahore
7.2.4 Function Body

The function body contains the


computer program (code) that
actually performs the
computations.
The code can use all MATLAB
programming features.
This includes calculations,
assignments, any built-in or user-
defined functions, flow control
(conditional statements and
loops), comments, blank lines, and
interactive input and output.
Department of Mathematics, UET, Lahore
7.3 Local & Global Variable
❑The variables are defined and recognized only inside the
function file.
❑ All the variables in a function file are local (the input and
output arguments and any variables that are assigned values
within the function file).
❑ The variable(s) used by a function cant be accessed by other
script(s).
❑To make a variable common (recognized) in several different
function files, and perhaps in the workspace too, declare the
variable global with the global command.

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

Using an anonymous function:


1. Once an anonymous function is defined, it can be used
by typing its name and a value for the argument (or
arguments) in parentheses.
2. Anonymous functions can also be used as arguments in
other functions.
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:

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.

▪ A function function, which accepts another function (imported


function), includes in its input arguments a name that represents
the imported function.
▪ The imported function name is used for the operations in the
program (code) of the function function.
▪ There are two methods for listing the name of an imported
function in the argument list of a function function. One is by
using a function handle, and the other is by typing the name of the
function that is being passed in as a string expression.
▪ Using function handles is easier and more efficient, and should be
the preferred method.
Department of Mathematics, UET, Lahore
7.9.1 Using Function Handles for Passing a Function into a
Function Function
Function handles are used for passing (importing) user-defined
functions, builtin functions, and anonymous functions into function
functions that can accept them.

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

1. For built-in and user-defined functions, a function handle is created

by typing the symbol @ (or Str2func) in front of the function name.

2. The function handle can also be assigned to a variable name.

3. Anonymous functions, their name is already a function handle.


Department of Mathematics, UET, Lahore
7.9.1 Using Function Handles for Passing a Function into a
Function Function

Writing a function function that accepts a function handle as an input argument:

1. The input arguments of a function function (which accepts another function)


includes a name (dummy function name) that represents the imported
function. This dummy function (including a list of input arguments enclosed in
parentheses) is used for the operations of the program inside the function
function.
2. The function that is actually being imported must be in a form consistent with
the way that the dummy function is being used in the program. This means
that both must have the same number and type of input and output
arguments.
Department of Mathematics, UET, Lahore
7.9.1 Using Function Handles for Passing a Function into a
Function Function

Writing a function function that accepts a function handle as an input argument:


Department of Mathematics, UET, Lahore
7.9.1 Using Function Handles for Passing a Function into a
Function Function

Writing a function function that accepts a function handle as an input argument:

A Simple Example:

Create a MATLAB function that accepts a function handle in the

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

1. It is possible to place more than one function in a single


file.
2. If more than one function is present in a file the top
function is primary function and the others are the sub
functions.
3. Sub functions are accessible in the same file only.
Department of Mathematics, UET, Lahore
7.10 Subfunctions
Example:

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

1. A nested function is a user-defined function that is


written inside another user defined function.
2. The portion of the code that corresponds to the nested
function starts with a function definition line and ends
with an end statement.
3. An end statement must also be entered at the end of the
function that contains the nested function. (Normally, a
user-defined function does not require a terminating
end statement. However, an end statement is required if
the function contains one or more nested functions.)
4. Nested functions can also contain nested functions.
Obviously, having many levels of nested functions can
be confusing.
Department of Mathematics, UET, Lahore
7.10 Subfunctions
Two nested functions:
Department of Mathematics, UET, Lahore
7.10 Subfunctions
Example:

Lets Go to MATLAB

You might also like