1 / 30

Computer Programming (ECGD2102 ) Using MATLAB

Computer Programming (ECGD2102 ) Using MATLAB. Lecture (7): Functions (Chapter 3) Functions. Instructor: Eng. Eman Al.Swaity. Objectives ( CHAPTER (3):Functions). At the end of this lesson, you will be able to understand: The function use.

Download Presentation

Computer Programming (ECGD2102 ) Using MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Programming(ECGD2102 ) Using MATLAB Lecture (7): Functions (Chapter 3) Functions Instructor: Eng. Eman Al.Swaity

  2. Objectives (CHAPTER (3):Functions) At the end of this lesson, you will be able to understand: • The function use. • Structure , input and output argument of a function. • The scope of variables-Local and global. • Return statement. • The use of nargin and nargout. • The concept of recursive functions.

  3. Definition • A user-defined function is a MATLAB program that is created by the user, saved as a function file, and then can be used like a built-in function. • A function file can be illustrated by:

  4. CREATING A FUNCTION FILE • Function files are created and edited, like script files, in the Editor/Debugger Window. • The first line in a function file must be the function definition line as discussed in the next section. • Function file is not executed like M-file using run command, instead it need to be called from the command window or the function it self.

  5. 3.1 General Structure of a Function

  6. 3.1 General Structure of a Function Important Note: The M-file name must have the same function name to be called using it.

  7. 3.1 General Structure of a Function

  8. 3.1 General Structure of a Function

  9. 3.1 General Structure of a Function • SAVING A FUNCTION FILE • A function file must be saved before it can be used. • It is highly recommended that the file is saved with a name that is identical to the function name in the function definition line. In this way the function is called (used) by using the function name • If a function file is saved with a different name, the name it is saved under must be used when the function is called Examples:

  10. 3.1 General Structure of a Function • USINGA FUNCTION FILE • A user-defined function is used in the same way as a built-in function. • The function can be called from the Command Window, from a script file, or from another function. • To use the function file, the directory where it was saved must either be in the current directory or be in the search path

  11. 3.1 General Structure of a Function function is used with numbers as input arguments: function is used with two pre-assigned variables and a number as the input arguments

  12. 3.1 General Structure of a Function • Input and Output Arguments • The input and output arguments are used to transfer data into and out of the function. • The input arguments are listed inside parentheses following the function name. • The output arguments, which are listed inside brackets on the left side of the assignment operator in the function definition line, transfer the output from the function file. • Function files can have none, one, or several output arguments. If there are more than one, the output arguments are separated with commas. • A function without an output argument can, for example, generate a plot or print data to a file.

  13. 3.1 General Structure of a Function Examples

  14. 3.1 General Structure of a Function Examples: Resistors in parallel

  15. 3.1 General Structure of a Function Examples: Resistors in parallel-cont

  16. 3.2 Scope of Variables A variable's scope is where in a program the variable is defined. Suppose we have this function: This function uses variables a, b, c, and z inside the function.

  17. 3.2 Scope of Variables Now let's use this function inside the following program: When we run the program, we see this dialog:

  18. 3.2 Scope of Variables As another example, let's define function example2 as follows: This function never sets the values of a, b, and c inside the function. However, it does attempt to print out their values. We will call this function with the following code segment: ( 5 )

  19. 3.2 Scope of Variables When we run the code segment, this output is generated: In the same way the variables defined inside a function are not known to the main program

  20. 3.4 Global Variables • The variables are not shared between the code segment and function; that is, they are treated as completely different variables by MATLAB. • Local variables are desirable because in different functions, variables with the same name are independent. • Local variables are desirable because we use many common variables like i, j, and x frequently. It would be confusing if every function needed unique variable names. • On occasion, it is desirable for a variable to be used in the same way in more than one function. • A variable of this type is called a global variable because it's defined in more than one function.

  21. 3.4 Global Variables Example: Function xxx2 defines variable a as GLOBAL but never sets its value. This will be done in the code segment. Both the code segment and the function must define a as GLOBAL: When we run the code segment, we see this output:

  22. 3.4 Global Variables

  23. 3.5 The RETURN Statement • Normally, a function returns to its caller after it executes the last line of the function. • However, a function can be made to return to its caller at any point by using the MATLAB RETURN statement • Example:

  24. 3.6 nargin and nargout • A good example is the MATLAB R ESIDUE function • If you read the help information for the function, you will see that the function can be used in two forms: In the first case, the function has two input arguments and three output variables. In the second case, it has three input arguments and two output variables. The same function handles both cases. The question is, how does the function know how to handle both cases? It must know the number of input and output arguments inside the function.

  25. 3.6 nargin and nargout • The MATLAB variables nargin and nargout contain this information.

  26. 3.7 Recursive Functions • A recursive function is a function that calls itself. A classic example is the factorial function. The process stops when we reach a known value of the factorial

  27. COMPARISONBETWEENSCRIPT FILES AND FUNCTION FILES

  28. Exercise 1

  29. Exercise 2

  30. End of the Lecture Let Learning Continue

More Related