0% found this document useful (0 votes)
3 views

Introduction to MATLAB - 01

The document outlines a Bridge/Mandatory Course on Introduction to MATLAB and Simulink, held from October 22 to October 12, 2022, at VIT Bhopal University. It includes course details, instructors' expertise, prerequisites for students, and a structured schedule covering various MATLAB functionalities and applications. The course aims to equip students with foundational knowledge in MATLAB programming, data visualization, and Simulink for circuit design and simulation.

Uploaded by

Piyush Bharti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Introduction to MATLAB - 01

The document outlines a Bridge/Mandatory Course on Introduction to MATLAB and Simulink, held from October 22 to October 12, 2022, at VIT Bhopal University. It includes course details, instructors' expertise, prerequisites for students, and a structured schedule covering various MATLAB functionalities and applications. The course aims to equip students with foundational knowledge in MATLAB programming, data visualization, and Simulink for circuit design and simulation.

Uploaded by

Piyush Bharti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

22nd -12th October 2022 Bridge/Mandatory Course on

Introduction to MATLAB and


Simulink

School of Electrical & Electronics Engineering

Dr. Susant Kr. Panigrahi


Assisant Professor, SEEE
VIT Bhopal University
Getting Started with MATLAB. 2

Course Instructors
Dr. Susant Kumar Panigrahi Dr. Anirban Bhowmick
Assistant Professor (SEEE) Assistant Professor Sr. (SEEE)
PhD, NIT Rourkela PostDoc, IISc Bangalore
Area of Expertise: Inverse Problems in Image Area of Expertise: Speech and Audio
Processing, Sparse Signal Representation and Processing, Speech Enhancement, Speech
Analysis, Computer Vision, Embedded System Recognition, Language identification and
Design and Programming. Audio Event Detection
Email: [email protected] Email: [email protected]

Mr. Kundan Kumar Dr. Om Prakash Pahari


Teaching Fellow (SEEE) Assistant Professor (SEEE)
PhD (Thesis Submitted), IIT Patna PhD, NIT Rourkela
Area of Expertise: State Estimation, Kalman Area of Expertise: Systems and Control,
Filtering, Target Tracking, Interacting Multiple Grid Integration of Renewable Energy.
Model, Event-triggered Estimation.

Email: [email protected] Email: [email protected]


Getting Started with MATLAB.

Pre-Requisite
3

Students are requested to create their respective accounts in MATLAB for online coding
and simulation:

https://in.mathworks.com/products/matlab-online.html
Click here to create your
own account

Bridge Course Organized By: School of Electrical and Electronics Engineering


Getting Started with MATLAB. 4

Click here to fill all the


details for new account.

Students are advised to use


VIT Bhopal Email id.

Bridge Course Organized By: School of Electrical and Electronics Engineering


Students are advised to use
VIT Bhopal Email id.

Fill all the details and click


on create.
After this check your email for more details regarding logging procedure of your created account.

Bridge Course Organized By: School of Electrical and Electronics Engineering


Check you email and follow the steps to get
access to Online MATLAB.

Bridge Course Organized By: School of Electrical and Electronics Engineering


Click back here and login
with your email id and
password.

Click here to start online


MATLAB simulator.

Bridge Course Organized By: School of Electrical and Electronics Engineering


Course Content Getting Started with MATLAB. 8

Getting Started with MATLAB. Dr. Susant K. Panigrahi,


 Basic Features Assistant Prof., SEEE, VIT
Day 1  Various Environments Bhopal
Sept. 22nd 2022 MATLAB Simulink. Circuit design
 A Minimum MATLAB Session
and Simulation tool – I Dr. Om Prakash Pahari,
 Using MATLAB as a calculator Day 8
 Basics of Simulink. Assistant Prof., SEEE,
Oct. 3rd 2022
Basic Syntax and Commands. Dr. Susant K. Panigrahi,  Circuit Simulation VIT Bhopal
 Most Commonly used MATALAB Assistant Prof., SEEE, VIT
Day 2 commands Bhopal MATLAB Simulink. Circuit design
Sept. 23rd 2022  Variable types Day 9 Dr. Om Prakash Pahari,
and Simulation tool – II
 Variable Formatting Oct. 4th 2022 Assistant Prof., SEEE,
 Modelling in Simulink
 Strings VIT Bhopal
Mr. Kundan Kumar, Assistant MATLAB Simulink. Circuit design
Day 3 Linear Algebra (working with Matrix and Day 10 Dr. Om Prakash Pahari,
Prof., SEEE, VIT Bhopal and Simulation tool – III
Sept. 26th 2022 Vectors). Oct. 6th 2022 Assistant Prof., SEEE,
 Dynamic Systems in Simulink
