introduction to engineering matlab - 12 agenda function files
TRANSCRIPT
Introduction to EngineeringMATLAB - 12
Agenda Function files
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.
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.
CREATING A FUNCTION FILE
Once M-file is selected, the M-file Editor/Debugger window opens.
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.
The M-file Editor/Debugger window
The first line of thefunction file is typed here.
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:
1. Defines that the M-file is a function.
2. Defines the name of the function.
3. 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).
function [output variables] = function_name (input variables)
EXAMPLES OF FUNCTION DEFINITION LINES
Function definition line File 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
FORMAT OF A FUNCTION FILE
function [xout,yout] = functioname(xin,yin)
% description of the function% help comments% name of creator, date
a = ….b = ….....xout = ……yout = ……
Function definition line
Outputvariables
Inputvariables
Functionname
Optional comments.Displayed when helpfunctioname is typed Inthe command window.
Body of the function
The outputvariables mustbe assigned values
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)
Either form is ok.
Either form is ok.
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.
Function definition line Example of variables whenthe 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.
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.
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.
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;
Function definition line
Outputvariable
Functionname
Inputvariables
Comments
Value assigned to the output variable
>> 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
EXECUTING THE accountvalue FUNCTION
Three examples of executing the accountvalue function in the command window are shown below:
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?
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')
EXAMPLE OF A FUNCTION FILE
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
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.