1 / 19

Introduction to Engineering MATLAB - 12

Introduction to Engineering MATLAB - 12. Agenda Function files. FUNCTION FILE. A function file is a program that can be used in two ways:.

lenoraochoa
Download Presentation

Introduction to Engineering MATLAB - 12

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. Introduction to EngineeringMATLAB - 12 Agenda Function files

  2. FUNCTION FILE A function file is a program that can be used in two ways: • Perform a task frequently. Examples: calculate the value of a math function for different values of the independent variable, performs a series of commands with different values of variables. • Be a subprogram in a large program. In this way a large program can be made of smaller “building blocks” that can be tested independently.

  3. FUNCTION FILE • Function files are used in the same way as built in functions. i.e. once they are created, the functions can be used in the command window, in script files, or inside other function files. • Usually, 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, transferred, or available to other parts of MATLAB. • Function files are like subroutines in FORTRAN and BASIC, procedures in PASCAL, and functions in C.

  4. CREATING A FUNCTION FILE A function file is created in the M-file Editor/Debugger window (the same window that is used to create a script file). In the command window click on the File menu, select New, and then select M-file. Once M-file is selected, the M-file Editor/Debugger window opens.

  5. The M-file Editor/Debugger window The first line of the function file is typed here.

  6. function [output variables] = function_name (input variables) FUNCTION DEFINITION LINE The first line of a function file MUST be the function definition line (without this line it will be a script file). The function definition line: • Defines that the M-file is a function. • Defines the name of the function. • Has the list of input and output variables. When the file is saved, the file name MUST be identical to the function_name (with the .m extension).

  7. EXAMPLES OF FUNCTION DEFINITION LINES Function definition lineFile name function [A] = RectArea(a,b) RectArea.m function[V, S] = SphereVolArea(r) SphereVolArea.m function[d,h] = projectile(v,theta) projectile.m function = CirclePlot(r) CirclePlot.m

  8. FORMAT OF A FUNCTION FILE Function name Output variables Input variables Function definition line function [xout,yout] = functioname(xin,yin) % description of the function % help comments % name of creator, date a = …. b = ….. . . . xout = …… yout = …… Optional comments. Displayed when help functioname is typed In the command window. Body of the function The output variables must be assigned values

  9. Either form is ok. Either form is ok. COMMENTS ABOUT FUNCTION FILES The word function, which is the first word in the function definition line must be typed in lower case letters. 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) function CirclePlot(r)

  10. COMMENTS ABOUT FUNCTION FILES The names of the input and output variables given in the function definition line and in the body of the function are local. This means that other variable names can be used in the function call. Specific numbers, or mathematical expressions can also be used as input variables. (Example on the next slide.) The variables are assigned according to their position in the output or input variables list in the function definition line.

  11. Function definition lineExample of variables when the function is used function [A] = RectArea(a,b) S=RectArea(g,r) T=RectArea(8,25) In the first example g and r must have assigned values before they are used as input variables. When the function is executed, a will have the value of g, b will have the value of r, and the value of A will be assigned to S. In the second example a is assigned the value 8, and b is assigned the value 25.

  12. COMMENTS ABOUT FUNCTION FILES As in the command window and a script file, a semicolon in a function file suppresses output of a command. If a semicolon is not typed, the output is displayed in the command window. This can be useful when debugging.

  13. SAVING A FUNCTION FILE • Once the function file is completed, it must be saved. In our class use Save As and save in the floppy A drive. • 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.

  14. Output variable Function name Input variables Comments Function definition line Value assigned to the output variable EXAMPLE OF A FUNCTION FILE function value = accountvalue(depo,t,rate) % The function calculates the the value of a saving % account in which the interest compounds annually. % The output of the function is the account value. % The input to the function is: % depo: the initial deposit. % t: number of years. % rate: the interest rate in percent. format bank value=depo*(1+rate/100)^t;

  15. EXECUTING THE accountvalue FUNCTION Three examples of executing the accountvalue function in the command window are shown below: >> x = accountvalue(20000,15,6.5) x = 51436.82 >> amount = 20000; >> years = 15; >> intrat = 6.5; >> money = accountvalue(amount,years,intrat) money = 51436.82 >> amount = 20000; >> accountvalue(amount,15,6.5) ans = 51436.82

  16. EXAMPLE OF A FUNCTION FILE function [dmax,hmax] = trajectory(v0,theta) % The function calculates the trajectory of a projectile. % The input to the function is: % v0: the initial velocity (units: m/s) of the projectile. % theta: the angle (units: deg.) at which the projectile is shot. % The output of the function are: % dmax: the distance (units: m) the projective travels. % hmax: the max height (units m) the projectile reaches. % in addition, the function creates a plot of the trajectory. v0x = v0*cos(theta*pi/180); % Horizontal component of initial velocity. v0y = v0*sin(theta*pi/180); % Vertical component of initial velocity. (Continues on the next slide) NOTE: Why use the double quotes in PROJECTILE’s?

  17. EXAMPLE OF A FUNCTION FILE hmax = v0y^2/(2*9.81); % The max height. t = v0*sin(theta*pi/180)/9.81; % Time to highest point. ttotal = 2*t; % Total flying time. dmax = v0x*ttotal; % Max distance traveled. tplot = linspace(0,ttotal,200); % Creating a vector of time. x = v0x*tplot; % x coordinate as a function of time. y = v0y*tplot+0.5*(-9.81)*tplot.^2; % y coordinate as a function of time. plot(x,y) xlabel('DISTANCE') ylabel('HEIGHT') title('PROJECTILE''S TRAJECTORY')

  18. EXECUTING THE trajectory FUNCTION Executing the trajectory function in the command window for: V0 = 250 m/s, and theta = 32 degrees. >> [dist,height]=trajectory(250,32) dist = 5.7263e+003 height = 894.5414

  19. ASSIGNMENT 8: 1. Problem 16 page 162 in the textbook. 2. Problem 17 page 162 in the textbook. 3. Problem 20 page 163 in the textbook. In each problem submit a printout of the function file, and a printout of the command window showing how the function was used. The second line in the function file, and the first line in the command window should be a comment with your name.

More Related