VIT Bhopal
Mr. Kundan Kumar, Assistant
Day 4 Loops and Conditional Statements
Prof., SEEE, VIT Bhopal Certification (Simulink
Sept. 27th 2022 Creating ‘. 𝒎’ files. Day 11 Dr. Om Prakash Pahari,
Oct. 7th 2022 Assistant Prof., SEEE,
Onramp)
Data Visualization. VIT Bhopal
Dr. Susant K. Panigrahi,
Day 5  Basics of Plotting
Assistant Prof., SEEE, VIT Day 12 Symbol Space – I Dr. Anirban Bhowmick,
Sept. 28th 2022  2D Plots
Bhopal Oct. 10 2022 Introduction
th Assistant Prof., SEEE,
 Formatting and Managing 2D plots
VIT Bhopal

Data Visualization. Symbol Space – II


Dr. Susant K. Panigrahi, Day 13 Dr. Anirban Bhowmick,
Day 6  3D Plotting Detailed: Solving Linear Equation and
Assistant Prof., SEEE, VIT Oct. 11th 2022 Assistant Prof., SEEE,
Differential Equations.
Sept. 29th 2022  Pie Chart, Histogram and Few other plots Bhopal VIT Bhopal
 Reading and Displaying an Image Concluding Remarks
Day 14 Scope and Applications (Signal and Dr. Anirban Bhowmick,
Oct. 12 2022 Image Processing and Beyond)
th Assistant Prof., SEEE,
Day 7 Certification (MATLAB Dr. Susant K. Panigrahi, Quiz VIT Bhopal
Assistant Prof., SEEE, VIT
Sept. 30th 2022 Onramp) Bhopal
Bridge Course Organized By: School of Electrical and Electronics Engineering
Overview on MATLAB
MATLAB is a programming and numeric computing
9 platform used by millions of engineers and scientists
to analyze data, develop algorithms, and create
models.
MATLAB Capabilities

Pic Courtesy: Mathworks®


MATLAB Capabilities

Pic Courtesy: Mathworks®


MATLAB Environments

11

MATLAB (.m) Simulink (.mdl) Graphical User


Programing/Coding Interface (GUI)
Programming (.m & .fig)
Functions
Blocks

Structures

https://matlab.mathworks.com/ Pic Courtesy: Mathworks®


Getting Started with MATLAB. 12

Brief History
 The name MATLAB stands for MATrix LABoratory.

 The first MATLAB was not a programming language; it was a simple


interactive matrix calculator, there were no Programs, no graphics and no Cleve Moler, Chief
ODEs or FFTs. Mathematician
Cleve Moler is chief mathematician,
chairman, and cofounder of
MathWorks.
 MATLAB was written to originally to provide easy access to matrix software
developed by the LINPACK and EISPACK projects

 MATLAB is a high-performance language for technical computing. It


integrates computation, visualization, and programming environment.
Furthermore
Jack Little, CEO
Jack Little is CEO and cofounder of MathWorks. He was a
coauthor and principal architect of early versions of the
Bridge Course Organized By: School of Electrical and Electronics Engineering company's flagship MATLAB
Starting MATLAB Getting Started with MATLAB. 13
Change Layout
Get Simulink View of MATLAB
Action Menu Window clicking here. Search MATALB
here command
documents here

MATLAB Help
View or Change Current Folder or Dictionary

Current folder MATLAB Script (.m) files


This folder at now. Code is written in this file and Workspace
stored in Current folder Stored current and
previous variables

Command Window
Run single line of code and verify
Details
About the selected item from
the current folder.
Bridge Course Organized By: School of Electrical and Electronics Engineering
Variables Getting Started with MATLAB. 14

 Have not to be previously declared

 Variable names can contain up to 63 characters

 Variable names must start with a letter followed by letters, digits, and underscores( Valid:

x1 = 5, Invalid: 1x = 5)

 Variable names are case sensitive (a1 does NOT equal A1)

Bridge Course Organized By: School of Electrical and Electronics Engineering


Getting Started with MATLAB. 15

Commonly used MATLAB data types


'single' Single-precision number Examples
'double' Double-precision number (Default)
'int8' Signed 8-bit integer
'int16' Signed 16-bit integer
'int32' Signed 32-bit integer
'int64' Signed 64-bit integer
'uint8' Unsigned 8-bit integer
'uint16' Unsigned 16-bit integer
'uint32' Unsigned 32-bit integer
'uint64' Unsigned 64-bit integer
'logical' Logical 1 (true) or 0 (false)
'char' Character
'string' String array

