Control Systems Lab No. 01 (Matlab Basics)
Control Systems Lab No. 01 (Matlab Basics)
01
Matlab Basics
What is Matlab???
MATLAB is a high-performance language for technical computing. It integrates computation,
visualization, and programming in an easy-to-use environment where problems and solutions are
expressed in familiar mathematical notation. Typical uses include:
a) Math and computation
b) Algorithm development
c) Modelling, simulation, and prototyping
d) Data analysis, exploration, and visualization
e) Scientific and engineering graphics
f) Application development, including graphical user interface building.
Display Windows
Three basic windows:
1. Graphic (Figure) Window: It displays plots and graphs, created in response to graphics
commands.
2. M-file editor/debugger window: Create and edit scripts of commands called M-files.
3. Command window: In command window we type commands, view program
variables; we can clear all the variables by typing Clear in the command Window.
0 y is a column vector
0.785
4
1.570
8
2.356
2
3.141
6
Vector Addressing – A vector element is addressed in MATLAB with an integer index
enclosed in parentheses.
Example:
>> x(3)
ans =
rd
1.5708 3 element of vector x
The colon notation may be used to address a block of elements.
(start : increment : end)
start is the starting index, increment is the amount to add to each successive
index, and end is the ending index. A shortened format (start : end) may be used
if increment is 1.
Example:
>> x(1:3)
ans =
>> f = [ 1 2 3; 4 5 6]
f=
1 2 3
4 5 6
>> h = [2 4 6
1 3 5]
h=
2 4 6
1 3 5
Matrix Addressing:
matrixname(row, column)
colon may be used in place of a row or column
reference to select the entire row or column.
Some useful commands:
size (A) for a m x n matrix A, returns the row vector [m,n] containing
the number of rows and columns in matrix.
MATLAB CODE:
>> A = [ 3 2 -1; -1 3 2; 1 -1 -1];
>> b = [ 10; 5; -1];
>> x = inv(A)*b
x=
-2.0000
5.0000
-6.0000
Answer: x1 = -2 x2 = 5 x3 = -6
Example
Plot the functions i) a = 2x, ii) b = x2 iii) c = 2 x3 iv) d = √x in the interval (-3,3). In
editor window:
% defining interval (-3,3).
x= -3:0.01:3;
% defining functions i) a = 2x, ii) b = x2 iii) c = 2 x3 iv) d = √x
a=2.*x;
b=x.^2;
c=2.*x.^3;
d=sqrt(x);
% plotting the function a = 2x,
figure (1);
subplot (2,2,1), plot (x,a);
title ('Linear Relation');
xlabel (‘Time ( seconds )’)
ylabel ('Output'), grid;