Functions MATLAB Functions n There are thousands of
Functions
MATLAB Functions n There are thousands of functions defined by MATLAB that can be directly used q q n You can use lookfor command to search for a function, or You can use help/doc to read about a specific function You can also define your own function (user defined functions)
MATLAB functions n Exponential and Logarithmic functions q Exponential n n n q exp(x): ex sqrt(x): square root nthroot(x, n): nth root (n is an odd number) Logarithmic n n n log(x): ln(x) log 10(x): log 10(x) log 2(x): log 2(x)
MATLAB functions n Complex q q q abs(x): magnitude of x angle(x): angle of x conj(x): x conjugate Imag(x): imaginary part real(x): real part
MATLAB functions
MATLAB functions n Numeric q q q ceil(x): rounds to the nearest integer grater than x fix(x): removes the fractional part of x floor(x): rounds to the nearest integer smaller than x round(x): rounds to the nearest integer sign(x): +1 if x > 0; 0 if x = 0; -1 if x < 0 mod(x, y): returns the remainder of dividing x by y
MATLAB functions
MATLAB functions n Trigonometric (Inverse trigonometric) q q q sin(x) (asin(x)) cos(x) (acos(x)) cot(x) (acot(x)) tan(x) (atan(x)) sec(x) (asec(x)) csc(x) (acsc(x))
User defined functions n n Are defined in a. m script file. The first line of code of the function must start be: q n function [output variables] = function_name(input variables) The function name should be the same as the file name
Why functions n n n Application development is easier Application maintenance is easier Application enhancement is easier Application documentation is easier Facilitates team work
Example n n Write a MATLAB script the reads in the information of three cylinders and computes the averages of their volumes and their surface areas Modify the above program using a function called Area. Volume that computes the surface area and the volume for a given cylinder: q q The function accepts two parameters: the radius r and the height h of a cylinder The function returns both, the surface area and the volume of the cylinder
Example
Example
Example n Write a MATLAB function that accepts the temperature in degree F/C and computes the corresponding value in degree C/F. The relation is defined as: C = (5/9)(F – 32).
Example n Write a MATLAB function called max_mod that computes the maximum number on a matrix and its column and row.
Some useful MATLAB functions n fzero(`fname`, x 0): It is used to find the points where the function cross the x-axis q q n fname: a function name or an explicit function (ex. 3*x^3 – 2*X + 1) x 0: a user supplied guess for the zero Other forms: q q q [x, fval] = fzero(`function`, x 0) [x, fval, flag] = fzero(……) n flag >0 means a zero was found n flag < 0 means no zero was found … = fzero(`function`, [x 0 x 1]); n f(x 0)*f(x 1) < 0
fzero
fzero
fzero
fzero
fzero
fzero
fminbnd function n fminbnd(`function`, x 1, x 2): finds the minimum of a function of a single variable q n It returns a value of x that minimizes the function in the interval x 1 ≤ x 2. Other forms: q q [x, fval] = fminbnd(`function`, x 0, x 1) [x, fval, flag] = fminbnd(……) n n flag >0 means a zero was found flag < 0 means no zero was found
fminbnd
fminbnd
fminbnd
- Slides: 26