User Defined Function
User Defined Function
Massive Library of Inbuilt functions makes MATLAB as a first choice software for computational
work. MATLAB also gives flexibility to the users to create functions as per user’s need. Such
functions are called as User Defined Function.
User defined file in MATLAB can be created by using a special type of M-file (.m ) called function
file. All the variables in the function file are local variable means their values are available within
the function only. After the execution of the function, these variables will not be stored in the
workspace of MATLAB.
function file is used when a set of commands needs to be used number of times.
Function Body
statement
end
function: function definition line start with function statement, each letter of which should be in
lowercase.
output variables: output variables are those variables which are computed during program and
need to show after the execution of the program. These output variables which you want to show
as an output of the program, should be written inside square bracket (this bracket is optional, if
there is only one output). If there are more than one output variables, these should be separated
by comma ( , ) or one spacing.
input variables: input variables should always been written within parentheses, even if there is
one input variable. If input variables are more than one, they must be separated by comma ( , ).
fun_filename: fun_filename should be same as file name with which this function file is saved
(using extension .m). If the function name is num_operation, the function file should be saved with
name num_operation.m.
Any function file is called by typing its name in command window or within another script file.
Thus, while writing the function file name, one should be very careful that this name should not the
predefined inbuilt or user defined function. The existence of the same can be checked by writing
“exist function” (for detail about exist command, go to help section).
Problem: 2. Write a function file to calculate area and circumference of circle by taking radius as
input.
Show that in case of more than one output, if you have not given any output name, it will show
result of first output.
Problem: 3. Call the function file of Problem 2 in a script file and show the variation of area and
circumference with radius.
Sometimes we need to write a function that behaves differently depending on how many number
of inputs the function have. One can use “nargin” function which stands for “number of input
arguments”. Using condition loop one can direct the flow of computation depending on the number
of input arguments.
Problem: Write a function file to compute the square root of the input if there is only one, but
compute the square root of the average if there are two inputs.
In previous discussion, we have seen that if any function is written for three output variables,
user can see one, two or three outputs of the function, without having any error message from
the MATLAB. In this case, Matlab does the calculation for all the output parameters, however, it
displays the only those which is requested by the user.
It is, therefore, useful to calculate the number of desired output by the user, so that the parameters
which are not desired by the user should not computed and thus a programmer can save the time
by avoiding the unnecessary calculations.
Problem: The following formulas enable us to convert a 3-D point representation from Cartesian
coordinates (x,y,z) to cylindrical coordinates (r,θ,zycl):
𝑟 = √𝑥 2 + 𝑦 2
𝑦
𝜃 = 𝑡𝑎𝑛−1 ( )
𝑥
zycl=z
Inline function
anomalous function
Inline function:
Syntax
f=inline (‘function_as_string’)
anomalous function:
Syntax
f=@(variables) function