Tuesday, March 21, 2017

                                                      Chapter-5
                                    FUNCTIONS AND FUNCTION FILES
When a function needs to be evaluated many times for different values of arguments it is easy to create a “user-defined” function. Once the user-defined function is created(saved) it can be used just like built-in functions.


The main feature of a function file is that it has an input and an output. Function files can be used as subprograms in large programs.
CREATING A FUNCTION FILE-
For opening new function file, In the file menu, select new and then select function.
Here is an example of function file that takes five scalars as input variable and returns the variable having maximum value as output.
FUNCTION DEFINITION LINE



  • ·         The first executable line must be the function definition line.
  • ·         It defines the name of the function.
  • ·         It defines the input and output arguments
  • ·         The function name can be made up of letters, digits, and the underscore character. While naming function it is good practice to avoid names of built-in functions and names of variables already defined by the user or predefined by MATLAB.

·         There is at least one input argument although it is possible to have a function without a single input arguments. If there are more than one, the input arguments are separated with commas.
·         Input arguments can be scalars or vectors, it means that the mathematical expression in the function file must be written according to the dimensions of arguments.
·         The actual values of input arguments are assigned when the function is used(called).

 OUTPUT ARGUMENTS:
·         Function files can have none, one, or several output arguments. In order for the function file to work, values should be assigned to the output arguments within the commands that are in function body.
·         When a function does not have an output argument, the assignment operator in the function definition line can be omitted. A function file that is without an output argument can, for example, generate plot or print data.
The following are examples of function definition lines of function files with different number of input and output arguments:
         function [mpay,tpay] = loan(amount,rate,years)
         function [A] = CircleArea(r)
         function A = CircleArea(r)
    function trajectory(v,h,g) %Three input arguments and no output argument

From above examples we can easily conclude that only one output argument can be typed without brackets.

H1 LINE AND HELP TEXT LINES:
H1 line and help text lines are comment lines (lines that begin with % percent sign) following the function definition line. They are optional.
·         The first line in comment lines is called H1 line and it usually contains the name and a short definition of the function.
·         When a user types (in the command window) lookfor a_word, MATLAB searches for a_word in the H1 lines of all the functions, and if a match is found, the H1 line that contains the match is displayed.
·          The help text lines are explanation of function and are displayed when the user types help function_name in the command window. This is true for MATLAB built-in functions as well as user defined functions.


SAVING A FUNCTION FILE:
A function file must be saved by clicking on save button before it can be used. It is highly recommended to save the file with a name that is identical to the function name in the function definition line.
Function files are saved with extension .m.
EXAMPLE:

FUNCTION DEFINITION LINE
FILE NAME
function [A] = CircleArea(a,b)
CircleArea.m
function trajectory(v,h,g)
trajectory.m
function max = mymax(n1,n2,n3,n4,n5)
mymax.m

Structure of a function file:


USING A FUNCTION FILE:
A user-defined function is used in the same way as built-in function. The function can be called from the command window, from a script file or from another function file. To use the function file, the folder where it was saved must either be in current folder.
For example:
>> mymax(10,20,30,31,42)
ans =
       42


No comments:

Post a Comment