Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.

Similar presentations


Presentation on theme: "ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool."— Presentation transcript:

1 ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool User Function Library 2.Function Syntax User Defined Functions / Chapter 6

2 ENG 1181 2 Why Not Use Script Files For Everything? Imagine that you’re heading a programming group supervising 10 programmers. Imagine that you have to allocate tasks to create a new application program. Imagine that everything is done with script files. What would you have to do? Create a table of all variable names to be used in every part of the program

3 ENG 1181 3 Why Not Use Script Files For Everything? Imagine you created a useful set of code two years ago and would like to use it again for a new program Wouldn’t it be nice to not have to remember exactly what you did? If back then you used a variable name you’d like to use for something else now – wouldn’t it be nice to not have to find all cases and change it?

4 ENG 1181 4 Consider the built in functions like the sin(x) function Do you have to know every variable name used by the person who programmed it? Wouldn’t it be nice to do the same with your own programming? Function files help solve these problems. Why Not Use Script Files For Everything?

5 ENG 1181 5 Function Files Every programming language has a way to do this. In MATLAB it’s called a user-defined function In FORTRAN and BASIC it’s called a subroutine In PASCAL it’s called a procedure In C it’s also called a function

6 ENG 1181 6 A USER-DEFINED FUNCTION IN A FUNCTION FILE 141- 142 Function File Input dataOutput  A user-defined function is a program created by the user and can be used like a built-in function.  Its main feature is that it has an input and an output.  The function can be used many times with different values of the input data.

7 ENG 1181 7 FUNCTION FILE A function file is a program that can be used in two ways:  Perform tasks that are used frequently  Be a subprogram in a large program In this way a large program can be made of smaller “building blocks” that can be developed independently. 141- 142

8 ENG 1181 8 Programming Insight In practice, only the top level of a program is a script file. The script file interacts with the user and keeps track of calling the other routines (like a project manager). ALL other routines are functions. Because of this, function files never use the input command

9 ENG 1181 9 Programming Insight User created function files can either be in the current working directory or be placed into a user library whose location has been added to MATLAB’s search path. Add a folder to MATLAB’s search path by going to File > Set Path Click on the “Add a Folder” button and indicate the folder you want to add Don’t forget to “Save” if you want this folder used in future MATLAB sessions!

10 ENG 1181 10 FUNCTION FILE  Once they are created, the functions can be used in the Command Window, in script files, or inside other function files  Data is transferred to a function file by input variables and the results are transferred back by output variables.  All variables inside the function are local (NOT global), (they are not recognized by other parts of MATLAB – e.g. the command window or any script file) 141- 142

11 ENG 1181 11 FUNCTION FILE  User-defined Function files are used in the same way as built-in functions, (i.e. once they are created, functions can be used in the Command Window, in script files, in equations, or inside other function files)  Data is transferred to a function file by input variables and the results are transferred back by output variables.  All the calculations and the variables that are used inside the function are local, (i.e. they are not recognized by, transferred to, or available to other parts of MATLAB) 141- 142

12 ENG 1181 12 CREATING A FUNCTION FILE A function file is created in the Editor/Debugger Window (the same window that is used to create script files). 142 The first line of the function file is typed here. It must be the function definition line.

13 ENG 1181 13 STRUCTURE OF A FUNCTION FILE 143 function [mPay, tPay] = loan(amount, rate, years) % loan() calculates monthly and total payment of loan % Input arguments: % amount = loan amount in $ % rate = annual interest rate in percent % years = number of years % Output arguments: % mPay = monthly payment % tPay = total payment ratePerMonth = rate * 0.01 / 12; rPM = (1 + ratePerMonth) ^ (years * 12); mPay = amount * rPM * ratePerMonth / (rPM - 1); tPay = mPay * years * 12; Function definition line Help text Function body (Program) Assign values to output arguments Output arguments Input arguments

14 ENG 1181 14 FUNCTION DEFINITION LINE The function definition line: 1.Defines that the M-file is a function 2.Defines the name of the function 3.Lists the input and output variables function [output vars] = function_name(input vars) 143- 144 function [mpay, tpay] = loan(amount, rate, years) The first line of a function file MUST be the function definition line (without this line it will be a script file) Example:

15 ENG 1181 15 THE FUNCTION DEFINITION LINE  The word function must be typed in lower case letters (if it is wrong, it won’t turn blue)  Square brackets are not required in the function definition line if the function has only one output variable: function [A] = RectArea(a,b) function A = RectArea(a,b)  If there are no output variables, the square brackets and the equal sign can be omitted: function CirclePlot(r) Either form is ok 143- 146

16 ENG 1181 16  The names of the input and output variables given in the function definition line and in the body of the function are local.  The variables are assigned according to their position in the output or input variables list in the function definition line.  Do not name a function file a name that is already used by MATLAB for a built-in function. To check if a function name is used by MATLAB type “help name” in the Command Window. 143- 146 THE FUNCTION DEFINITION LINE

17 ENG 1181 17 FUNCTION EXAMPLE w = 5; h = 10; a = RectArea(w, h);... Memory (main) w h a 5 function area = RectArea(width, height) area = width * height; Memory (local function) width height area 5 10 50 10 Call a function to calculate the area of a rectangle with sides w and h. Script file

18 ENG 1181 18 SAVING A FUNCTION FILE (similar to saving a script file)  Once the script file is completed, it must be saved. In our class use Save As and save: (N:\, USB drive). Save it in the working directory.  The file MUST be saved with a name that is identical to the function name in the function definition line  “Save and Run” will not work, since the function needs to be called from somewhere with input values.  It is common to forget to save changes, in which case MATLAB will run the OLD version. 147

19 ENG 1181 19 EXAMPLES OF FUNCTION DEFINITION LINES 145 Three input variables, two output variables, save as loan.m Two input variables, one output variable, save as RectArea.m One input variable, two output variables, save as SphereVolArea.m Three input variables, no output variables, save as trajectory.m function [mpay, tpay] = loan(amount, rate, years) function trajectory(velocity, height, gravity) function [volume, surfArea] = SphereVolArea(radius) function [area] = RectArea(width, height)

20 ENG 1181 20 USING A USER-DEFINED-FUNCTION (FUNCTION FILE)  A user-defined function is used in the same way as a built-in function.  To use a function file, the directory where it was saved must either be in the current directory, or be in the search path (same as required for a script file).  A variable that is assigned a value in the code of the function file will be displayed in the Command Window, unless a semicolon is typed at the end of the command (always use semicolons in functions) 148

21 ENG 1181 21 EXAMPLE OF A FUNCTION FILE 143 function [mPay, tPay] = loan(amount, rate, years) % loan() calculates monthly and total payment of loan % Input arguments: % amount = loan amount in $ % rate = annual interest rate in percent % years = number of years % Output arguments: % mPay = monthly payment % tPay = total payment ratePerMonth = rate * 0.01 / 12; rPM = (1 + ratePerMonth) ^ (years * 12); mPay = amount * rPM * ratePerMonth / (rPM - 1); tPay = mPay * years * 12; Function definition line Help text Function body (Program) Assign values to output arguments Input arguments Output arguments

22 ENG 1181 22 >> [month total] = loan(25000, 7.5, 4) month = 600.72 total = 28834.47 >> a = 70000; b = 6.5; >> [x y] = loan(a, b, 30) x = 440.06 y = 158423.02 EXECUTING THE loan() FUNCTION 148 First input argument is loan amount Second is interest rate Third is the number of years Define variables a and b. Use a, b, and the number 30 for input arguments and x (monthly pay) and y (total pay) for output arguments.


Download ppt "ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool."

Similar presentations


Ads by Google