Bridge Course Organized By: School of Electrical and Electronics Engineering


Getting Started with MATLAB. 16

Memory and Changing Types


Class (Data Type) Bytes Supported Operations
single 4 Most math Examples: Memory
double 8 All math

logical 1 Logical/conditional operations

int8, uint8 1 Arithmetic and some simple


functions
int16, uint16 2 Arithmetic and some simple
functions
int32, uint32 4 Arithmetic and some simple
functions
int64, uint64 8 Arithmetic and some simple
functions

Examples: Changing Variable Types

Q1. Find the memory of a1, a2 and a3.

Bridge Course Organized By: School of Electrical and Electronics Engineering


MATLAB Operators
Getting Started with MATLAB. 17

Bridge Course Organized By: School of Electrical and Electronics Engineering

 EX: Using command window


 >> x = 5; % this is used as a comment Name Operator Operation
 >> y = 3;
Assignment = a = b (assign b to a)
 >> z = x + y
z=8
Addition + a+b
 Semicolon (;) : Suppresses output
 Percentage (%) : Commenting. Only good for Subtraction - a-b
that line Multiplication * or.* a*b or a.*b
Examples: Memory Division / or ./ a/b or a./b

Power ^ .^ a^b or a.^b


Dot power and Power are
different

You will have noticed that if you do not specify an output variable, MATLAB uses a default
variable ans, short for answer, to store the results of the current calculation.
Q2. Find the variable type of ‘ans’. Do the same operation by changing variable type to unit8 and assign a
variable name X to hold the answer. Compare the memory size of ‘ans’ and X.
Using MATLAB as a Calculator
Getting Started with MATLAB. 18

 Parenthesis takes first preference and It gets evaluated first.


 Then multiplication operation is executed.

Examples: Use of Parenthesis

Bridge Course Organized By: School of Electrical and Electronics Engineering


Using MATLAB as a Calculator
Getting Started with MATLAB. 19

Q3. Do the following operations and check the corresponding answers.

a) 6 x 4 ÷ 12 + 72 ÷ 8 – 9

b) ((6 x 4) ÷ 12) + (72 ÷ 8) – 9

c) uint8(((6 x 4) ÷ 12) + (72 ÷ 8) – 9)

d) uint8(6) x uint8(4) ÷ unit8(12) + uint8(72) ÷ uint8(8) – uint8(9)

e) uint8(6) x uint8(4) ÷ unit8(12) + uint8(72) ÷ uint8(8) – 9

f) uint8(6) x uint8(4) ÷ unit8(12) + uint8(72) ÷ uint8(8) – uint16(9)

Bridge Course Organized By: School of Electrical and Electronics Engineering


Error messages
Getting Started with MATLAB. 20

 If we enter an expression incorrectly, MATLAB will return an error message.

 For example, in the following, we left out the multiplication sign, *, in the following expression

There are two main types of errors that can be introduced while
programming in any language:
1. Syntax Errors
2. Algorithmic Errors
1. Syntax Errors:

A syntax error occurs when the calling syntax you use for a function is incorrect, or when you
provide the function with inputs that are of the wrong shape, size, and/or type, or are otherwise not valid
for the function in question. A typo, or typographic error, could be considered as a syntax error.
Bridge Course Organized By: School of Electrical and Electronics Engineering
Error messages
Getting Started with MATLAB. 21

1. Syntax Errors: Examples

Assignment Error

Parenthesis Error

Miscellaneous Error

Bridge Course Organized By: School of Electrical and Electronics Engineering


Error messages
Getting Started with MATLAB. 22

2. Arithmetic Errors: Examples


Usually these are self-explanatory. As a reminder, here are some common functions that
cannot be performed and what MATLAB returns (along with a warning for each one):

“NaN” (Know as Not a Number):

Other Arithmetic Errors: Will very often result in errors or useless results unless measures
are taken to avoid propagating them.

So check the dimensions of all the terms in your expression.

This error often occurs because of indexing issues OR because


you meant to use component-wise multiplication but forgot the dot.

This is usually because you meant to use component-wise exponentiation


and forgot the dot.
Bridge Course Organized By: School of Electrical and Electronics Engineering
Variable ‘Format’ Function
Getting Started with MATLAB. 23

