Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.

Similar presentations


Presentation on theme: "ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions."— Presentation transcript:

1 ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions Anonymous Functions 2.Use of function main 3.Nested Functions More about Functions / Chapter 6

2 ENG 1181 2 Script File Limitations  Only one line functions can be included inside a script file  There are two kinds of one line functions o Inline functions o Anonymous functions

3 ENG 1181 3 Inline functions  Older format, may become obsolete name =inline('math expression as a string') or name =inline('math expression as a string','arg1','arg2') % where order of arguments is specified, default is % alphabetical order For example, MofInertia=inline('b*h^3/12', 'h', 'b') % not alphabetical h=3; b=5; I=MofInertia(h,b); % define function before using it 169- 170

4 ENG 1181 4 Inline functions  Any letter except i and j may be used for independent variables  The one line math expression can use any built-in or user-defined functions  Include dot operators if the input variables can be arrays  The expression CANNOT use predefined variables, e.g. a=3; b=5; Royal_Mess=inline('sqrt(a*x.^2+b*y.^2) ') 169- 170

5 ENG 1181 5 Anonymous functions  Newer format, name becomes function handle (points to where function is stored in computer) name = @ (arglist) math_expression For example, MofInertia=@ (h,b) b*h^3/12 h=3; b=5; I=MofInertia(h,b);  The anonymous function needs to be defined before it is used 166- 168

6 ENG 1181 6 Anonymous functions  The one line math expression can use any built-in or user-defined functions  Include dot operators when using arrays for input  The math expression CAN use predefined variables, a=3; b=5; Royal_Mess=@ (x,y) sqrt(a*x.^2+b*y.^2) However!, the predefined variables must be defined before the anonymous function is defined – NOT when it is used. New values for the predefined variables mean redefining the function!!! 169- 170

7 ENG 1181 7 MATLAB will not allow a multi-line function to be included at the end of a script file! Sending a directory of files to a user can be confusing for them and a pain to e-mail Wouldn’t it be nice to just use one file? What if you want everything in one file?

8 ENG 1181 8 Function Main MATLAB, however, does allow the user to place a function inside another function. Thus if you add the line function main to the top of your script file, i.e. a function with no input variables and no output variables, then you can add multi-line user defined functions to the file. This top level still acts like and should be used as a script file! When you do this, MATLAB needs to know when the main routine ends. This is done with an end statement 176

9 ENG 1181 9 Function Main Example function main % script file commands... end function [output] = name_1(input)... end function [output] = name_2(input)... end 176

10 ENG 1181 10 Programming Insight You can now use save and run. This will run the top level script file (function main) and call each included function when that line of the script file is reached. ALL functions in a file will only be understood inside that file, not in other files. Because of this, general purpose functions should be as separate files in a user library Include user-defined functions AFTER the end statement for “function main”, not before!

11 ENG 1181 11 Nested Functions  Functions can be placed inside other functions (before the end statement). These are called Nested functions. Example: Function out = nameA (input) Function out2 = nameB(input2) … end 178

12 ENG 1181 12 Nested Functions  Nested Functions share variable names and values, just like between script files but with limitations. This can cause problems if you do not want such interactions!  For example, a variable defined in a primary function is recognized and can be redefined by any functions contained within it (before the end statement for the primary function) 178

13 ENG 1181 13 Nested Functions In Summary:  Place user-defined functions separately, each after the end statement of the previous one if you want the functions to behave independently and not share variable names and storage  Nest functions if you do want interaction (more similar to separate script files – see the book and help menus for more details) 178

14 ENG 1181 14 159- 160 ASSIGNMENT : MAT #10 per course homework packet


Download ppt "ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions."

Similar presentations


Ads by Google