EXPERIMENT 03
Introduction to MATLAB Functions
Objectives:
• Familiarizing students with MATLAB functions
Equipment required:
• MATLAB installed on PCs
Background Knowledge:
Functions:
A function is a group of statements that together perform a task. In MATLAB, functions are defined in
separate files. The name of the file and of the function should be the same. Functions operate on variables
within their own workspace, which is also called the local workspace, separate from the workspace you
access at the MATLAB command prompt which is called the base workspace. Functions can accept more
than one input arguments and may return more than one output arguments. The existing commands and
functions that compose the new function reside in a text file called an M-file with a filename extension of
‘m’. At the top of the file must be a line that contains the syntax definition for the new function.
Syntax:
function [y1,...,yN] = myfun(x1,...,xM)
function [y1,...,yN] = myfun(x1,...,xM) declares a function named myfun that accepts inputs x1,...,xM and
returns outputs y1,...,yN. This declaration statement must be the first executable line of the function. Valid
function names begin with an alphabetic character, and can contain letters, numbers, or underscores.
You can save your function:
• In a function file which contains only function definitions. The name of the file must match the
name of the first function in the file.
• In a script file which contains commands and function definitions. Functions must be at the end of
the file. Script files cannot have the same name as a function in the file. Functions are supported in
scripts in R2016b or later.
Files can include multiple local functions or nested functions. For readability, use the end keyword to
indicate the end of each function in a file. The end keyword is required when:
• Any function in the file contains a nested function.
• The function is a local function within a function file, and any local function in the file uses the end
keyword.
• The function is a local function within a script file.
This example defines a new function called “stat” that calculates the mean and standard deviation of a
vector. The variables within the body of the function are all local variables.
Input and Output Parameter:
The input and output variables can be scalars, vectors, matrices, and strings. In fact, MATLAB does not
really distinguish between variables types until some calculation or operation involving the variables is
performed. It is perfectly acceptable that the input to a function is a scalar during one call and a vector
during another call. To make the preceding point more concrete, consider the statement y = sin(x) which is
a call to the built-in sine function. If x is a scalar (i.e. a matrix with one row and one column) then y will
be a scalar. If x is a row vector, then y will be a row vector. If x is a matrix then y is a matrix.
Sub-Functions:
A sub-function, visible only to the other functions in the same file, is created by defining a new function
with the function keyword after the body of the preceding function or sub-function. Sub-functions are not
visible outside the file where they are defined.
For example, avg is a sub-function within the file stat.m:
An end statement can be used to terminate any function but, in most cases this is optional. end statements
are required only in m-files that employ one or more nested functions. Within such an m-file, every function
(including primary, nested, private, and sub-functions) must be terminated with an end statement.
Functions can have different output types and values. The output could be a scalar, vector or matrix
depending on the type of function.
Function with One Output:
Define a function in a file named average.m that accepts an input vector, calculates the average of the
values, and returns a single result.
Call this function from the command line.
Function with Multiple Outputs:
Define a function in a file named in a file named stat.m that returns the mean and standard deviation of an
input vector.
Call the function from the command line.
Function in a Script File:
The functions created in MATLAB can be called in the main script file and they provide outputs for
different inputs. For example, create a function in your workspace named yplusx.m.
Create a script file and assign values to inputs x and y. Call the function yplusx and obtain the result.
disp Command:
The “disp” command displays the value of variable in MATLAB. disp(X) displays the value of variable X
without printing the variable name. Another way to display a variable is to type its name, which displays a
leading “X =” before the value.
If a variable contains an empty array, disp returns without displaying anything.
Display Numbers:
Create a variable with numbers and then use the disp command to display these numbers. For example:
Display Text:
Create a variable with the desired text and then use the disp command to display this text. For example:
String Comparison in MATLAB:
strcmp command is used to compare strings.
tf = strcmp(s1,s2)
This command compares s1 and s2 and returns 1 (true) if the two are identical and 0 (false) otherwise. Text
is considered identical if the size and content of each are the same. The return result tf is of data type logical.
The input arguments can be any combination of string arrays, character vectors, and cell arrays of character
vectors.
Example:
Create a function that plots sin(x). Call this function in script file and get the plots.
sines Function and Script File:
Results when R is ‘f’ and ‘n’:
Lab Tasks:
1. Create a function that transform years in days. Call this function in script file, input the number
of years, and get the number of days as output.
2. Create a function with two arguments; frequency (f) and time (t).The function should return
the following:
a. 𝑋 = cos(2 ∗ 𝑝𝑖 ∗ 𝑓 ∗ 𝑡 2 )
1+2𝑡 −1
b. 𝑌 = 25∗𝑓∗𝑡
3. Create a function in MATLAB which displays the following output:
a) The entered number is a positive/negative number.
b) The entered number is even/odd number.
c) The entered number is a prime/composite number.
Call this function in script file and use input command to take input number from user.
Note: (Use for loops and if else conditions to write this code. Do not use built-in
functions to find prime numbers etc.)
4. Create a MATLAB function which computes the following sum while requesting the
value of x and n from the user.
The user should be able to call this function by typing s = sumnx(n,x) in the editor file.
5. Write a code in MATLAB which tests whether the input matrix is symmetric or not.
The function should display “The entered matrix is symmetric ” in case the matrix
input from user is symmetric. The function should be called by typing m = matsymm(
A) in the editor file and the matrix A should be taken as an input from user.
Note: An nxn matrix is called symmetric matrix when Ai,j=Aj,i with i≠j.
Conclusion: