Functions in MATLAB

There are two types of functions in MATLAB

  • ·         Standard Library Function
  • ·         User Defined Function



  •   Standard Library Function- Standard library function can also be said as predefined function, predefined functions are functions which are already in there, MATLAB, C or any other language.
As you have seen many mathematical functions & predefined functions in MATLAB in our previous Blogs

rand                    %Generates uniformly distributed random numbers between 0 and 1.
randn                  %Generates normally distributed random numbers.
complex              % For Complex variables
exp(x)                 %Exponential 
log(x)                  %Natural logarithm.
ln(x)                    %log10(x) Common (base 10). 
sqrt(x)                 %Square root of x.


x=pascal(x)- Pascal Matrix
x=magic(x)- Magic Matrix wit Random Number
x=eye(x)-  Identical Matrix
x=zeros(x)- Zeros Matrix
x=ones(x)- Ones Matrix


Now some functions of Array
x=x'                    Transpose
x=[sum(x)]         Sum of elements
length(x)             Length of matrix or calculating dimensions
numel(x)             Determining total elements on matrix or array
sort(x,'ascend')    Ascending order  of elements array
sort(x,'descend')  Descending order  of elements array
mean(x)               Getting mean
max(x)                 Finding largest no. in series

min(x)                  Finding smallest no. in series




  •     User defined Functions- User define function are those function which are created by user for their ease.
These functions are created by users at the time of programming so that they should not use program lines again and again, in MATLAB creating user define function is easy, so for no trouble you can create your own define function and use it in any code.
For creating user defined function you need to remember some points-
1.      User defined, Functions name & File name should same.
For example- if you want to create function for simple interest than, Let suppose function name is simpleint & file name while saving should be simpleint.

2.  Creating function should start with-

function z=name(x,y)
%where x,y are variables
z = x+y %(or anything you want to create)
end


For example see the code below- Simple Interest function







Applications of user defined function-
Can solve formula based numerical with simple function creating

Can reduce efforts of repeating code in a program

Comments