Matlab 预定义函数
15
User-defined Functions
• User-defined functions must start with a function definition line. • The definition line contains…
– – – – The word function A variable(variables) that defines the function output A function name A variable(variables) used for the input argument
– Name – Input – Output
A=cos(x)
6
How to call functions?
• The functions can be called directly in the command window, or by any other program or function.
2
本章学习的目标:
• Introduction to Matlab Functions
• built in functions and user defined
functions • Optional Arguments • Sharing Data Using Global Memory • Preserving Data Between Calls to a
14
User-defined Functions • You can use user defined functions in your programs just as you would use MATLAB’s built in functions. • User defined functions help you write more compact programs and write complex programs into shorter ones that are easier to debug and get running.
Function
• Function Functions • Subfunctions
3
Functions
• Functions are more complex than scripts • Functions have their own local variables • Functions return output as specified, and can accept input as specified
– returns vector of same size with cumulative sum of x – i.e. x=[4,2,3] returns [4,6,9]
• cumprod(x)
– returns vector of same size with cumulative products in vector of same size – i.e. x=[4,2,3] returns [4,8,24]
• Isolation from unintended side effects: – Isolation of function variables from the main program variables. – The input and the output variables are specified clearly.
• max(x,y)
– x and y arrays of same size, returns vector of same length with larger value from corresponding positions in x and y >>max([3 5 2],[6 4 8]) ans= 6 5 8
• same type of functions are available for min
11
Builtin Functions for vectors
• • • • • • sort(x) sort in ascending or descending order mean(x) average or mean value median(x) median value sum(x) sum of elements of x prod(x) product of elements of x cumsum(x)
8
Some examples of Built in Functions
• Exponential(指数)
– exp(x) – sqrt(x) exponential of x (ex) square root of x
• Logarithmic(对数)
– log(x) natural logarithm( ln x ) – log10(x) – log2(x)
• Very important: if the input variables are changed in the functions they will not change in the main program (PASS-BYVALUE SCHEME)
7
Built in Functions
19
Comments for Functions
• The comment lines immediately after the first line are returned when you query the help function
• Built in functions allow us to reuse computer code for calculations that are performed frequently. For example, suppose there were no built in sine function in MATLAB and you will do a lot of trigonometric calculations involving the sine of an angle. • We’ve already become familiar with a number of MATLAB built in functions like sin (x) or plot (x,y) for example.
4
Why we use functions?
• Independent Testing of Sub-Tasks: – Each task is an independent unit that can be tested separately (unit testing). • Reusable Code: – Within the same program (the code is not repeated). – It can be used in another program.
17
You can use your user defined functions from the Command Window or from a M-file program.
18
When x is a vector…
• Consider the following, >> A = [1, 2, 5]; % A is a 3-element row vector >> y = 2 * poly(A) y= 14 82 982 • In the statement y = 2* poly(A) above, the user defined function poly is called which evaluates the polynomial at each point in the input row vector. Then, each value is multiplied by 2, resulting in the row vector called y having the values [14, 82, 982].
12
Builtin functions applied to matrices
• Matrices (arrays) are stored in column major form • When builtin functions for vectors are applied to a matrix function operates on columns and returns a row vector
13
Builtin functions applied to matrices
>>sum([0 1 2;3 4 5]) ans= 3 5 7 >>prod([0 1 2;3 4 5]) ans= 0 4 10 >>cumsum([0 1 2;3 4 5]) ans= 0 1 2 3 5 7 >>cumprod([0 1 2;3 4 5]) ans= 0 1 2 0 4 10 >>sort([9 1 2;3 2 4]) ans= 3 1 2 9 2 4
9
Some examples of Built in Functions
• Numeric(数值)
– – – – – – – – – fix(x) round to nearest integer towards 0 round(x) round to nearest integer sign(x) +1, 0 or -1 rem(x,y) Finds remainder of x/y abs(x) angle(x) conj(x) imag(x) real(x) absolute value phase angle in complex plane conjugate of x imaginary part of x real part of x