[go: up one dir, main page]

0% found this document useful (0 votes)
6 views18 pages

06 - Chapter 01 Introduction to MATLAB

Chapter 1 introduces MATLAB as a versatile tool for engineering, particularly in electrical engineering, highlighting its capabilities for problem-solving, system design, and model simulation. The chapter covers the history of MATLAB, its purpose, importance, installation methods, system requirements, and key features, emphasizing its wide applicability across various engineering fields. Additionally, it outlines the MATLAB environment and the role of variables in programming within the software.

Uploaded by

edgardoehvcm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views18 pages

06 - Chapter 01 Introduction to MATLAB

Chapter 1 introduces MATLAB as a versatile tool for engineering, particularly in electrical engineering, highlighting its capabilities for problem-solving, system design, and model simulation. The chapter covers the history of MATLAB, its purpose, importance, installation methods, system requirements, and key features, emphasizing its wide applicability across various engineering fields. Additionally, it outlines the MATLAB environment and the role of variables in programming within the software.

Uploaded by

edgardoehvcm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Chapter 1

Introduction to MATLAB

1.1 Introduction

MATLAB is a highly useful tool in engineering for solving problems, designing


systems, and simulating models. The versatile nature of MATLAB makes it suitable
for numerous applications catering to the needs of almost all engineering fields. This
book is particularly designed for electrical engineering problems. This chapter will
provide a basic overview of MATLAB and help the readers get familiarized with the
software to be able to gradually build the complete idea given in the next chapters.

1.2 What Is MATLAB?

MATLAB provides an environment for researchers and engineers of all domains to


create models and algorithms and compute and analyze numerical data with pro-
gramming capability. The acronym MATLAB came from Matrix Laboratory, as
initially the software was built with a goal to perform numerous operations on
matrices and vectors. Over the years, MATLAB has developed several toolboxes
that facilitate research on control systems, signal/image processing, deep learning,
robotics, and so on. It provides the high graphic capability to visualize data in both
2D and 3D formats. MATLAB is also a high-level programming language, and the
platform offers great flexibility to use with other programming languages, e.g.,
Python, C/C++, Java, Fortran, etc. Another distinguishing feature of MATLAB is
that it can run in a public cloud environment outside the MathWorks cloud domain.
The features of parallel computing, hardware interfacing, embedded applications,
and app building have elevated MATLAB to a much higher level in the field of
scientific research and engineering applications.

© The Author(s), under exclusive license to Springer Nature Switzerland AG 2022 1


E. Hossain, MATLAB and Simulink Crash Course for Engineers,
https://doi.org/10.1007/978-3-030-89762-8_1
2 1 Introduction to MATLAB

1.3 History, Purpose, and Importance

The historical development of MATLAB, the purpose of introducing MATLAB,


and the importance of MATLAB in the present engineering world are described in
the following sections.

1.3.1 History

The earliest origin of MATLAB can be traced back to the invention of EISPAC
(Matrix Eigensystem Package) software, which was developed to solve eigenvalue
problems. The fundamental of this software was the procedure followed by ALGOL
60 to solve such problems. It was first developed at the Argonne National Laboratory
around 1970, and the first version of the software was released in 1971, followed by
the release of the second update in 1976. Later, another software for mathematical
analysis named LINPACK (Linear Equation Package) was developed as a byproduct
in 1975 in the same lab by Cleve Moler, Jack Dongarra, Pete Stewart, and Jim
Bunch. EISPACK and LINPACK both were formed in Fortran and can be consid-
ered as the very primal stage before the appearance of MATLAB.
Although EISPACK and LINPACK were capable of performing numerical
analysis and solving linear algebra problems, Moler wanted to upgrade both of
them to reduce the complexity of access for his students. With that aim, Moler
first came up with the idea of MATLAB, which was named after the Matrix
Laboratory. MATLAB was simply a matrix calculator, where the data type of
input was matrix. This version was created by Moler only for the usage of his
students, which is later regarded as classical MATLAB.
The idea of the first commercial MATLAB was proposed by a graduate student of
Stanford named Jack Little in 1983. Jack Little, Steve Bangert, and Cleve Moler took
the initiative to bring IBM PC-based MATLAB, which was translated in C from
Fortran. The first PC-based commercial MATLAB made its appearance in
December 1984, followed by the first marketization in 1985. This new version of
MATLAB was updated and modified significantly by both Jack Little and Steve
Bangert, where they added many mathematical functions, graphics, and toolboxes
based on various applications. MATLAB has been upgraded multiple times for
adjusting itself with the new applications and requirements in the domain of engi-
neering and science. The major changes that have occurred over time in different
versions of MATLAB are summarized in Table 1.1.
1.3 History, Purpose, and Importance 3

