Image for post
Image for post
Photo by Markus Spiske on Unsplash

Let us try to make this a really fast paced, to the point article. Okay? Good. Let’s go!

So, ‘matlab’ is a Hindi language word which translates, in English, to ‘meaning of’. ‘Matlab’ can also be found in the Urdu language and here too it translates to ‘meaning of’.
And this word has got nothing to do with the software MATLAB.
(Haha. Gotcha!)

MATLAB is short for MATrix LABoratory .

It’s case sensitive. The syntax is somewhat like that of C language.

There are four different windows in MATLAB.

  1. MATLAB Desktop
    When you first launch the MATLAB software, what you see is the MATLAB Desktop. This window has three sub windows:
    The Command Window
    You type the commands here.
    The Command History
    All the commands in the present session are shown here. It’s the history of all the commands. If MATLAB is closed, the history is lost.
    The Workspace
    It has a listing of the variables in use, their memory space and the functions generated during the execution of the commands.
    Current Folder
    All the .m and .mat files present in the working directory are shown here.
  2. Figure Window
    Shows the output of the graphics
  3. Editor Window
    Just like any other editor window, this is where you write your code and save it with extension ‘.m’ .
  4. Simulink Model Window
    This is where the Simulink model is displayed.

Types of files in MATLAB

1. M Files
Standard ASCII text files. With ‘.m’ extension.
2. Mat Files
Binary data files with ‘.mat’ extension. These files are generated as soon as the save command is given for saving functions, variables etc.
3. Fig Files
Binary figure files in ‘.fig’ extension.
4. P Files
Compiled MATLAB files with ‘.p’ extension.
5. Mex Files
These are C and FORTRAN programs which are MATLAB callable.

Cool. So we now know the various windows and types of files in MATLAB.

Let’s look at some mathematical functions one can find in MATLAB.

Trigonometric Functions

  1. sin(a)
    Gives the sine of ‘a’. Here ‘a’ is considered to be in radians.
  2. sind(a)
    Gives the sine of ‘a’ with ‘a’ now in degrees.
  3. asin(a)
    Gives the inverse sine of ‘a’. ‘a’ is in radians.
  4. asind(a)
    Gives the inverse sine of ‘a’. ‘a’ is in degrees.
  5. sinh(a)
    Gives the hyperbolic sine of ‘a’.
  6. asinh(a)
    Gives the inverse hyperbolic sine of ‘a’.

And similarly we have ‘cos’, ‘tan’, ‘sec’, ‘csc’ and ‘cot’ functions. With all the six variations as mentioned above.

Functions Related To Complex Numbers

  1. abs (5+9j)
    This will give the resultant of the complex number within brackets, i.e square root of (5*5 + 9*9).
    Note that passing just a real number in this function will produce the absolute (positive) value of it. Example: ‘abs(-9)’ will result in ‘9’.
  2. real(5+9j)
    This will output the real part of the complex number, i.e. ‘5’.
  3. imag(5+9j)
    This will output the imaginary part of the complex number, i.e. ‘9’.
  4. conj(5+9j)
    This will return the conjugate of the complex number passed into it, i.e.
  5. complex(3,8)
    This will produce a complex number using the first argument (3) as real part and the second argument (8) as the imaginary part of it.
  6. angle(5+9j)
    This is will return the angle this complex number makes with the horizontal.

Functions To Generate Arrays and Matrices

  1. linspace(0,1,10)
    generates 10 equally spaced points between ‘0’ and ‘1’.
  2. logspace(0,3)
    Generates 50 logarithmically spaced points between 10⁰ and 10³.
  3. logspace(0,3,4)
    Generates 4 logarithmically spaced points between 10⁰ and 10³.
  4. zeros(1,10)
    Generates a 1 x 10 matrix of zeros.
    0 0 0 0 0 0 0 0 0 0
  5. ones(1,5)
    Generates a 1 x 5 matrix of ones.
    1 1 1 1 1
  6. a = 0:10:35
    Generates a sequence of numbers from 0 to 35 with a step of 10.
    0 10 20 30
  7. a = 4:8
    Generates a sequence of numbers from 4 to 8 with a (default) step of 1.
  8. a=5:0
    Will generate an empty matrix, since to go from ‘5’ to ‘0’, we need a negative value of step. But the default value is ‘1’.
  9. eye(5,5)
    Generates a 5 x 5 matrix of zeros with ones on the main diagonal.
    1 0 0 0 0
    0 1 0 0 0
    0 0 1 0 0
    0 0 0 1 0
    0 0 0 0 1
  10. rand(2,3)
    Generates a 2 x 3 matrix of random numbers.
    0.4565, 0.8214, 0.6154
    0.0185, 0.4447, 0.7919

