A function is a collection of sequential statements that accepts an input argument from the user and provides output to the program.
Functions allow us to program efficiently. It avoids rewriting the computer code for calculations that are performed frequently.
User-defined functions are stored as M-files.
See a very simple MATLAB function poly20.m in the following Code that calculates the value of a particular polynomial.
% This function calculates the value of third order
output=x^3 +x+3;
Note that file name should be the same as that of function. Therefore, we save as poly20.m
In the Command Window,
>> poly20(3)
ans =
33
We have developed MATLAB function exchange.m given in the following Code to exchange the elements of pth and uth rows in any matrix.
We have written MATLAB function identityop.m in the following Code to place the identity element at any position in the matrix.
We have written a simple MATLAB function eliminationop.m in the following Code to eliminate all elements of any row using pivot element in the matrix.
Find rank of the following matrix in MATLAB
A = identityop(A,1,1/2)
A = eliminationop(A,2,1,-1)
A = eliminationop(A,3,1,-3)
A = eliminationop(A,3,2,-1)
Therefore, =Number of nonzero rows=2.