Table 1.1 Chronological development of MATLAB


MATLAB version Major features Year
Classical • Input data type matrix Around
MATLAB • Used as a simple matrix calculator 1981
• Written in Fortran
MATLAB 1.0 • Translated in C from Fortran 1984
(PC-MATLAB) • IBM PC-based software
• Multiple mathematical functions, graphics, and
toolboxes
MATLAB-3 • Ordinary differential equation toolbox 1987
• Signal processing toolbox
MATLAB-4 • Simulink 1992
• Sparse matrix
• 2D and 3D color graphics
MATLAB-5 • Data types 1996
• Visualization (advanced)
• Cell arrays and structure
• Graphical user interface
MATLAB-6 • Desktop MATLAB 2000
• Linear Algebra Package (LAPACK)
MATLAB-7 • Parallel computing toolbox 2004
• Anonymous function
• Nested function
• Integer data types
MATLAB-8 • MATLAB app 2012
• Toolstrip interface
MATLAB-9 • Live editor 2016
• App designer
MATLAB-9.9 • Simulink online 2020
MATLAB-9.10 • Satellite communication toolbox 2021
• Radar toolbox
• DDS blockset

1.3.2 Purpose and Importance

The original purpose of MATLAB is to provide a programming platform where


mathematical analysis, along with different applications in the engineering and
science domain, can be performed in the most optimized and user-friendly way.
As mentioned earlier, MATLAB can take input in the form of a matrix; hence, the
vector, array, and matrix operations can be performed more easily by writing
minimal code compared to other programming languages. In MATLAB, algorithm
development and advanced mathematics problems can be solved using numerous
built-in functions. One of the most important features of MATLAB is its toolbox,
which can be used to enhance the performance of MATLAB in any desired domain.
4 1 Introduction to MATLAB

MATLAB covers control systems, signal processing, image processing, robotics,


communications, mechatronics, biology, data analytics, and other numerous fields in
different domains. Such widespread versatility and functionality of MATLAB have
made it one of the most widely used scientific platforms in the world.

1.4 Installation and Dependencies

Installation of MATLAB software can be conducted in three ways [1], as described


hereunder.

Method 1: Installation with Internet Connection


Step 1: Sign in to the MathWorks account and download the installer.
Step 2: Run the installer and accept the license agreements, which will create the
appearance of the following window in Fig. 1.1.
Step 3: Use the activation key, or navigate to the relevant license file by selecting the
Advanced Options > I want to install network license manager.
Step 4: Navigate to the directory, where the MATLAB will be installed.
Step 5: MATLAB provides a variety of products, and it provides the option to the
user to only install products that are necessary, or relevant for a particular user.
Thus, it facilitates the users to save enough space by avoiding the installation of
irrelevant products. Choose the products based on your work or interest.
Step 6: Choose any convenient option and make the confirmation.
Step 7: Click on the “Begin Install” option, and wait for a while.
Step 8: After the installation is completed, a “Finish” option will appear. By clicking
on it, finalize the installation of MATLAB.
Method 2: Installation Using File Activation Key
Step 1: Start the installer by clicking on the installer file.
Step 2: Accept all the terms and agreements.

Fig. 1.1 MathWorks Product Installer


1.4 Installation and Dependencies 5

Fig. 1.2 MathWorks Product Installer dropdown menu