Matrix Functions

  1. diag(v)
    If v is a vector, diag(v) will produce a square matrix with elements of v on the main diagonal.
  2. diag(A)
    If A is a matrix, diag(A) will produce a column vector with the main diagonal elements of matrix A.
  3. rot90(A)
    Rotates the matrix A by 90 degrees.
  4. rot90(A,K)
    Rotates the matrix A by k*90.
  5. flipr
    Flips the matrix from left to right.
  6. flipud
    Flips the matrix from up to down.
  7. tril
    Extracts the lower triangular part of the matrix
  8. trilu
    Extracts the upper triangular part of the matrix
  9. reshape(A, 5, 4)
    Reshape the matrix A into a 5 x 4 matrix.
  10. a+6
    For a matrix ‘a’, a+6 will add ‘6’ to each element of the matrix.
  11. 2*a
    For a matrix ‘a’ , 2*a will multiple each element of the matrix by ‘2’.
  12. a ^ 2
    This function will produce a * a matrix, i.e. it will multiply the matrix a with itself.
  13. a-1
    It will substract 1 from each element of matrix a.
  14. a. ^ 2
    This function will replace each element of the matrix a with its square.
  15. sqrt(a)
    This function with replace each element of matrix a with its square root.
  16. sqrtm(a)
    This function will produce a matrix ‘b’ such that, ‘b * b’ matrix is equal to matrix ‘a’.
  17. log(A)
    This function will replace each element of matrix A with it’s log value(natural base).
  18. a(4,5)
    For a matrix ‘a’, this function will return the element at location (4,5).
  19. a(4,5:7)
    This function will return all the elements of matrix ‘a’ from row 4 and column 5 to 7.


The polynomial 7x² + 8 will be written as h = [7 0 8]
Similarly, 5x⁵ + 4x⁴ + 6x² — 9 will be written as
h=[5 4 0 6 0 -9]
Some functions:

  1. polyval(c,7)
    Wil return the value of polynomial ‘c’, when ‘x’ in the polynomial is replaced by ‘7’.
  2. roots(c)
    This function will return the roots of the polynomial ‘c’.
  3. conv(a,b)
    This function will return the multiplication of the two polynomials ‘a’ and ‘b’.
  4. [q,r] = deconv(a,b)
    This function divides the polynomial ‘a’ by ‘b’. And returns the quotient and the remainder.
  5. polyder(a)
    Will return the first derivative of the polynomial ‘a’.
  6. polyder(a,b)
    This function first multiplies polynomial ‘a’ with ‘b’ and then returns the derivative of the product polynomial.
  7. polyfit(x,y,n)
    This one is a little tricky.
    So, suppose we have a set of points with x and y coordinates. And we want to plot these points on a graph. And then see what kind of curve we obtain.
    In the function polyfit(x,y,n) , ‘x’ is the vector with x coordinates. ‘y’ is the vector with y coordinates. And ’n’ is the degree of the polynomial (curve) that we want to these points into. The equation of a straight line is of the form x=Cy and has a degree of one.
    Suppose x and y are the coordinates of a straight line. We let n=1. polyfit(x,y,1)
    Obviously, when these points are plotted on a graph, a straight will pass through all these points. But suppose these points are not the coordinates of a straight line. But of some random curve. In such a case if we pass n=1, the curve formed (a straight line) will not pass through all the points.
    We hence use a different (greater) value of ’n’ to obtain a better graph.
    polyfit(x,y,n) will return the coefficients of the polynomial of degree ’n’ that is a best for the data in y.

Linear Regression

Let us first understand (vaguely) what Linear Regression is.
Suppose we have some relational database, i.e. some database in two variables ‘x’ and ‘y’, where ‘y’ is related to ‘x’.
We say ; Y is dependent on X.
For example, a list of hundred cities(x) and the number of road accidents(y) in each of these cities that took place in a particular year.
Each city will be mapped to a number(of accidents).
If we plot this data on a graph, the cities on x axis and number of accidents on y axis, it is easy to understand that we would not obtain a straight line. Because there is no relation between the number of road accidents in one city to that of another.
If for each city and number of accidents pair (x,y) , a dot is drawn on the graph, there dots will be randomly distributed.

Something like this:

Image for post
Image for post

Now we want to draw a straight line such that it passes through the maximum number of dots in this graph.

Image for post
Image for post

The linear regression function (represented by a backward slash) in MATLAB gives the slope of such a line.