MATLAB® displays output in both the Command Window and the Live Editor.
Style Result Example
Examples:
short Short, fixed-decimal format with 4 digits after 3.1416
the decimal point. This is the default
numeric setting.
long Long, fixed-decimal format with 15 digits 3.141592653589793
after the decimal point for double values, and
7 digits after the decimal point
for single values.
shortE Short scientific notation with 4 digits after the 3.1416e+00
decimal point.
longE Long scientific notation with 15 digits after 3.141592653589793e+00
the decimal point for double values, and 7
digits after the decimal point for single values.
shortG Short, fixed-decimal format or scientific 3.1416
notation, whichever is more compact, with a
total of 5 digits.
longG Long, fixed-decimal format or scientific 3.14159265358979
notation, whichever is more compact, with a
total of 15 digits for double values, and 7
digits for single values. Bridge Course Organized By: School of Electrical and Electronics Engineering
Getting Started with MATLAB. 24

Most Commonly used MATALAB Commands


1. clc
Clear Command Window

clc clears all the text from the Command


Window, resulting in a clear screen. After
running clc, you cannot use the scroll bar in
the Command Window to see previously
displayed text

Bridge Course Organized By: School of Electrical and Electronics Engineering


Getting Started with MATLAB. 25

Most Commonly used MATALAB Commands


2. clear
Remove items from workspace, freeing up system memory

Examples:

Bridge Course Organized By: School of Electrical and Electronics Engineering


Getting Started with MATLAB. 26

Most Commonly used MATALAB Commands


3. Whos
Syn. & Def. whos

Returns a string. Here variable will be ‘s’ which is assign to the sting.

Example -- s = whos (): returns structure array with fields: name, size, bytes, class, global,
sparse, complex, nesting, persistent.

Examples:

Bridge Course Organized By: School of Electrical and Electronics Engineering


Getting Started with MATLAB. 27

Most Commonly used MATALAB Commands


4. Length

Syn. & Defn -- numberOfElements = length (array)

Finds the number of elements along the largest dimension of an


array.

Example -- x= [1, 8, 3, 5];


y=length(x)
y=4: here the length function gives length of the array.

Create a string array and compute its length, which is the number of
elements in each row.

Bridge Course Organized By: School of Electrical and Electronics Engineering


Getting Started with MATLAB. 28

Most Commonly used MATALAB Commands


5. Save

Saves workspace variable to file.

save(filename) – stores all variables from the current workspace in a MATLAB formatted binary file (MAT-file)
called filename.

Example -- save(filename, variables) stores only the specified variables.

Bridge Course Organized By: School of Electrical and Electronics Engineering


Getting Started with MATLAB. 29

Most Commonly used MATALAB Commands


6. Linespec (Line Specification):

Generate linearly spaced vector.

The linspace function generates linearly spaced vectors. It is similar to the colon operator ":", but gives direct
control over the number of points.

y = linspace(a,b) generates a row vector y of 100 points linearly spaced between a and b.
y = linspace(a,b,n) generates n points.

% create an array from 1 to 10 with 5 values


values = linspace(1, 10, 5)

ans =
1.0000 3.2500 5.5000 7.7500 10.0000

% create a matrix starting at 0 going to 20 with 100 values


values = linspace(0,20,100)

Bridge Course Organized By: School of Electrical and Electronics Engineering


Getting Started with MATLAB. 30

Most Commonly used MATALAB Commands:


At a Glance
Functions/Co
Syntax Definition Example
mmands
List only those variables specified in ‘vars’, S = whos (): Returns structure array
where ‘vars’ is a comma-delimited list of with fields: name, size, bytes, class,
whos S = whos (vars) quoted strings: 'var1', 'var2', ..., 'varN' global, sparse, complex, nesting,
persistent.
Input: x= [1, 8, 3, 5];
Finds the number of elements along the
length NoOfElements = length (X) largest dimension of an array.
y=length(x);
Output: y=4

OldFolder = cd(newFolder): Returns


the existing current folder as a string
Changes the current folder to the string
cd OldFolder = cd (newFolder) ‘newFolder’.
to OldFolder, and then changes the
current folder to newFolder.

S = load (filename, variables):


load S = load (filename) It loads data from MAT file into workspace. Loads the specified variables from a
MAT-file.

save (‘filename’): Stores all variables from


the current workspace in a MATLAB save(‘filename’,’variable’): Stores
save(‘filename’) ‘or’
save formatted binary file (MAT-file) called only the specified variables.
save(‘filename’, ’variable’) filename.

Bridge Course Organized By: School of Electrical and Electronics Engineering


Functions/Co Getting Started with MATLAB. 31
Syntax Definition Example
mmands
Most Commonly used MATALAB Displays an array, without printing the
disp disp(X) array name.

