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-de fined functions are stored as M-fi les.
See a very simple MATLAB function poly20.m in the following Code that calculates the value of a particular polynomial.

function output = poly20 (x)
% This function calculates the value of third order
output=x^3 +x+3;

Note that fi le 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.

function A=exchangeop (A, p , u)
% input : augmented matr ix A, row p , u
% output : augmented mat rix A
[ ~ , n]= s i z e (A) ;
for J=1:n
t=A(p , J ) ;
A(p , J)=A(u , J ) ;
A(u , J)=t ;
end

 

We have written MATLAB function identityop.m in the following Code to place the identity element at any position in the matrix.

function A=identityop (A, p , e )
%input : Augmented matr ix A, pivot
%row p , pivot e l ement e
%output : Augmented matr ix A
[ ~ , n]= size (A) ;
format ra t
for J=1:n
A(p , J)=sym( e*A(p , J ) ) ;
end

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.

function A=eliminationop (A, u , p , co )
%input : augmented mat rix A, row u ,
%pivot row p , c o e f f i c i e n t v a l u e co
%output : augmented matr ix A
[ ~ , n]= size (A) ;
format rat
for J=1:n
A(u , J)=sym(A(u , J)+( co *A(p , J ) ) ) ;

end

Find rank of the following matrix in MATLAB

\[ \left( \begin{array}{ccc} 2 & 2 & 2 & -2 \\ 1 & 2 & 3 & 4 \\ 3 & 4 & 5 & 2 \end{array} \right)\]

A = identityop(A,1,1/2)

\[ \left( \begin{array}{ccc} 1 &1 & 1 & -1 \\ 1 & 2 & 3 & 4 \\ 3 & 4 & 5 & 2 \end{array} \right)\]

A = eliminationop(A,2,1,-1)

\[ \left( \begin{array}{ccc} 1 &1 & 1 & -1 \\ 0 & 1 & 2 & 5 \\ 3 & 4 & 5 & 2 \end{array} \right)\]

A = eliminationop(A,3,1,-3)

\[ \left( \begin{array}{ccc} 1 &1 & 1 & -1 \\ 0 & 1 & 2 & 5 \\ 0 & 1 & 2 & 5 \end{array} \right)\]

A = eliminationop(A,3,2,-1)

\[ \left( \begin{array}{ccc} 1 &1 & 1 & -1 \\ 0 & 1 & 2 & 5 \\ 0 & 0 & 0 & 0 \end{array} \right)\]

Therefore,  \rho (A)=Number of nonzero rows=2.