If X is a matrix of some data values and Y is a matrix of data values that are related to respective values of X , then
X\Y will return the slope of a straight line that passes through maximum number of dots, when the given data is plotted on a graph. This slope is called Regression Coefficient.

Multiple Linear Regression

Now, if instead of just depending on one variable, Y depends on more than one variable, i.e. Y is dependent on X1, X2, X3 and so on.
In this case if we try to find the slope of a straight line such that it passes through maximum number of dots, it is called Multiple Linear Regression.

The regress(y, [x1, x2, x3]) function of MATLAB can be used to find the regression coefficient in this scenario.

Non Linear Regression

If ‘y’ is dependent on more than one variables; x1,x2, x3… but ‘y’ cannot be expressed as a linear combination of x1,x2,x3…, then it is said that the function y = f(x1, x2, x3…) is non linear. And on such a function, we carry out non linear regression.
In MATLAB nlinfit function can be used to achieve this.

Various Loops in MATLAB

  1. For Loop
    A for loop ends with the ‘end;’ statement.
    To sum all the numbers of a row vector.
    sum = 0;
    for k = [1,2,5,7,9];
    sum = sum+k
    sum =1
    sum = 3
    sum = 8
    sum = 15
    sum = 24
    Another example;
    sum = 0;
    for k[1:10]
    sum = sum+k
    This loop will sum all the numbers from 1 to 10.
  2. While Loop
    The loop keeps running as long as the condition defined is true. Example: sum = 0;
    num = input(‘Enter a number : ’);
    sum = sum+num;
    num = input(‘Enter a number: ’);
    This will keep taking input and adding that to ‘sum’ as long as the input is not equal to 0.
  3. If Loop
    It is actually a conditional operator.
    Check this example.
    num = input(‘Enter a number: ’);
    display(‘The number is greater than 50’);
    else if(num<50)
    display(‘The number is less than 50’);
    display(‘The number is equal to 50’);
  4. Switch Cases
    num = input(‘Enter ‘1’ for small, ‘2’ for medium and ‘3’ for large.’);
    switch num,
    fprintf(‘You chose small.’);
    fprintf(‘You chose medium’);
    fprintf(‘You chose large’);


Suppose we have a set of ten x and y coordinates representing some data. Each x is related to y by some function ‘f’. The act of speculating more ‘x’ coordinates and their corresponding ‘y’ coordinates using the ‘f’ functions is called interpolation.
Interpolation can be used for filling in the missing data or to make predictions.
We’ll look at two main interpolation functions provided by MATLAB: interp1 and interp2

It is used for 1D functions.
Let x be the 1D matrix with ‘x’ coordinates and ‘y’ be the 1D matrix with ‘y’ coordinates. And xq be the vector containing the query points around which the interpolated values need to be calculates.
interp1(x, y, xq) will return the interpolated values.

Another syntax of interp1 functions is: interp1(x, y, xq, method), where one can specify the method of interpolation. By default the method is ‘linear’.
Alternative interpolation methods are :
nearest (nearest neighbor interpolation),
(piece wise cubic Hermit interpolation),
(cubic spline interpolation),
cubic (same as pchip) etc.


Used for the interpolation of 2D gridded data.
Z1 = interp2(X, Y, Z, X1, Y1)
X, Y, Z are the matrixes.
Z1 will then contain the elements corresponding to elements of X1, Y1 and determined by interpolation within the 2D function specified by the matrix X, Y, Z.

Fuzzy Logic

The easiest way to understand Fuzzy Logic is to do so in contrast to Boolean Logic. Boolean logic has two states, true(1) or false(0). But Fuzzy Logic can have many states in between 0 and 1.
For example:
Is it hot?
Boolean Logic:
1 → Yes, it is hot.
0 → No, It is not hot.

Fuzzy Logic:
1 → It is very hot.
0.87 →It is hot.
0.5 → It is warm.
0.1 → Not hot at all.


An Artificial Neural Network (ANN) is an adaptive system that learns from the data it is fed. From the knowledge of this training data, it can predict results for more data that it has not seen before.


Simulink is the graphical extension of MATLAB.
The systems are drawn as block diagrams in Simulink Models.
In MATLAB command prompt, simply type ‘simulink’ to start Simulink.

Dealing with Ordinary Differential Equations in MATLAB

To solve Ordinary Differential Equations, MATLAB provides several functions.
1. ODE23
2. ODE45
3. Quad
4. Quad8

Dealing with Partial Differential Equations in MATLAB

residue(p,q) function can be used to carry out partial differential of equations.

Randomly organized shit.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store