Lecture Note 4 Fall 22 23
Lecture Note 4 Fall 22 23
Introduction to MATLAB
for Engineers, Third Edition
William J. Palm III
Chapter 3
Functions and Files (Cont’d)
3-19 (continued …)
User-Defined Functions: Example (continued)
>>z = fun(3,7)
z =
303
(continued …)
3-20
User-Defined Functions: Example (continued)
>>fun(3,7)
ans =
303
>>z
??? Undefined function or variable ’z’.
(continued …)
3-21
User-Defined Functions: Example (continued)
>>q = fun(3,7)
q =
303
3-22
Local Variables: The variables x and y are local to the
function fun, so unless you pass their values by naming
them x and y, their values will not be available in the
workspace outside the function. The variable u is also
local to the function. For example,
>>x = 3;y = 7;
>>q = fun(x,y);
>>x
x =
3
>>y
y =
7
>>u
??? Undefined function or variable ’u’.
3-23
Only the order of the arguments is important, not the
names of the arguments:
>>x = 7;y = 3;
>>z = fun(y,x)
z =
303
3-24
You can use arrays as input arguments:
>>r = fun(2:4,7:9)
r =
300 393 498
3-25
A function may have more than one output. These are
enclosed in square brackets.
3-26
The function is called as follows, if the radius is 4.
>>[A, C] = circle(4)
A =
50.2655
C =
25.1327
3-27
A function may have no input arguments and no output
list.
function show_date
clear
clc
today = date
3-28
Examples of Function Definition Lines
1. One input, one output:
3-29
Function Example
(continued …)
3-30
Function Example (continued)
>>a = 32.2;
>>initial_speed = 10;
>>time = 5;
>>[feet_dropped,speed] = . . .
drop(a,initial_speed,time)
(continued …)
3-31
Function Example (continued)
[feet_dropped,speed] = drop(32.2,10,5)
[feet_dropped,speed]=drop(32.2,10,0:1:5)
3-32
Local Variables
3-33
Global Variables
global a x q
function y = f1(x)
y = x + 2*exp(-x) - 3;
3-35
To minimize the function f = xe-x2 - y2 , we first define it in
an M-file, using the vector x whose elements are x(1) =
x and x(2) = y.
function f = f4(x)
f = x(1).*exp(-x(1).^2-x(2).^2);
>>fminsearch(@f4,[0,0])
ans =
-0.7071 0.000
3-45
Methods for Calling Functions
(continued …)
3-46
Methods for Calling Functions (continued)
function y = fun1(x)
y = x.^2-4;
(continued …)
3-47
Methods for Calling Functions (continued)
>>fun1 = ’x.^2-4’;
>>fun_inline = inline(fun1);
>>[x, value] = fzero(fun_inline,[0, 3])
(continued …)
3-48
Methods for Calling Functions (continued)
4. As a string expression:
>>fun1 = ’x.^2-4’;
>>[x, value] = fzero(fun1,[0, 3])
or as
(continued …)
3-49
Methods for Calling Functions (continued)
3-50
Types of User-Defined Functions
(continued …)
3-51
Types of User-Defined Functions
Primary functions
• This type of function is the first function in an M-file and typically
contains the main program.
• Following the primary function in the same file can be any number of
subfunctions, which can serve as subroutines to the primary function.
• Usually the primary function is the only function in an M-file that you
can call from the MATLAB command line or from another M-file
function.
• You invoke this function using the name of the M-file in which it is
defined.
• We normally use the same name for the function and its file, but if the
function name differs from the file name, you must use the file name to
invoke the function.
(continued …)
3-51
Types of User-Defined Functions (continued)
Anonymous functions
• Enable you to create a simple function without needing to create an
M-file for it.
• You can construct an anonymous function either at the MATLAB
command line or from within another function or script.
• Thus, anonymous functions provide a quick way of making a
function from any MATLAB expression without the need to create,
name, and save a file.
(continued …)
3-53
Anonymous Functions
(continued …)
3-59
Anonymous Functions (continued)
>>sq([5,7])
ans =
25 49
3-60 (continued …)
Anonymous Functions (continued)
3-62
Multiple Input Arguments
Then type
>>sqrtsum(3, 4)
ans =
5
3-63
As another example, consider the function defining a
plane, z = Ax + By. The scalar variables A and B
must be assigned values before you create the
function handle. For example,
>>A = 6; B = 4:
>>plane = @(x,y) A*x + B*y;
>>z = plane(2,8)
z =
44
3-64
Calling One Function within Another
3-65
Variables and Anonymous Functions
(continued …)
3-66
Variables and Anonymous Functions (continued)
Subfunctions
• These are placed in the primary function and are called by the
primary function.
• You can use multiple functions within a single primary function M-file.
(continued …)
3-54
Subfunctions
(continued …)
3-68
Subfunctions (continued)
(continued …)
3-70
Precedence When Calling Functions (continued)
3-71
The following example shows how the MATLAB M-
function mean can be superceded by our own
definition of the mean, one which gives the root-mean
square value.
function y = subfun_demo(a)
y = a - mean(a);
%
function w = mean(x)
w = sqrt(sum(x.^2))/length(x);
(continued …)
3-72
Example (continued)
>>a=[4,-4];
>>b = a - mean(a)
b =
4 -4
3-73
Thus the use of subfunctions enables you to reduce
the number of files that define your functions.
Nested functions
• These are functions defined within another function. They can help to
improve the readability of your program and also give you more
flexible access to variables in the M-file.
• The difference between nested functions and subfunctions is that
subfunctions normally cannot be accessed outside of their primary
function file.
(continued …)
3-54
Nested Functions
(continued …)
3-75
Nested Functions (continued)
(continued …)
3-76
Example
function f = parabola(a, b, c)
f = @p;
function y = p(x)
y = a*x^2 + b*x + c;
end
end
(continued …)
3-77
Example (continued)
3-78
Nested functions might seem to be the same as
subfunctions, but they are not. Nested functions
have two unique properties:
(continued …)
3-79
2. If you construct a function handle for a nested
function, the handle not only stores the information
needed to access the nested function; it also stores the
values of all variables shared between the nested
function and those functions that contain it.
Overloaded functions
• These are functions that respond differently to different types of input
arguments.
• They are similar to overloaded functions in any object-oriented
language.
• For example, an overloaded function can be created to treat integer
inputs differently than inputs of class double.
Private functions
• These functions enable you to restrict access to a function.
• They can be called only from an M-file function in the parent
directory.
(continued …)
3-56
Private Functions
(continued …)
3-81
Private Functions (continued)
3-82
The term function function
• This not a separate function type but refers to
any function that accepts another function as an
input argument, such as the function fzero.
3-58
Importing Spreadsheet Files
num = xlsread(filename)
num = xlsread(filename,sheet)
num = xlsread(filename,xlRange)
num = xlsread(filename,sheet,xlRange)
• Reads from the specified worksheet and range.
[num,txt,raw] = xlsread(___)
• Additionally returns the
• text fields in cell array txt
• both numeric and text data in cell array raw,
using any of the input arguments in the
previous syntaxes
filename = 'myExample.xlsx';
sheet = 1;
xlRange = 'B2:C3';
subsetA = xlsread(filename,sheet,xlRange)
[num,txt,raw] = xlsread('myExample.xlsx')
num =
1 2 3
4 5 NaN
7 8 9
txt =
'First' 'Second' 'Third'
'' '' ''
'' '' 'x'
raw =
'First' 'Second' 'Third'
[ 1] [ 2] [ 3]
[ 4] [ 5] 'x'
[ 7] [ 8] [ 9]
(continued …)
3-84
The Import Wizard (continued)
3-86
The first screen in the Import Wizard. Figure 3.4–1, page 139
3-87