MATLAB (an abbreviation of “matrix laboratory”) is a programming language and numerical computing environment developed by MathWorks. It is used for mathematical operations.
- Good understanding of any programming language.
- Have MATLAB installed, you can download it from here.
Uses of MATLAB
- Signal Processing and Communications.
- Data analysis and visualization.
- Scientific and engineering graphics.
- Developing algorithms.
- Modeling, simulation, and prototyping.
MATLAB has an additional package, called “Simulink”, that you can use for graphical multi-domain simulation and model-based design for dynamic and embedded systems. You can get access to the symbolic computing abilities by using an optional toolbox that uses the MuPAD symbolic engine.
As we mentioned before, MATLAB is like any programming language that allows you to do mathematical operations. For example, try:
Also, you can present complex numbers by:
|angle||angle of complex number x.|
|conj(x)||complex conjugate of x.|
|imag(x)||imaginary part of complex number x.|
|real(x)||real part of complex number x .|
The vector is a one-dimensional array of numbers.
You can create row vectors by:
a = [ 1 2 3 4]
Or by using a comma to delimit the elements.
MATLAB will return the following result:
a = 1 2 3 4
To output a specific range of elements, you have to reference the vector with a colon.
values=[6 513 21 9 23 1 0]; values(2:5)
The result will be:
ans = 513 21 9 23
If you want to list all of the vector elements, you can use
You need to use semicolons between values.
a = [1;2;3;4]
a = 1 2 3 4
The matrix is a two-dimensional array of numbers.
To create a matrix, you have to enter the elements in each row with space or colon between them. Then you have to use semicolons to delimit the rows as we saw above.
values=[1 2 3 4;5 6 7 8; 9 10 11 12]
values = 1 2 3 4 5 6 7 8 9 10 11 12
Now let’s create a smaller matrix from our existing one.
values(:, 1:2) and the result will be:
ans = 1 2 5 6 9 10
You can specify the rows you want before the comma, in our case, it is every row in that matrix. You can also specify the columns you want after the comma, in the code above it is the first and the second rows.
Check if you get the same result as shown below:
ans = 2 3 4 6 7 8 10 11 12
Zeros, ones, and eye
When you use
zeros which is a built-in function, it initializes a matrix of specified dimensions with values initialized to 0.
For example, if you write
a = zeros(4:5) then the result will be four rows and five columns of zeros:
a = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ones method is similar to
zeros, except it initializes the matrix’s values to 1.
Eye is a built-in function that generates an identity matrix. An identity matrix is characterized by all diagonal elements being 1 and the non-diagonal entries equal to 0.
c = eye(4)
The result is:
c = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Linspace generates vectors with entries spaced equidistantly. It takes in the starting point, the ending point, and the number of points as its arguments.
For example, if you write
linspace(1,10,10) then the result will be:
ans = 1 2 3 4 5 6 7 8 9 10
But if you change the last value to 8 instead of 10 then the result will be:
ans = 1.0000 2.2857 3.5714 4.8571 6.1429 7.4286 8.7143 10.0000
Using the MATLAB interpreter is not a wise choice when implementing and storing MATLAB code. A better option is to store the code in
Let’s begin by clicking on New Script.
Second, press on New followed by Function:
It is that easy!
Now let’s create a circle area function by:
function [area] = areafunction(r) %UNTITLED3 Summary of this function goes here % Detailed explanation goes here area = pi*r*r; end
After that, you have to save the function with the extension “.m” as we mentioned above.
areafunction(2) in the command window and the result will be:
ans = 12.5664
If you are familiar with the Matplotlib plotting library for Python programming language, it will be very easy for you to follow this section. However, we will explain it simply as possible.
To plot any graph we need to define the range of X and Y values. Then to call the plot command which is
Now let’s move on to a complex example.
We are going to create the following figure:
We need to define t (a variable indicating time).
t = 0:0.01:5
Then to define the first sin wave:
first = 3*sin(2*t+pi/2) subplot(2,2,1),plot(t,first) title('3sin(2t+pi/2)')
The subplot allows you to create multiple plots in one figure. The first and the second numbers specify how many rows and columns we want, in our case, it is 2 rows and 2 columns.
The last number (1) specifies that our plot will be in the first row and the first column (in the upper left corner). The title to define our plot title.
Let’s do the same for the second and the third waves.
second = 3*cos(2*t) subplot(2,2,3),plot(t,second) title('3cos(2t)') third = 2*cos(10*t) subplot(2,2,[2 4]),plot(t,third) xlabel('Time') ylabel('Amplitude') title('2cos(10t)')
xlabel('') and the
ylabel('') defined the names of those labels.
Note: Please be sure to execute all of the code together to allow the 3 plots to appear in the same figure.
Try it out for yourself and see the results. For more articles and updates on MATLAB and SimuLink, follow the languages section. Have a nice day.
Peer Review Contributions by: Lalithnarayan C
About the authorAhmad Mardeni
Ahmad is a passionate software developer, an avid researcher, and a business man. He began his journey to be a cybersecurity expert two years ago. Also he participated in a lot of hackathons and programming competitions. As he says “Knowledge is power” so he wants to deliver good content by being a technical writer.