Input: format long


Pi
Output: 3.141592653589793
Styles: short, long, short e,
Commands: At a Glance

format etc…
Sets format for output to display.
Input: format short
Pi
Output: 3.1416

Returns at most the first k indices


ind = find(X) ‘or’
find ind = find(X, k, 'first')
Locates all nonzero elements of array X. corresponding to the nonzero
entries of X.

Returns largest value among the array


max max(X)
of different dimension

Returns smallest value among the array


min min(X)
of different dimension.

Input: X = [2, 5, 8, 3, 1, 10]


sort Y = sort(X) Sorts the elements of x. Y = sort(X)
Output: Y = [1, 2, 3, 5, 8,10]
Bridge Course Organized By: School of Electrical and Electronics Engineering
Getting Started with MATLAB. 32

Functions/Co
Most Commonly used MATALAB Syntax Definition Example
mmands
ones(n): Returns n x n matrix with
each element of value 1.
X = ones(n) ‘or’
ones ones(n,m):returns n x m matrix
Y = ones(m,n) with each element of value 1.
Commands: At a Glance

ones(n): Returns n x n matrix with


X = zeros(n) ‘or’ each element of value 0.
zeros
Y = zeros(m,n) ones(n,m):returns n x m matrix
with each element of value 0.
cell(n): Returns n x n empty matrix.
Cell matrix is matrix wich contain
X = cell(n) ‘or’ cell(n,m): Returns n x m empty
cell matrix within a matrix. (See
Y = cell(m,n) matrix.
MATLAB help)

Input: a = [2, 5, 8, 3, 1, 10]


X = num2cell(a)
Converts every elements of array to
num2cell X = num2cell(a) Output : X = [2] [5] [8] [3]
cell.
[1] [10]

ones(n): Returns n x n matrix with


each element of value 1.
X = ones(n) ‘or’
ones ones(n,m):returns n x m matrix
Y = ones(m,n) with each element of value 1.
Bridge Course Organized By: School of Electrical and Electronics Engineering
Getting Started with MATLAB. 33
Functions/Co
Syntax Definition Example
Most Commonly used MATALAB mmands

Input: X = 3+j*4
Returns absolute value and complex
abs Y = abs(X) Y = abs(X)
magnitude.
Output: Y = 5.

Input: X = 3+j*4
Commands: At a Glance

angle P = angle(X) Returns phase angle. P = abs(X)


Output: P = 0.9273Radian.

Input: A = [2, 5, 8, 3, 1, 10]


Returns average value or mean value of a
mean M = mean(A) M = mean(A)
vector.
Output: M = 4.3833.

Input: X = [2, 5, 8, 3, 1, 10]


Retunes standard deviation of random
std S = std(X) S = std(X)
variable.
Output: S = 3.5449.
R = rand(n): returns an n-by-n
R = rand(n) ‘or’ Generate random values between ‘0’ to ‘1’ matrix.
rand R = rand(m,n) in double format. R = rand(m,n): returns an m-by-n
matrix.

Returns a random integer on the interval Input: R = randi([2,30],1,10)


randi R = randi([imin,imax],m,n) [imin:imax]. Output: R = [22, 2, 10, 3, 4, 25, 22, 11,
29, 2]

Bridge Course Organized By: School of Electrical and Electronics Engineering


Getting Started with MATLAB. 34
Functions/Co
Syntax Definition Example
Most Commonly used MATALAB mmands
Input: R = randperm (10)
Returns a random permutation of
randperm R = randperm(n) Output: R = [7, 4, 8, 2, 6, 10, 9,
the integers [1:n].
3,1, 5]

Input: B = ceil (10.5)


Commands: At a Glance

ceil B = ceil(A) Round toward positive infinity.


Output: B = 11

Input: B = floor (10.5)


floor B = floor(A) Round toward negative infinity.
Output: B = 10

Rounds the elements of A to the Input: B = round(10.5)


round B = round(A)
nearest integers. Output: B = 11

Compares the strings str1 and str2


and returns logical 1 (true) if they Input: TF = strcmp('A', 'B')
strcmp TF = strcmp('str1', 'str2')
are identical, and returns logical 0 Output: TF = 0
(false) otherwise.
Input: s1 = ‘SU’
Horizontally concatenates strings in s2 = ‘SANT’
strcat CStr = strcat(s1, s2, ..., sN)
arrays s1, s2, ..., sN. CSrt = strcat(s1,s2)
Output: CSrt = SUSANT
Bridge Course Organized By: School of Electrical and Electronics Engineering

You might also like