Step 3: Find the “Advanced Option” dropdown box, and choose the option “I have a
File Installation Key.” This is shown in Fig. 1.2.
Step 4: Enter the key.
Step 5: Navigate to the license file location.
Step 6: Navigate to the directory, where MATLAB will be installed.
Step 7: Choose the products for the installation.
Step 8: Choose the options that are convenient.
Step 9: Click on the “Begin Install” option, and wait for a while.
Step 10: After the installation is completed, a “Finish” option will appear. By
clicking on it, finalize the installation of MATLAB.
Method 3: Download Installation Package
Step 1: Download the installation file and run the installer.
Step 2: MathWorks sign-in option will appear; hence, sign in to the account. From
the dropdown “Advanced Options,” select the “I want to download without
installing” option.
Step 3: Navigate to the directory where MATLAB will be installed.
Step 4: Choose the platform of installation and also decide the products that are
relevant to be installed.
Step 5: After proceeding further, a “Begin Download” option will appear. Click on
that option, and wait for a while.
Step 6: When the download will be finished, click on the “Finish” button, which will
appear right after the completion of the download.
Step 7: Move the file to the desired destination, and run the installer file. For
windows, the installer file will be found as “setup.exe.” For Linux and MAC,
the name of the installer file will be “install” and “InstallForMacOSX,”
respectively.
Step 8: After running the installer file, follow Method 1, if the Internet connection is
available. Otherwise, follow Method 2, if a file activation key is available for
completing the installation.
6 1 Introduction to MATLAB

1.4.1 Dependencies

Before installing MATLAB, the dependencies, or the system requirements, need to


be checked. The dependencies for different platforms are listed in Table 1.2.

Table 1.2 Dependencies of MATLAB


Windows Mac Linux
Operating • Windows Server 2016 • macOS Mojave (10.14) • Red Hat Enterprise
systems • Windows Server 2019 • macOS Catalina (10.15) Linux 7 (minimum 7.6)
• Windows 7 Service Pack • macOS Big Sur (11) • Red Hat Enterprise
1 Linux 8 (minimum 8.1)
• Windows 10 (version • Ubuntu 16.04 LTS
1803 or higher) • Ubuntu 18.04 LTS
• Ubuntu 20.04 LTS
• SUSE Linux Enterprise
Server 12 (minimum
SP2)
• SUSE Linux Enterprise
Desktop 12 (minimum
SP2)
• SUSE Linux Enterprise
Server 15
• SUSE Linux Enterprise
Desktop 15
RAM Min. 4 GB Min. 4 GB Min. 4 GB
Recommended: 8 GB Recommended: 8 GB Recommended: 8 GB
For Polyspace: 4 GB/core For Polyspace: 4 GB/core For Polyspace: 4 GB/
(recommended) (recommended) core (recommended)
Processors Min. any Intel or AMD Min. any Intel x86-64 Min. any Intel x86-64
x86-64 processor processor processor
Recommended: any Intel Recommended: any Intel Recommended: any Intel
or AMD x86-64 processor x86-64 processor with x86-64 processor with
with four logical cores and four logical cores and four logical cores and
AVX2 instruction set AVX2 instruction set AVX2 instruction set
support support support
Disk 3.4 GB of HDD space for 3 GB of HDD space for 3.3 GB of HDD space for
MATLAB only, 5–8 GB MATLAB only, 5–8 GB MATLAB only, 5–8 GB
for a typical installation for a typical installation for a typical installation
A full installation of all A full installation of all A full installation of all
MathWorks products may MathWorks products MathWorks products
require 29 GB may require 22 GB may require 27 GB
Graphics Graphics card: not limited Graphics card: not limited Graphics card: not lim-
Recommended: hardware Recommended: hardware ited
accelerated graphics card accelerated graphics card Recommended: hard-
supporting OpenGL 3.3 supporting OpenGL 3.3 ware accelerated
with 1 GB GPU memory with 1 GB GPU memory graphics card supporting
OpenGL 3.3 with 1 GB
GPU memory
1.6 MATLAB Environment 7

