NPTEL Online Certification Courses Indian Institute
of Technology Kharagpur
Foundations of Cyber-Physical Systems
Matlab Basics
● How to use Online Matlab?
1. Go to the link: https://matlab.mathworks.com/
2. Login with your email ID to Mathworks account. If your institute has Matlab license, you can
login with your institute ID.
3. Click on the ‘New’ button on left corner of menu bar to create a new script and save it
4.
5.
with ‘.m’ extension.
EL
Click on the ‘Run’ button (or F5) to execute the script
Use ‘help’ followed by Matlab’s in-built function name to know about the function
description. For example ‘help ode45’
PT
N
● Basic matrix operations in Matlab
% a 3x3 matrix
NPTEL Online Certification Courses Indian Institute
of Technology Kharagpur
A = [1 2 3;
3 1 2;
5 0 4];
% a 3x2 matrix
B = [1 2;
3 4;
5 6];
disp('Determinant of A')
disp(det(A))
disp('Inverse of A')
disp(inv(A))
disp('Transpose of A')
disp(A') % Or disp(transpose(A))
disp('Rank of A')
rank(A)
disp('Eigen values of A')
eig(A)
disp('Adjoint of A')
adjoint(A)
EL
disp('Matrix multiplication (AxB)')
PT
disp(A*B)
Output:
Determinant of A
-15.0000
Inverse of A
N
-0.2667 0.5333 -0.0667
0.1333 0.7333 -0.4667
0.3333 -0.6667 0.3333
Transpose of A
1 3 5
2 1 0
3 2 4
Rank of A
ans =
Eigen values of A
ans =
7.3544
NPTEL Online Certification Courses Indian Institute
of Technology Kharagpur
-2.2577
0.9034
Adjoint of A
ans =
4.0000 -8.0000 1.0000
-2.0000 -11.0000 7.0000
-5.0000 10.0000 -5.0000
Matrix multiplication (AxB)
22 28
16 22
25 34
EL
● Example of system modeling using Matlab: Lorenz Attractor
PT
Lorenz model is a system of 3 non-linear ordinary differential equations that relate the
properties of a two-dimensional fluid layer uniformly warmed from below and cooled from
above:
𝑥̇ = 𝜎(𝑦 − 𝑥)
𝑦̇ = 𝑥(𝜌 − 𝑧) − 𝑦
𝑧̇ = 𝑥𝑦 − 𝛽𝑧
N
Here, 𝑥 ∝ the rate of convection, 𝑦 ∝ the horizontal temperature variation, and 𝑧 ∝ the
vertical temperature variation. The parameters 𝜎, 𝜌, and 𝛽 are proportional to the Prandtl
number, Reyleigh number, and certain physical dimension of the layer itself respectively (all
are assumed to be positive).
Considering 𝜎 = 10, 𝛽 = 8/3, 𝜌 = 28, and the initial values of (𝑥, 𝑦, 𝑧) = (1,1,1), the
following Matlab code simulate Lorenz model over a time window of 100 units.
sigma = 10;
beta = 8/3;
NPTEL Online Certification Courses Indian Institute
of Technology Kharagpur
rho = 28;
% Inline function definition for Lorenz model
f = @(t,a) [-sigma*a(1) + sigma*a(2); rho*a(1) - a(2) - a(1)*a(3); -beta*a(3) + a(1)*a(2)];
% Solving ODE using Runge-Kutta 4th/5th order ODE solver
[t,a] = ode45(f,[0 100],[1 1 1]);
% plotting in 3 dimensions
plot3(a(:,1),a(:,2),a(:,3))
xlabel(‘x’);
ylabel(‘y’);
zlabel(‘z’);
EL
PT
N