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.
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.
- 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.
It has a listing of the variables in use, their memory space and the functions generated during the execution of the commands.
All the .m and .mat files present in the working directory are shown here.
- Figure Window
Shows the output of the graphics
- Editor Window
Just like any other editor window, this is where you write your code and save it with extension ‘.m’ .
- 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.
Gives the sine of ‘a’. Here ‘a’ is considered to be in radians.
Gives the sine of ‘a’ with ‘a’ now in degrees.
Gives the inverse sine of ‘a’. ‘a’ is in radians.
Gives the inverse sine of ‘a’. ‘a’ is in degrees.
Gives the hyperbolic sine of ‘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
- 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’.
This will output the real part of the complex number, i.e. ‘5’.
This will output the imaginary part of the complex number, i.e. ‘9’.
This will return the conjugate of the complex number passed into it, i.e.
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.
This is will return the angle this complex number makes with the horizontal.
Functions To Generate Arrays and Matrices
generates 10 equally spaced points between ‘0’ and ‘1’.
Generates 50 logarithmically spaced points between 10⁰ and 10³.
Generates 4 logarithmically spaced points between 10⁰ and 10³.
Generates a 1 x 10 matrix of zeros.
0 0 0 0 0 0 0 0 0 0
Generates a 1 x 5 matrix of ones.
1 1 1 1 1
- a = 0:10:35
Generates a sequence of numbers from 0 to 35 with a step of 10.
0 10 20 30
- a = 4:8
Generates a sequence of numbers from 4 to 8 with a (default) step of 1.
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’.
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
Generates a 2 x 3 matrix of random numbers.
0.4565, 0.8214, 0.6154
0.0185, 0.4447, 0.7919
If v is a vector, diag(v) will produce a square matrix with elements of v on the main diagonal.
If A is a matrix, diag(A) will produce a column vector with the main diagonal elements of matrix A.
Rotates the matrix A by 90 degrees.
Rotates the matrix A by k*90.
Flips the matrix from left to right.
Flips the matrix from up to down.
Extracts the lower triangular part of the matrix
Extracts the upper triangular part of the matrix
- reshape(A, 5, 4)
Reshape the matrix A into a 5 x 4 matrix.
For a matrix ‘a’, a+6 will add ‘6’ to each element of the matrix.
For a matrix ‘a’ , 2*a will multiple each element of the matrix by ‘2’.
- a ^ 2
This function will produce a * a matrix, i.e. it will multiply the matrix a with itself.
It will substract 1 from each element of matrix a.
- a. ^ 2
This function will replace each element of the matrix a with its square.
This function with replace each element of matrix a with its square root.
This function will produce a matrix ‘b’ such that, ‘b * b’ matrix is equal to matrix ‘a’.
This function will replace each element of matrix A with it’s log value(natural base).
For a matrix ‘a’, this function will return the element at location (4,5).
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]
Wil return the value of polynomial ‘c’, when ‘x’ in the polynomial is replaced by ‘7’.
This function will return the roots of the polynomial ‘c’.
This function will return the multiplication of the two polynomials ‘a’ and ‘b’.
- [q,r] = deconv(a,b)
This function divides the polynomial ‘a’ by ‘b’. And returns the quotient and the remainder.
Will return the first derivative of the polynomial ‘a’.
This function first multiplies polynomial ‘a’ with ‘b’ and then returns the derivative of the product polynomial.
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.
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:
Now we want to draw a straight line such that it passes through the maximum number of dots in this graph.
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
- 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 = 3
sum = 8
sum = 15
sum = 24
sum = 0;
sum = sum+k
This loop will sum all the numbers from 1 to 10.
- 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.
- If Loop
It is actually a conditional operator.
Check this example.
num = input(‘Enter a number: ’);
display(‘The number is greater than 50’);
display(‘The number is less than 50’);
display(‘The number is equal to 50’);
- Switch Cases
num = input(‘Enter ‘1’ for small, ‘2’ for medium and ‘3’ for large.’);
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),
pchip (piece wise cubic Hermit interpolation),
spline (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.
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.
Is it hot?
1 → Yes, it is hot.
0 → No, It is not hot.
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.
Dealing with Partial Differential Equations in MATLAB
residue(p,q) function can be used to carry out partial differential of equations.