1.5 Starting MATLAB

Windows: Open the program menu and find the MATLAB application file (matlab.
exe) from the directory. Double-click on it to start the MATLAB.
Linux: Open the terminal and write “matlab.” After pressing enter, the MATLAB
will start.
Mac: On the dock, find the MATLAB icon. By clicking on it, MATLAB can be
opened.

1.6 MATLAB Environment

In the MATLAB environment, there are multiple windows that can be set as the
starting layout. In a starting layout, the basic windows that are docked in default
form are Command Window, Editor, Current Folder, and Workspace. In the header
tool strip of MATLAB, the “Layout” option is available, by clicking on which the
layout can be changed according to our convenience. In Fig. 1.3, the layout of the
MATLAB starter page in default mode is shown. The “Command Window” pro-
vides a window where any MATLAB command can be written for execution. The
command can be run by pressing the “Enter” button on the keyboard. For writing a
long program, the command window probably is not the best place. For that, the
“Editor” window is available, where a complete program can be written and run for
execution. The output of the program will appear on the command window. In the
default mode, there are also two other windows: Current Folder or Directory and

Fig. 1.3 MATLAB environment


8 1 Introduction to MATLAB

Workspace. The current directory indicates the directory from where any MATLAB
file can be exported or imported. In the workspace, all the variables defined in the
command window, or the editor, will appear along with their values after the
execution of the program. From the “Layout” menu, “Command History” window
can also be docked in the starter page, which lists all the previous commands in an
ordered manner. Therefore, if any previous command needs to be reused, it can be
selected from the command history. By clicking the “Up” and “Down” key of the
keyboard, the previous commands can also be navigated. In MATLAB, there is a
header tool strip, where multiple menus and tools are available, which are also parts
of the MATLAB environment.

1.7 Features of MATLAB

The features of MATLAB are crucial in the field of engineering. Some of the most
important features of MATLAB can be listed as follows:
1. High-level programming language
2. Built-in graphics
3. Interactive environment
4. High computational capability
5. Numerous mathematical functions
6. Numerous toolboxes dedicated to separate applications
7. Compatibility with other languages
8. Parallel computing
9. App designing
10. Algorithm formation
11. Hardware interfacing
12. Deployment capability, etc.
One of the important aspects of MATLAB is that it is not limited to one
application area; instead, it covers a wide area of applications. MATLAB 9.10
version has a total of 82 toolboxes dedicated to numerous application fields in
various domains. A list of available toolboxes in different application fields is
given in Table 1.3.

1.8 Variables in MATLAB: Categories and Conversion


Between Variables

While writing a MATLAB code, variables are used to define different parameters
that are needed to be used in the same program multiple times. In variable definition,
the main task is to assign data against each variable, and this assigned data can be of
1.8 Variables in MATLAB: Categories and Conversion Between Variables 9

Table 1.3 Number of toolboxes available in MATLAB for various applications


Applications Number of toolboxes
Aerospace 04
Automotive 10
Code verification 07
Computational biology 02
Computational finance 07
Control systems 10
FPGA, ASIC, and SoC development 08
Image processing and computer vision 03
RF and mixed signal 05
Robotics and autonomous systems 09
Signal processing 06
Test and measurement 06
Wireless communications 05
Total 82

different types, such as integer, float, string, etc. When a variable is defined, it can be
observed in the Workspace. The name of the variables can be any letter, or any
combination of multiple letters and numbers, such as “var1.” However, a variable’s
name cannot be started with a number; for example, “1var” is not a valid variable
name. In addition, special characters such as “@,” “#,” “$,” “&,” and “-” cannot be
included in a variable’s name. Underscore can be allowed to be used in the variable’s
name except at the starting place. For example, “var_1” is a valid variable name, but
“_var” is invalid in MATLAB.

1.8.1 Categories of Data Types

The mostly used categories of data types of variables can be listed as follows:
(a) Numeric data type: integer, float (single and double), and logical
(b) Character and string type: character, string, and cell array
(c) Date and time
An example of variable definition for each of the above-mentioned data types is
given below using MATLAB.

