0% found this document useful (0 votes)
16 views

User Defined Function

Uploaded by

Sourabh kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

User Defined Function

Uploaded by

Sourabh kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

User Defined Function

Lecture: 10 (Dr. Ajeet Kumar)

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.

Following points need to be considered to create a function file

1. Open a script (.m) file


2. First line of this file should start with function definition line. The syntax of function definition
line is written below

function definition line function [output variables] = fun_filename(input variables)

H1 Line % Message written in these statement lines will be printed

% in the command prompt when you use help command

% for the function search

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.

Input variable Output Variable function definition line

Single input Single output function y=file (x)

Single input multiple output function [y1, y2] = file (x)

Multiple input Single output function y=file (x1,x2)

Multiple input multiple output function [y1, y2, y3]=file (x1,x2)

Single/Multiple input No output function file (x1,x2)

No input No output function file

Calling of function file:

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: 1. Write a function file to calculate the addition of two numbers.

Discuss about local variable, H1 line statement, argument input value

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.

Input and Output Arguments

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.

“nargout” can be used for this purpose.

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

Write a function file to convert the Cartesian coordinates to cylindrical coordinates.

Few more function statements

 Inline function
 anomalous function

Inline function:

Syntax

f=inline (‘function_as_string’)

anomalous function:

Syntax

f=@(variables) function

You might also like