Applications of User Defined functions in MATLAB EE
Applications of User Defined functions in MATLAB EE 201 C 5 -2 FALL 2012 1
Session Agenda o Contact before work 5 min. o Applications of user defined functions in MATLAB. 70 min. C 5 -2 FAll 2012 2
Class Learning Objectives o Achieve Comprehension LOL of how to use functions in real engineering applications using Matlab. o Demonstrate a willingness to respond to using functions in real engineering applications using Matlab. C 5 -2 FAll 2012 3
Contact before work o. Burning Questions C 5 -2 FAll 2012 4
Applications of User. Defined Functions o Some Matlab commands act on functions. o If the function of interest is no a simple function, it is more convenient to define the function in an M-file when using one of these commands. n Finding the Zeros of a Function The Fzero function n Minimizing a Function of One Variable The fminbnd function n Minimizing a Function of Several Variables The fminsearch function. C 5 -2 FAll 2012 5
Finding Zeros of a Function o You can use the fzero function to find the zero of a function of a single variable, which is denoted by x. o One form of its syntax is fzero(’function’, x 0) where function is a string containing the name of the function, and x 0 is a user-supplied guess for the zero. o The fzero function returns a value of x that is near x 0. o It identifies only points where the function crosses the x-axis, not points where the function just touches the axis. o For example, fzero(’cos’, 2)returns the value 1. 5708. C 5 -2 FAll 2012 6
Using fzero with User. Defined Functions (continued …) o To use the fzero function to find the zeros of more complicated functions, it is more convenient to define the function in a function file. o For example, if : y =x +2 e−x− 3. o define the following function file: function y = f 1(x) y = x + 2*exp(-x) -3; 7 end C 5 -2 FAll 2012
Plot of the function −x y =x +2 e − 3. o There is a zero near x = -0. 5 and one near x = 3. o (continued …) C 5 -2 FAll 2012 Figure 3. 2– 1 8
Example (continued) o To find a more precise value of the zero near x = -0. 5, type: >>x = fzero(‘f 1’, -0. 5) The answer is x = -0. 5881. o More? See pages 156 -157 C 5 -2 FAll 2012 9
Finding the Minimum of a Function o The fminbnd function finds the minimum of a function of a single variable, which is denoted by x. o One form of its syntax is fminbnd(’function’, x 1, x 2) o where function is a string containing the name of the function. o The fminbnd function returns a value of x that minimizes the function in the interval x 1 ≤x ≤x 2. o For example, fminbnd(’cos’, 0, 4) returns the value 3. 1416. C 5 -2 FAll 2012 10
When using fminbnd it is more convenient to define the function in a function file. o For example, if y =1 −xe−x, define the following function file: function y = f 2(x) y = 1 -x. *exp(-x); o To find the value of x that gives a minimum of y for 0 ≤x ≤ 5, type >> x = fminbnd(’f 2’, 0, 5) The answer is x = 1. o To find the minimum value of y, type >> y = f 2(x). The result is y = 0. 6321. C 5 -2 FAll 2012 11
Other forms of fminbnd function o To find the maximum of a function , use the fminbnd function with the negative of the function of interest. o For example, to find the maximum of y =x e-x o over the interval 0 ≤ x ≤ 5, you must define the function file as follows: function y = f 3 (x) y = -x. *exp(-x); C 5 -2 FAll 2012 12
o A function can have one or more local minima and a global minimum. o If the specified range of the independent variable does not enclose the global minimum, fminbnd will not find the global minimum. o Fminbnd will find a minimum that occurs on a boundary. C 5 -2 FAll 2012 13
Plot of the function y = 0. 025 x 5− 0. 0625 x 4− 0. 333 x 3+x 2. o This function has one local and one global minimum. On the interval [1, 4] the minimum is at the boundary, x = 2. 8236 C 5 -2 FAll 2012 Figure 3. 2– 2 14
The fminsearch function o To find the minimum of a function of more than one variable, use the fminsearch function. One form of its syntax is: fminsearch(’function’, x 0) where function is a string containing the name of the function. The vector x 0 is a guess that must be supplied by the user. C 5 -2 FAll 2012 15
To minimize the function 2−y 2 −x f = xe o we first define it in an M-file, using the vector x whose elements are z(1) = x and z(2) = y. function f = f 4(z) f = z(1). *exp(-z(1). ^2 -z(2). ^2); Suppose we guess that the minimum is near x = y = 0. The session is >>fminsearch(’f 4’, [0, 0]) ans=-0. 7071 0. 000 Thus the minimum occurs at x = − 0. 7071, y = 0. C 5 -2 FAll 2012 16
- Slides: 16