Title: MATLAB CHAPTER 3 Functions and Files
1 MATLAB ??CHAPTER 3 Functions and Files
2Elementary Mathematical Functions
- lookfor find functions that are relevant to
your application - help when you know the correct spelling of the
function
3Exponential and Logarithmic Functions
- Some common mathematical functions
4Complex Number Functions(1/2)
- rectangular representation
- a ib
- polar representation
- .
- abs(x), angle(x)
- absolute value (magnitude)
- angle
- conj(x)
- complex conjugate
5Complex Number Functions(2/2)
6Numeric Functions
- MATLAB has been optimized to deal with arrays.
- round(y) rounds to the nearest integer ? ans
2, 3, 4 - fix(y) truncates to the nearest integer toward
zero ? ans 2, 2, 3 - ceil(y) rounds to the nearest integer toward 8
? ans 3, 3, 4 - e.g. z -2.6 , -2.3 , 5.7
- floor(z) rounds to the nearest integer toward
- 8 ? ans -3, -3, 5 - fix(z) -2, -2, 5
- abs(z) 2.6 , 2.3, 5.7
7Trigonometric Functions
- Table 3.1-2
- Trigonometric functions
8Hyperbolic Functions(1/2)
9Hyperbolic Functions(2/2)
10User-Defined Functions(1/4)
- Function file when need to repeat a set of
commands several times. - variables local
- syntax
- function output variables function_name
(input variables) - function_name saved file name ( with the .m
extension) - ? function_name drop file name drop.m
- e.g.
11User-Defined Functions(2/4)
12User-Defined Functions(3/4)
- the order of arguments is important, not the
names of the arguments - use arrays as input arguments
13User-Defined Functions(4/4)
14Variations in the Function Line
15Local Variables, Global Variables
- Local variables
- variables created by a function file are local to
that function. - local their values are not available outside
the function. - Global variables
- their values are available to the basic workspace
and to other functions that declare these
variables global. - variables in script file global
16Minimization and root-finding functions
- Table 3.2-1 Minimization and root-finding
functions
17Advanced Function Programming
- Function Handles
- using the at sign _at_
- e.g.
- gtgt sine_handle _at_sin
- gtgt plot(00.016, sine_handle, 0 0.01 6)
- function x gen_plot(fun_handle, interval)
- plot(interval, fun_handle, interval)
- gtgtgen_plot(sine_handle, 0 0.01 6) or
- gtgtgen_plot(_at_sin, 0 0.01 6)
- advantages
- speed of execution and providing access to
subfunctions. - a standard MATLAB data type, and thus can be used
in the same manner as other data types.
18Methods for Calling Functions
- four ways to invoke, or call, a function into
action. - 1. As a character string identifying the
appropriate function M-file. - function y fun1(x)
- y x.2-4
- gtgtx, value fzero(fun1,0,3)
- 2. As a function handle.
- gtgtx, value fzero(_at_fun1,0,3)
- 3. As an inline function object, or
- gtgtfun1 x.2-4
- gtgtfun_inline inline(fun1)
- gtgtx, value fzero(fun_inline, 0,3)
- 4. As a string expression.
- gtgtfun1 x.2-4
- gtgtx, value fzero(fun1, 0,3) or as
- gtgtx, value fzero(x.2-4, 0,3)
19Types of Functions(1/2)
- primary functions
- contains the main program.
- the only function that you can call from the
MATLAB command line or from another M-file
function - anonymous functions
- create a simple function without needing to
create an M-file for it. - provide a quick way of making a function from any
MATLAB expression - subfunctions
- placed in the primary function
- use multiple functions within a single primary
function M-file
20Types of Functions(2/2)
- nested functions
- defined within another function
- help to improve the readability of your program
- difference between nested functions and
subfuntions subfunctions normally cannot be
accessed outside of their primary function fle - overloaded functions
- functions that respond differently to different
types of input arguments. - created to treat integer inputs differently than
inputs of class double. - private functions
- restrict access to a function
- called only from an M-file function in the parent
directory
21Anonymous Functions
- create a simple function without needing to
create an M-file for it. - MATLAB command line
- from within another function or script
- syntax
- fhandle _at_(arglist) expr
- arglist a comma-separated list of input
arguments to be passed to the function - expr any single, valid MATLAB expression
- e.g.
- sq _at_(x) x.2
- gtgtsq(5)
- gtgtans 25
- gtgtsq(5,7)
- gtgtans 25 49
- be useful for more complicated functions
involving numerous keystrokes.
22Variables and Anonymous Functions
- variables can appear in anonymous functions in
two ways - as variables specified in the argument list
- e.g.
- f _at_(x) x.3
- as variables specified in the body of the
expression - e.g.
- plane _at_(x, y) Ax By
23Subfunctions(1/2)
- all other functions in the primary function are
called subfunctions. - the order for checking functions in MATLAB
- 1. checks to see if the function is a built-in
function such s sin. - 2. checks to see if the function is subfunction
in the file. - 3. checks to see if the function is private
function. - may use subfunctions with the same name as
another existing M-file. - allow you to name subfunctions without being
concerned about whether another function exists
with the same name - protects you from using another function
unintentionally
24Subfunctions(2/2)
- e.g.
- function y subfun_demo(a)
- y a mean(a)
-
- function w mean(x)
- w sqrt(sum(x.2))/length(x)
- a sample session follows.
- gtgt y subfun_demo(4,-4)
- y
- 1.1716 -6.8284
- if had used the MATLAB M-function mean ?
different answer - gtgt a 4, -4
- gtgt b a mean(a)
- b
- 4 -4
25Nested Functions(1/2)
- Functions that are defined within the main
function. - contains the usual components of an M-file
function. - must always terminate with an end statement.
- e.g.
- function f parabola(a, b, c)
- f _at_p
- function y p(x)
- y ax2 bx c
- end
- end
- In the command window type
- gtgtf parabola(4, -50, 5)
- gtgtfminbnd(f, -10, 10)
- ans
- 6.2500
26Nested Functions(2/2)
- two unique properties
- 1. can access the workspace of all functions
inside of which it is nested. - 2. function handle
- stores the information needed to access the
nested function - stores the values of all variables shared between
the nested function and those functions that
contain it - call a nested function
- 1. from the level immediately above it
- 2. from a function nested at the same level
within the same parent function - 3. from a function at any lower level
27Private Functions
- reside in subdirectories with the special name
private. - visible only to functions in the parent
directory. - invisible outside the parent directory.
- MATLAB looks for private functions before
standard M-file functions.
28Working with Data Files
- header a comment that describe what the data
represent, the date it was created, and who
created the date. - Importing data bring data created by other
applications into the MATLAB workspace. - Exporting data package workspace variables so
that they can be used by other applications. - Importing Wizard a graphical user interface
29Importing Data from Externally Generated Files
- ASCII file format
- if the file has a header or the data is separated
by commas, MATLAB will produce an error message. - to correct
- load the data file into a text editor
- remove header
- replace the commas with spaces
- type load filename to retrieve the data into
MATLAB - e.g. if the data file name is force.dat ? type
load force.dat - Importing Spreadsheet Files
- file format .wk1
- command M wk1read(filename)
- Microsoft Excel workbook file .xls
- A xlsread(filename)
30The Import Wizard(1/5)
- To import ASCII data
- How many data items are in each row?
- Are the data items numeric, text strings, or a
mixture of both types? - Does each row or column have a descriptive text
header? - What character is used as the delimiter, that is,
the character used to separate the data items in
each row? The delimiter is also called the column
separator. - The data format will usually fall into one of the
following categories - 1.Space-delimited ASCII data files,
- 2.Mixed text and numeric ASCII data files,
- 3.ASCII data files with text headers, or
- 4.ASCII data files with nonspace delimiters
(usually semicolons).
31The Import Wizard(2/5)
- The import Wizard present a series of dialog
boxes in which you - 1. Specify the name of the file you want to
import, - 2. Specify the delimiter used in the file, and
- 3. Select the variables that you want to import
- Note
- when you use the Import Wizard to create a
variable in the MATLAB workspace, it overwrites
any existing variable in the workspace with the
same name without issuing a warning.
32The Import Wizard(3/5)
33The Import Wizard(4/5)
34The Import Wizard(5/5)