Professional Documents
Culture Documents
Introduction To MATLAB For Engineers, Third Edition
Introduction To MATLAB For Engineers, Third Edition
Introduction to MATLAB
for Engineers, Third Edition
William J. Palm III
Chapter 3
Functions and
Files
3-2
Exponential; e x
sqrt(x)
Square root; x
Logarithmic
log(x)
Natural logarithm; ln x
log10(x)
(continued)
3-3
Complex
abs(x)
angle(x)
conj(x)
imag(x)
real(x)
Absolute value.
Angle of a complex number.
Complex conjugate.
Imaginary part of a complex number.
Real part of a complex number.
(continued)
3-4
3-5
The rectangular
and polar
representations
of the complex
number a + ib.
3-6
(continued )
3-7
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-9
3.8730
3-10
(continued )
3-12
3-13
3-14
cos(x)
Cosine; cos x.
cot(x)
Cotangent; cot x.
csc(x)
Cosecant; csc x.
sec(x)
Secant; sec x.
sin(x)
Sine; sin x.
tan(x)
Tangent; tan x.
acos(x)
acot(x)
acsc(x)
asec(x)
asin(x)
atan(x)
atan2(y,x)
3-15
3-16
cosh(x)
Hyperbolic cosine
coth(x)
Hyperbolic cotangent.
csch(x)
Hyperbolic cosecant
sech(x)
Hyperbolic secant
sinh(x)
Hyperbolic sine
tanh(x)
Hyperbolic tangent
3-17
acosh(x)
acoth(x)
acsch(x)
asech(x)
asinh(x)
atanh(x)
User-Defined Functions
The first line in a function file must begin with a function
definition line that has a list of inputs and outputs. This line
distinguishes a function M-file from a script M-file. Its syntax is
as follows:
function [output variables] = name(input variables)
3-19
(continued )
(continued )
3-20
(continued )
3-21
3-24
3-25
3-26
3-27
3-28
3-29
Function Example
function [dist,vel] = drop(g,vO,t);
% Computes the distance travelled and the
% velocity of a dropped object,
% as functions of g,
% the initial velocity vO, and
% the time t.
vel = g*t + vO;
dist = 0.5*g*t.^2 + vO*t;
(continued )
3-30
3-31
(continued )
Local Variables
The names of the input variables given in the function
definition line are local to that function.
This means that other variable names can be used when
you call the function.
All variables inside a function are erased after the function
finishes executing, except when the same variable names
appear in the output variable list used in the function call.
3-33
Global Variables
The global command declares certain variables global,
and therefore their values are available to the basic
workspace and to other functions that declare these
variables global.
The syntax to declare the variables a, x, and q is
global a x q
Any assignment to those variables, in any function or in
the base workspace, is available to all the other functions
declaring them global.
More? See pages 124.
3-34
Function Handles
You can create a function handle to any function by using
the at sign, @, before the function name. You can then use
the handle to reference the function. To create a handle to
the function y x 2ex 3, define the following function
file:
function y = f1(x)
y = x + 2*exp(-x) - 3;
You can pass the function as an argument to another
function. For example, we can plot the function over 0 x
6 as follows:
>>plot(0:0.01:6,@f1)
3-35
3-37
(continued )
3-38
(continued )
Example (continued)
>>x = fzero(@f1,-0.5)
The answer is x = -0.5881.
3-42
3-44
(continued )
3-47
(continued )
(continued )
3-48
(continued )
3-49
(continued )
3-51
(continued )
3-53
(continued )
3-54
(continued )
3-55
3-56
(continued )
3-58
Anonymous Functions
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. The syntax for creating an anonymous
function from an expression is
fhandle = @(arglist) expr
where arglist is a comma-separated list of input
arguments to be passed to the function, and expr is
any single, valid MATLAB expression.
3-59
(continued )
(continued )
3-63
3-64
3-65
(continued )
3-66
Subfunctions
A function M-file may contain more than one user-defined
function. The first defined function in the file is called the
primary function, whose name is the same as the M-file
name. All other functions in the file are called subfunctions.
Subfunctions are normally visible only to the primary
function and other subfunctions in the same file; that is, they
normally cannot be called by programs or functions outside
the file. However, this limitation can be removed with the
use of function handles.
3-68
(continued )
Subfunctions (continued)
Create the primary function first with a function
definition line and its defining code, and name the
file with this function name as usual.
Then create each subfunction with its own function
definition line and defining code.
The order of the subfunctions does not matter, but
function names must be unique within the M-file.
3-69
(continued )
The following example shows how the MATLAB Mfunction mean can be superceded by our own
definition of the mean, one which gives the root-mean
square value.
The function mean is a subfunction.
The function subfun_demo is the primary function.
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 sample session follows.
>>y = subfn_demo([4, -4])
y =
1.1716 -6.8284
If we had used the MATLAB M-function mean, we
would have obtained a different answer; that is,
>>a=[4,-4];
>>b = a - mean(a)
b =
4
-4
3-73
Nested Functions
With MATLAB 7 you can now place the definitions of
one or more functions within another function.
Functions so defined are said to be nested within the
main function. You can also nest functions within
other nested functions.
(continued )
3-75
(continued )
3-76
Example
The following example constructs a function handle for a
nested function and then passes the handle to the
MATLAB function fminbnd to find the minimum point on
a parabola. The parabola function constructs and
returns a function handle f for the nested function p.
This handle gets passed to fminbnd.
function f = parabola(a, b, c)
f = @p;
function y = p(x)
y = a*x^2 + b*x + c;
end
end
3-77
(continued )
Example (continued)
In the Command window type
>>f = parabola(4, -50, 5);
>>fminbnd(f, -10, 10)
ans =
6.2500
Note than the function p(x) can see the variables a,
b, and c in the calling functions workspace.
3-78
Private Functions
Private functions reside in subdirectories with the
special name private, and they are visible only to
functions in the parent directory.
Assume the directory rsmith is on the MATLAB
search path. A subdirectory of rsmith called
private may contain functions that only the
functions in rsmith can call. Because private
functions are invisible outside the parent directory
rsmith, they can use the same names as functions
in other directories.
(continued )
3-81
3-82
(continued )
3-84
3-85
The first screen in the Import Wizard. Figure 3.41, page 139
3-88
Figure 3.23
3-89
Figure P12
3-90
Figure P13
3-91