1.8.2 MATLAB Example 1.1: Different Data Types

The MATLAB code demonstrating different data types is given in Fig. 1.4, and its
output is given in Fig. 1.5.
10 1 Introduction to MATLAB

Fig. 1.4 Code—Different data types in MATLAB

Fig. 1.5 Output—Different data types in MATLAB


1.8 Variables in MATLAB: Categories and Conversion Between Variables 11

Output:
In MATLAB whos command can be used to print the details of all the defined
variables stored in the Workspace. More precisely, it provides the name, size, bytes,
class, and attributes of the defined variables. Here, the class indicates the data types
of the variable. For example, the class of var1 is int8, which signifies that var1 is a
positive signed 8-bit integer. The data types of the rest of the variables can also be
found from the above-mentioned output.
After defining a variable, the assigned data of the variable can be reused any time
throughout the program, unless the variable is reassigned with new data, or cleared
by MATLAB command. To erase any defined variable from MATLAB, the clear
command can be utilized. Variables can also be cleared by selecting them in the
Workspace window and selecting the delete option.

• If you want to erase all variables from the MATLAB memory, type
clear or clear all
• To erase a specific variable, say x, type clear x
• To clear two specific variables, say x and y, type clear x y
• To clear only the command window, type clc

1.8.3 Conversions

The data types of the defined variables can be converted in MATLAB. The most
used conversions that are required are number-to-text conversion and text-to-number
conversion. The commands for such conversions are listed in Table 1.4.
A MATLAB example is given below for further illustration.

Table 1.4 MATLAB functions for data type conversion


Number to text Text to number
• int2str(number) • str2num(text)
Convert any integer number to a character. Convert any character or string into a double
Here, number indicates any integer number number. Here, text indicates any character or
• num2str(number) string
Convert any number to a character. Here, • str2double(text).
number indicates any number Convert any character or string into a double
• char(number) number. Here, text indicates any character or
Convert any number to a character. Here, string
number indicates any number
• string(number)
Convert any number to a string. Here,
number indicates any number
12 1 Introduction to MATLAB

1.8.4 MATLAB Example 1.2: Conversion of Data Types

The MATLAB code demonstrating the conversion of data types is given in Fig. 1.6,
and its output is given in Fig. 1.7.
Output:
Here, all the conversions can be verified by observing the “Class” of all the
variables.

Fig. 1.6 Code—Conversion of data types in MATLAB


1.9 Suppressing Output 13

Fig. 1.7 Output—Conversion of data types in MATLAB

Fig. 1.8 Suppressing output in MATLAB

1.9 Suppressing Output

In MATLAB, after each command, a semicolon is used to suppress the output.


Without a semicolon, the output of the command will be printed on the command
window. In general, we normally use a semicolon at the end of every line of code,
except the final output, or the results we want to see in the command window.
An example of MATLAB code with and without the usage of the semicolon is
shown in Fig. 1.8 to understand the distinction between them:
14 1 Introduction to MATLAB

Here, in the first example, a vector a has been defined without using a semicolon.
As an outcome, the value of a is printed in the command window. In the second
example, with the usage of the semicolon, the output is suppressed.

1.10 Recording a MATLAB Session

In MATLAB, diary function can be useful while recording a MATLAB session that
creates a file containing the keyboard inputs and the outputs. The MATLAB
command for the usage of the diary function is as follows:

MATLAB command for recording a session:

diaryð0 Name0 Þ

Here, 0Name0 indicates the name of the file in which the session will be
recorded.

A MATLAB example is provided in Fig. 1.9 with its output in Fig. 1.10 for
further illustration.
Output:
Here the output including the keyboard inputs is recorded in a text file named
“DiaryFile.txt,” as shown above in the output. To record a particular part of the
MATLAB session, diary on and diary off command placed at the start and the end of
a session, respectively, can be very useful. Later, the diary(‘Name’) command can be
used to define the file name on which the recorded version will be saved.

Fig. 1.9 Code—diary function in MATLAB


