Professional Documents
Culture Documents
Mat Lab Chapter 3
Mat Lab Chapter 3
Introduction to MATLAB
for Engineers, Third Edition
William J. Palm III
Chapter 3
Functions and
Files
Copyright © 2010. The McGraw-Hill Companies, Inc. This work is only for
non-profit use by instructors in courses for which this textbook has been
adopted. Any other use without publisher’s consent is unlawful.
Getting Help for Functions
3-2
Common mathematical functions: Table 3.1–1, page 114
Exponential
exp(x) Exponential; e x
Logarithmic
log(x) Natural logarithm; ln x
(continued…)
3-3
Some common mathematical functions (continued)
Complex
abs(x) Absolute value.
angle(x) Angle of a complex number.
conj(x) Complex conjugate.
imag(x) Imaginary part of a complex number.
real(x) Real part of a complex number.
(continued…)
3-4
Some common mathematical functions
(continued)
Numeric
ceil(x) Round to nearest integer toward ∞.
fix(x) Round to nearest integer toward zero.
floor(x) Round to nearest integer toward -∞.
round(x) Round toward nearest integer.
sign(x) Signum function:
+1 if x > 0; 0 if x = 0; -1 if x < 0.
3-5
The rectangular
and polar
representations
of the complex
number a + ib.
3-6
Operations with Complex Numbers
>>x = -3 + 4i;
>>y = 6 - 8i;
>>mag_x = abs(x)
mag_x =
5.0000
>>mag_y = abs(y)
mag_y =
10.0000
>>mag_product = abs(x*y)
50.0000
(continued …)
3-7
Operations with Complex Numbers (continued)
>>angle_x = angle(x)
angle_x =
2.2143
>>angle_y = angle(y)
angle_y =
-0.9273
>>sum_angles = angle_x + angle_y
sum_angles =
1.2870
>>angle_product = angle(x*y)
angle_product =
1.2870
3-8
Operations on Arrays
MATLAB will treat a variable as an array automatically.
For example, to compute the square roots of 5, 7, and
15, type
>>x = [5,7,15];
>>y = sqrt(x)
y =
2.2361 2.6358 3.8730
3-9
Expressing Function Arguments
We can write sin 2 in text, but MATLAB requires
parentheses surrounding the 2 (which is called the
function argument or parameter).
(continued …)
3-10
Expressing Function Arguments (continued)
3-11
Expressing Function Arguments (continued)
Another common mistake involves expressions like
sin2 x, which means (sin x)2.
3-12
Expressing Function Arguments (continued)
3-13
Trigonometric functions: Table 3.1–2, page 116
3-14
Inverse Trigonometric functions: Table 3.1–2
3-15
Hyperbolic functions: Table 3.1–3, page 119
3-16
Inverse Hyperbolic functions: Table 3.1–3
3-17
User-Defined Functions
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;
>>plot(0:0.01:6,@f1)
3-35
Finding Zeros of a Function
fzero(@function, x0)
3-36
Using fzero with User-Defined Functions
function y = f1(x)
y = x + 2*exp(-x) - 3;
(continued …)
3-37
Plot of the function y x 2ex 3. Figure 3.2–1, page
125
There is a
zero near x =
-0.5 and one
near x = 3.
3-38 (continued …)
Example (continued)
>>x = fzero(@f1,-0.5)
3-40
When using fminbnd it is more convenient to define the
function in a function file. For example, if y 1 xe x ,
define the following function file:
function y = f2(x)
y = 1-x.*exp(-x);
>>x = fminbnd(@f2,0,5)
3-41
A function can have one or more local minima
and a global minimum.
If the specified range of the independent variable
does not enclose the global minimum, fminbnd
will not find the global minimum.
fminbnd will find a minimum that occurs on a
boundary.
3-42
Plot of the function y = 0.025x 5 0.0625x 4 0.333x 3 x 2.
Figure 3.2–2
This function
has one local
and one
global
minimum.
On the
interval [1, 4]
the minimum
is at the
boundary, x
= 1.
3-43
To find the minimum of a function of more than one
variable, use the fminsearch function. One form of its
syntax is
fminsearch(@function, x0)
3-44
2 2
To minimize the function f = xex y , 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
3-46 (continued …)
Methods for Calling Functions (continued)
function y = fun1(x)
y = x.^24;
(continued …)
3-47
Methods for Calling Functions (continued)
>>fun1 = ’x.^24’;
>>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 (continued)
(continued …)
3-52
Types of User-Defined Functions (continued)
(continued …)
3-53
Types of User-Defined Functions (continued)
(continued …)
3-54
Types of User-Defined Functions (continued)
(continued …)
3-55
Types of User-Defined Functions (continued)
(continued …)
3-56
Types of User-Defined Functions (continued)
3-58
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)
(continued …)
3-68
Subfunctions (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);
3-72 (continued …)
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.
(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.
(continued …)
3-81
Private Functions (continued)
3-82
Importing Spreadsheet Files
(continued …)
3-84
The Import Wizard (continued)
3-86
The first screen in the Import Wizard. Figure 3.4–1, page 139
3-87
The following slides contain figures from
Chapter 3 and its homework problems.
3-88
Cross section of an irrigation channel.
Figure 3.2–3
3-89
Figure P12
3-90
Figure P13
3-91