The document provides an introduction to MATLAB and Simulink. It describes MATLAB as a numerical computing environment and matrix laboratory that is used for data analysis, algorithm development, modeling, and more across many disciplines. Simulink is introduced as a block diagram environment for multi-domain simulation and model-based design. Key features and uses of MATLAB and Simulink are outlined, including acquiring and analyzing data, developing functions and algorithms, modeling and simulation.
2. • “The Language of Technical Computing”
• Numerical Programming Environment
• MATLAB - MATrix LABoratory
• High-Level Interpreted Language
• Uses:
• Analyze Data
• DevelopAlgorithms
• Create Models andApplications.
• MultidisciplinaryApplications
Introduction to MATLAB and Simulink
3. • Acquire and Analyze Data from different sources
• Data from Measuring and Sensing Instruments
• Recorded Data (Spreadsheets, text files, images, audio files, etc)
• Analyze Data using different tools
• Develop Functions andAlgorithms
• Visualize Data in terms of graphs, plots, etc
• Simulink is used to Develop Models andApplications
• Deploy Code as StandaloneApplications
Introduction to MATLAB and Simulink
4. • MATLAB is a Multi-discipinary Tool
• Can be used in any Numerical ComputationApplication
• 90+ Toolboxes in multiple fields
• Mathematics(Symbolic Math, Statistics, Curve fitting, Optimization)
• Communications & Signal Processing (RF, LTE, DSP, Wavelets)
• Machine Vision (Image Processing, Computer Vision)
• Control Systems (Fuzzy Logic, Predictive Control, Neural Networks)
• Parallel Computing and Distributed Computing
• Statistics and Curve Fitting
• Computational Finance ( Financial, Econometrics, Trading, etc)
• Instrument Control, Vehicle Networks (CAN) , Aerospace
Introduction to MATLAB and Simulink
5. • Simulink is a Block Diagram Environment for Multidomain
simulation and Model-Based Design.
• Build, Simulate and Analyze models using Blocks.
• Connect to External Hardware (FPGA, DSP Processors,
Microprocessor, Microcontroller, etc) and run the models there
directly.
• Simscape (Physical Systems – Mechanical, Electrical, Hydraulic, etc)
• SimMechanics ( Robotics, Vehicle Suspensions, HIL system support)
• SimDriveline (1-D Driveline System Simulation)
• SimHydraulics (Hydraulic Components)
• SimRF (RF Systems)
• SimPowerSystems (Electrical Power Systems)
• SimElectronics (Motors, Drives, Sensors, Actuators, etc)
Introduction to MATLAB and Simulink
6. • MATLAB has optimized mathematical algorithms which
perform mathematical operations very efficiently.
• High speed of computation.
• Easy to learn and write MATLAB code.
• Tons of built-in code and freely available User-submitted code.
• Simulink uses Block approach with Drag-And-Drop.
• Easy to use and implement models.
• No hassle deployment of same model to multiple devices
Introduction to MATLAB and Simulink
7.
8. Introduction to MATLAB and Simulink
• Command Window
• Type commands
• Current Directory
• View folders and m-files
• Workspace
• View program variables
• Double click on a variable
to see it in the ArrayEditor
• Command History
• View Past Commands
• Save a whole
session using
Diary
9. • MATLAB works primarily (almost exclusively) with matrices.
• MATLAB functions are optimized to handle matrix operations.
• MATLAB can handle upto 13-dimensional matrices.
• Loops can be vectorized for faster operations.
Introduction to MATLAB and Simulink
10. • Matrix is a one- or multi-dimensional array of elements.
• Elements can be numerical, variables or string elements.
• By default, MATLAB stores numbers as double precision.
• ALL data in MATLAB are viewed as matrices.
• Matrices can be:
• Created Manually by User
• Generated by MATLAB Functions
• Imported from stored databases or files
Introduction to MATLAB and Simulink
11. • Unlike C, MATLAB is an interpreted language. So, there is no
need for Type Declaration.
• A single variable is interpreted as 1x1 matrix.
>> a = 5
a =
5
• Arrays are represented as a series of numbers (or characters)
within square brackets, with or without a comma separating the
values.
>> b = [1 2 3 4 5] % Percentage Symbol indicates Comment
b =
1 2 3 4 5
Introduction to MATLAB and Simulink
12. • 2-D or Multidimensional Arrays are represented withinsquare
brackets, with the ; (semicolon) operator indicating end of a
row.
>> c = [1 2 3 ; 4 5 6 ; 7 8 9 ; 10 11 12]
c =
1 2 3
4 5 6
7 8 9
10 11 12
• c is now a 2-D array with 4 rows and 3 columns
Note : Variable names are case sensitive and can be upto 31 characters long, and have
to start with an alphabet.
Introduction to MATLAB and Simulink
13. • Character strings are treated as arrays too.
>> name = 'Ravi’
is the same as
>> name = [‘R’ ‘a’ ‘v’ ‘i’]
And gives the output:
name =
Ravi
• Strings and Characters are both declared within SINGLE
quotes (‘ ’)
Introduction to MATLAB and Simulink
14. 1 2 3
4 5 6
• Unlike in case of C, MATLAB array indices start from 1.
>> d = [1 2 3 ; 4 5 6]
d =
• Addressing an element of the array is done by invoking the
element’s row and column number.
• In order to fetch the value of an element in the 2nd row and 3rd
column, we use:
>> e = d(2,3)
e =
6
Introduction to MATLAB and Simulink
15. • Rather than addressing single elements, we can also use
commands to address multiple elements in an array.
• The ‘:’ (colon) operator is used to address all elements in arow
or column.
• The ‘:’ operator basically tells the interpreter to addressALL
elements.
• The ‘:’ operator can also be used to indicate a range ofindices.
Introduction to MATLAB and Simulink
16. • Consider the earlier example: d = [1 2 3; 4 56]
•>> f = d(1, :) % Address All elements of 1st Row
f =
% Address All elements in 2nd Column
1 2 3
• >> g = d(:,2)
g =
2
5
%Address Rows from 1 to 2 and Columns from 1 to 2
>> h = d(1:2,1:2)
h =
1 2
4 5
Introduction to MATLAB and Simulink
17. • In some cases, we need to generate large matrices, which is difficult
to generate manually.
• There are plenty of built-in commands for this purpose!
• >> i = 0:10 % Generate numbers from 0 to 10 (Integers)
i =
0 1 2 3 4 5 6 7 8 9 10
• >> j = 0:0.2:1 % Generate numbers from 0 to 1, in steps of 0.2
j =
0 0.2000 0.4000 0.6000 0.8000 1.0000
• >> k = [1:3; 4:6;7:9] % Generate a 3x3 matrix of numbers 1 through 9
k =
1 2 3
4 5 6
7 8 9
Introduction to MATLAB and Simulink
18. • >> l = ones(3,2) %Generate a 3x2 matrix populated with 1s
l =
1 1
1 1
1 1
• >> m = zeros(2,4) %Generate a 2x4 matrix of 0s
m =
0 0 0 0
0 0 0 0
% Generate a 3x4 matrix of random numbers (Between 0 and 1)• >> n = rand(3,4)
n =
0.8147 0.9134 0.2785 0.9649
0.9058 0.6324 0.5469 0.1576
0.1270 0.0975 0.9575 0.9706
Introduction to MATLAB and Simulink
19. • x = linspace(a,b,n) % Generates n linearly-spaced values between a and b
(inclusive)
>> x = linspace(0,1,7)
x =
0 0.1667 0.3333 0.5000 0.6667 0.8333 1.0000
• x = logspace(a,b,n) % Generates n values between 10a and 10b in logarithm
space
>> x = logspace(0,1,7)
x =
1.0000 1.4678 2.1544 3.1623 4.6416 6.8129 10.0000
Introduction to MATLAB and Simulink
20. • Operations upon Matrices can be of two types:
• Element-wise Operation
• Matrix-wise Operation
• Common Arithmetic Operations:
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
• Exponentiation (^)
• Matrix Inverse (inv)
• Left Division () [AB is equivalent to INV(A)*B]
• Complex Conjugate Transpose (’)
Introduction to MATLAB and Simulink
21. • By default, the Operators perform Matrix-wise operations.
• During Matrix-wise operations, care must be taken to avoid
dimension mismatch, specially with exponentiation, division
and multiplication.
• In case of scalar + matrix operations, matrix-wise operations are
equivalent to element-wise operations.
• ie.
Scalar + Matrix = [Scalar + Matrix(i,j)]
Scalar * Matrix = [Scalar * Matrix(i,j)]
• A dot operator(.) preceding the operator indicatesElement-wise
operations.
Introduction to MATLAB and Simulink
22. • Let a = [2 5; 8 1];
b = [1 2 3; 4 5 6];
c = [1 3; 5 2; 46];
% 2 x 2 Matrix
% 2 x 3 Matrix
% 3 x 2 Matrix
2 5 8
6 9 12
• Matrix Addition (or Subtraction):
>> y = b+c' % b and c have different dimensions.
y =
• Complement:
>> d = c' % d is now a 2x3 matrix
d =
1 5 4
3 2 6
Introduction to MATLAB and Simulink
23. • Matrix Multiplication (Or Division):
>> x = b*c % b(2x3) * c(3x2) = y (2x2). No dimension mismatch
x =
23 25
53 58
• In case of element-wise multiplication, the corresponding
elements get multiplied (Matrix Dimensions must agree)
% b(2x3)*c(2x3). No dimension mismatch)>> y = b .* d
y =
1 10 12
12 10 36
Introduction to MATLAB and Simulink
24. • Element-wise Exponentiation is NOT the same as Matrix-wide
exponentiation.
• Matrix Exponentiation needs square matrix as input.
>> a^2 % Matrix Exponentiation: ans = a * a
ans =
44 20
32 44
>> a.^2 % Element-wise Exponentiation: ans = a .* a
ans =
4 25
64 4
Introduction to MATLAB and Simulink
25. 1 2 3 1 5 4
4 5 6 3 2 6
• Matrices can be concatenated just like elements in a matrix.
• Row-wise concatenation ( separated by space or commas)
>> f = [b d]
f =
• Column-wise concatenation (separated by semicolon)
>> g = [b ; d]
g = 1 2 3
4 5 6
1 5 4
3 2 6
Introduction to MATLAB and Simulink
26. % Generates a (n x n) Normally Distributed Random Matrix
% Generates a (n x n) Identity Matrix
% (n x n) Magic Matrix (Same Sum along Row, Column and Diagonal)
• >> randn(n)
• >> eye(n)
• >> magic(n)
• >> diag(A) % Extracts the elements along the primary diagonal of Matrix A
• >> blockdiag(A,B,C,..) % Generates a block diagonal matrix, with A, B, C, .. As
diagonal elements.
• >> length(x) % Calculates length of a vector x
• >> [m,n] = size(x) % Gives the [Rows,Columns] size of vectorx
• >> floor (x)
• >> ceil (x)
• >> clc
• >> clear
• >> close
• >>a = []
% Round x towards negative infinity (Floor)
% Round x towards positive infinity (Ceiling)
% Clears Command Window
% Clears the Workspace Variables
% Close Figure Windows
%Generates an Empty Matrix
Introduction to MATLAB and Simulink
27. Function Description
Max(x) Return largest element in a vector (each column)
Min(x) Return smallest element in a vector (each column)
Mean(x) Returns mean value of elements in vector (each column)
Std(x) Returns standard deviation of elements in vector (each column)
Median(x) Returns median of elements in vector (each column)
Sum(x) Returns sum of all values in vector (each column)
Prod(x) Returns product of elements in vector (each column)
Sort(x) Sorts values in vector in ascending order
Corr(x) Returns Pair-wise correlation coefficient
Hist(x) Plots histogram of vector x
Introduction to MATLAB and Simulink