1.11 Printing Output 15

Fig. 1.10 Output—diary function in MATLAB

1.11 Printing Output

The output of the MATLAB program can be printed in the command window by
using two MATLAB built-in functions fprintf() and disp(). It provides the users great
flexibility to choose which output they want to print. Both fprintf() and disp()
commands are explained below:

MATLAB fprintf command printing:

fprintf ð0 text 0 Þ

fprintf ð0 text%d0 , var Þ

fprintf ð0 text% f 0 , var Þ

fprintf ð0 ∖n0 Þ

Here, 0text0 can be any string or character, and var indicates the value of a
variable that is required to be printed. Finally, 0\n0 is used to shift to the
next line.

fprintf() can be used to print both text and numbers in the command window. The
input provided within the bracket will be printed in the command window as a string,
or characters. If the defined value of any variable is required to be printed with text,
the fprintf(0text % d0, var) command can be utilized where %d will be replaced by the
value of the defined variable var. Here var indicates the name of the variable.
However, it is only true if the variable is an integer type. For printing the values of
float type variables, %f is used in place of %d. To shift to a new line, \n is used.
16 1 Introduction to MATLAB

Fig. 1.11 Code—fprintf function in MATLAB

Fig. 1.12 Output—fprintf function in MATLAB

A MATLAB example regarding the usage of the fprintf() is provided in Fig. 1.11
with its output in Fig. 1.12.

Output:

Here, in the first fprintf() command, a text is printed. In the second command, \n is
used to print the next output in a separate line. In the third command, an integer
variable named var is printed with text. Finally, in the fourth command, a float type
variable printing with text is shown.
disp() is another MATLAB command for printing output. The usage of disp()
command is shown below:

MATLAB ‘disp’ command printing:

dispð0 text 0 Þ

dispðvar Þ

dispð½0 text 0 , num2str ð var ÞÞ

Here, 0text0 can be any string or character, and var indicates the value of a
variable that is required to be printed.

In disp(), a new line shift occurs by default; hence, \n notation, as used in fprintf(),
is not required to use here. To print both strings and variables, it is necessary to
convert the data types of the variables in a string format while using disp(). In the
1.12 Conclusion 17

Fig. 1.13 Code—disp function in MATLAB

Fig. 1.14 Output—disp function in MATLAB

above example, num2str() function is used to convert the data types of the variable in
string format. An example of the usage of disp() is shown in Fig. 1.13 with its output
in Fig. 1.14 for better understanding.

Output:

1.12 Conclusion

This chapter provides a brief introduction to MATLAB and presents the history,
purpose, and importance of MATLAB. The concept of MATLAB is put forward
such that the chapter can be interesting for a new audience. To help the readers
getting started with MATLAB with a hand-in experience, the chapter provides step-
by-step methods of its installation procedures. The readers are advised to implement
all the examples and coding in MATLAB simultaneously to understand the contents
more rigidly. To accomplish this purpose, this chapter introduces the overall
MATLAB environment along with some fundamental features, so that the readers
can feel comfortable writing MATLAB code while going through the rest of
the book.
18 1 Introduction to MATLAB

Exercise 1

1. Write down some of the notable applications of MATLAB in engineering.


2. What are the major data types in MATLAB? How are they represented in
MATLAB programming?
3. Mention the usage of the following commands/functions with examples where
applicable:
(a) clc
(b) num2str()
(c) str2double()
(d) int8()
(e) disp()
4. Perform the following operations in MATLAB and save the results in a variable.
Demonstrate the variables using “whos” command:
(a) 2*4^2
(b) (2*4)^2
(c) 503+224604
(d) (10^3)/(9*2)
(e) 6.25*0.42^3.56
(f) Save “MATLAB is fun!” in a variable
5. Take two numerical inputs from the user and save them in variables num1 and
num2. Perform the following operations and record the session using the diary
function:
(i) num1/num2
(ii) num1\num2
Do they produce the same result? If not, why?

Reference

1. https://www.mathworks.com/help/install/

You might also like