Getting Help For Functions
Getting Help For Functions
You can use the lookfor command to find functions that are
relevant to your application.
3-2
Common mathematical functions:
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
real(x) number.
Real part of a complex number.
3-4 (continued…)
Some common mathematical functions (continued)
Numeric
ceil(x) Round to nearest integer toward ∞.
fix(x) Round to nearest integer toward
floor(x) zero.
round(x)Round to nearest integer toward -∞.
sign(x) Round toward nearest integer.
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:
3-14
Inverse Trigonometric functions:
3-15
Hyperbolic functions
3-16
Inverse Hyperbolic functions:
3-17
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:
3-18
User-Defined Functions: Example
function z = fun(x,y)
u = 3*x;
z = u + 6*y.^2;
(continued …)
3-19
User-Defined Functions: Example (continued)
>>z = fun(3,7)
z =
303
(continued …)
3-20
User-Defined Functions: Example (continued)
Call this function without its output argument and try to access
its value. You will see an error message.
>>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
This means that other variable names can be used when you
call the function.
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
You can use the fzero function to find the zero of a function
of a single variable, which is denoted by x. One form of its
syntax is
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 + 2e-x - 3.
There is a
zero near x =
-0.5 and one
near x = 3.
3-38 (continued …)
Example (continued)
>>x = fzero(@f1,-0.5)
3-39
Finding the Minimum of a Function
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
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 (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-57
The term function function is 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
Anonymous Functions
(continued …)
3-59
Anonymous Functions (continued)
>>sq([5,7])
ans =
25 49
3-60 (continued …)
Anonymous Functions (continued)
(continued …)
3-61
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)
3-67
Subfunctions
(continued …)
3-68
Subfunctions (continued)
3-69
Precedence When Calling Functions
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.
3-74
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.
3-80
Private Functions
(continued …)
3-81
Private Functions (continued)
3-82
Importing Spreadsheet Files
3-83
The Import Wizard
To import ASCII data, you must know how the data in the file
is formatted.
For example, many ASCII data files use a fixed (or uniform)
format of rows and columns.
(continued …)
3-84
The Import Wizard (continued)
3-86
The first screen in the Import Wizard.
3-87