Chapter 3
Chapter 3
Chapter 3
Functions
1
Getting Help for Functions
2
Common mathematical functions
Exponential
exp(x) Exponential; e to the x power
3
Some common mathematical functions 1
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.
4
Some common mathematical functions 2
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; if x = 0; −1 if x < 0.
5
Operations with Complex Numbers 1
>>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
6
Operations with Complex Numbers 2
>>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
7
Operations on Arrays
>>x = [5,7,15];
>>y = sqrt(x)
y =
8
Expressing Function Arguments 1
9
Expressing Function Arguments 2
11
Expressing Function Arguments 4
12
Trigonometric Functions
13
Inverse Trigonometric Functions
14
Hyperbolic Functions
15
Inverse Hyperbolic Functions
16
User-Defined Functions 1
17
User-Defined Functions 2
18
The default Editor Window when creating a new
function 1
Source: MATLAB 19
User-Defined Functions: Example 1
function z = fun(x,y)
u = 3*x;
z = u + 6*y.^2;
end
20
User-Defined Functions: Example 2
21
User-Defined Functions: Example 3
>>fun(3,7)
ans =
303
>>z
??? Undefined function or variable ‘z’.
22
User-Defined Functions: Example 4
>>q = fun(3,7)
q =
303
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’.
24
Argument Order
25
Arrays as Inputs
26
Multiple Outputs
27
Circle Example
>>[A, C] = circle(4)
A =
50.2655
C =
25.1327
28
Example of No Inputs and No Outputs
function show_date
clear
clc
today = date
end
29
Examples of Function Definition Lines
30
Function Example 1
31
Function Example 2
32
Function Example 3
33
Local Variables 2
34
Global Variables
35
Function Handles 1
36
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)
where @function is the function handle for the function
function, and x0 is a user-supplied guess for the zero.
The fzero function returns a value of x that is near x0.
37
Using fzero with User-Defined Functions 1
38
Using fzero with User-Defined Functions 2
39
Finding the Minimum of a Function 1
40
Finding the Minimum of a Function 2
y = 1-x.*exp(-x);
end
>>x = fminbnd(@f2,0,5)
If the specified range of the independent variable does not enclose the
global minimum, fminbnd will not find the global minimum.
fplot(@f7, [0 4]);
fzero(@f7, 1)
fminbnd(@f7,0,4)
42
Finding the Minimum of a Function 5
− x2 − y 2
To minimize the function f = xe , 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);
end
Suppose we guess that the minimum is near x = y = 0. The session is
>>fminsearch(@f4,[0,0])
ans =
-0.7071 0.000
Thus the minimum occurs at x = −0.7071, y = 0.
43
Function Handles 2
44
Anonymous Functions 1
45
Anonymous Functions 2
47
Anonymous Functions 4
√ ( x + y ) , type
2 2
50
Calling One Function within Another
One anonymous function can call another to implement
function composition. Consider the function 5 sin ( x3 ) . It is
composed of the functions g ( y ) = 5 sin ( y ) and f ( x ) = x 3 . In
the following session the function whose handle is h calls the
functions whose handles are f and g.
51
Variables and Anonymous Functions 1
52
Variables and Anonymous Functions 2
53
The default Editor Window when creating a new
function 2
Source: MATLAB 54
Importing Spreadsheet Files
55