Control Systems Lab No. 01 (Matlab Basics) | PDF
0% found this document useful (0 votes)
51 views

Control Systems Lab No. 01 (Matlab Basics)

Matlab is a programming language for technical computing. It can be used for math and computation, algorithm development, modelling, data analysis, and more. There are three main display windows: the graphic window to view plots, the M-file editor for writing scripts, and the command window for typing commands. Help is accessed using the help command. Variables must start with a letter and are case sensitive. Special variables include pi, ans, and eps. Vectors can be row or column, and elements are accessed using indexes. Matrices are two-dimensional arrays that are addressed using row and column indexes. Basic functions include sin, cos, tan, exp, and log. Plotting functions like plot, axis, and multiple curves

Uploaded by

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

Control Systems Lab No. 01 (Matlab Basics)

Matlab is a programming language for technical computing. It can be used for math and computation, algorithm development, modelling, data analysis, and more. There are three main display windows: the graphic window to view plots, the M-file editor for writing scripts, and the command window for typing commands. Help is accessed using the help command. Variables must start with a letter and are case sensitive. Special variables include pi, ans, and eps. Vectors can be row or column, and elements are accessed using indexes. Matrices are two-dimensional arrays that are addressed using row and column indexes. Basic functions include sin, cos, tan, exp, and log. Plotting functions like plot, axis, and multiple curves

Uploaded by

Ashno Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lab No.

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.

HELP COMMAND in a Command Window


help – lists all the help topic
help topic – provides help for the specified topic
help command – provides help for the specified command
help help – provides information on use of the help command
helpwin – opens a separate help window for navigation
lookfor keyword – Search all M-files for keyword
LESSON No. 01
Variables
 Must start with a letter
 May contain only letters, digits, and the underscore “_”
 Matlab is case sensitive, i.e. one & OnE are different variables.
 Matlab only recognizes the first 31 characters in a variable name.
Assignment statement
Matlab is a case sensitive that is ‘a’ is not the same as ‘A’.
Matlab has a built in variables like ‘pi’,’ans’ and ‘eps’.The
variable ‘ans’ will keep the track of the last output which was not assigned to another variable.
Special variables
ans : default variable name for the result
pi:  = 3.1415926…………
eps:  = 2.2204e-016, smallest amount by which 2 numbers can differ.
Inf or inf :, infinity
NaN or nan: not-a-number

Variable = number;
Variable = expression;
The equality sign is used to assign values to the variables. For
example
>>x=3
>>y=x^2
NOTE: when a semi-colon ”;” is placed at the end of each command, the result is not displayed.
Example:
>>tutorial = 1234;
>>tutorial = 1234
tutorial =
1234
Variable arithmetic’s:
Operators Precedence:
Let we have an equation like: 2+3*4^2
The following is the operator precedence in Matlab:
“^, * or /, + or -.”
So the ans of above equation will be:
>>ans=
50
Commands involving variables
 who: lists the names of defined variables
 whos: lists the names and sizes of defined variables
 clear: clears all variables, reset the default values of special variables.
 clear name: clears the variable name
 clc: clears the command window
 clf: clears the current figure and the graph window.
Built in Matlab Functions
Table 1:Basic Built in Matlab Functions:

Function Meaning Example

Sin Sine Sin(pi)=0.0

Cos Cosine Cos(pi)=1.0

Tang Tangent Tan(pi/4)=1.0

Exp Exponential Exp(1.0)=2.7183

Log Natural log Log(2.7183)=1.0


Table 1-1
LESSON No. 02
Vectors
 A row vector in MATLAB can be created by an explicit list, starting with a left
bracket, entering the values separated by spaces (or commas) and closing the
vector with a right bracket.
 A column vector can be created the same way, and the rows are separated by
semicolons.
 Example:

>> x = [ 0 0.25*pi 0.5*pi 0.75*pi pi ]


x=

0 0.7854 1.5708 2.3562 3.1416 x is a row vector

>> y = [ 0; 0.25*pi; 0.5*pi; 0.75*pi; pi ]


y=

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 =

0 0.7854 1.5708  1st to 3rd elements of vector x


NOTE: MATLAB index starts at 1.

Table 2: Some useful commands:

create row vector x starting with start, counting by one, ending


x = start:end
at end
create row vector x starting with start, counting by increment,
x = start:increment:end
ending at or before end
create row vector x starting with start, ending at end, having
linspace(start,end,number)
number elements
length(x) returns the length of vector x
y = x’ transpose of vector x
dot (x, y) returns the scalar dot product of the vector x and y.
LESSON No. 03
Matrices
A Matrix array is two-dimensional, having both multiple rows and multiple columns,
similar to vector arrays:

it begins with [, and end with ]

spaces or commas are used to separate elements in a row

semicolon or enter is used to separate rows.
•Example:

>> 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:

zeros(n) returns a n x n matrix of zeros

zeros(m,n) returns a m x n matrix of zeros

ones(n) returns a n x n matrix of ones

ones(m,n) returns a m x n matrix of ones

rand(n) returns a n x n matrix of random number


rand(m,n) returns a m x n matrix of random number

size (A) for a m x n matrix A, returns the row vector [m,n] containing
the number of rows and columns in matrix.

Length(A) returns the larger of the number of rows or columns in A.

eye(n)  returns an n x n identity matrix

eye(m,n)  returns an m x n matrix with ones on the main diagonal and


zeros elsewhere.

B=A’ returns matrix B as Transpose of matrix A.

Table 3: Matrix Operations:


LESSON No. 04
Scalar-Array Mathematics
For addition, subtraction, multiplication and division of an array by a scalar simply
apply the operations to all elements of the array.

Each element in the array f is multiplied by 2, then


subtracted by 1.

Element-by-Element Array-Array Mathematics:

Each element in x is multiplied by the corresponding element in y.

Table 4: Arithmatic Algebraic Operations

Solutions to Systems of Linear Equations

Example: a system of 3 linear equations with 3 unknowns (x1, x2, x3):


3x1 + 2x2 – x3 = 10
-x1 + 3x2 + 2x3 = 5
x1 – x2 – x3 = -1
in matrix form
3 2 −1 x1 10
𝐴 = [ −1 3 2] 𝑥 = [𝑥2] 𝑏=[ 5 ]
1 −1 −1 𝑥3 −1
Then, the system can be described as:
Ax = b

Solution by Matrix Inverse:


Ax = b
A-1Ax = A-1b
x = A-1b

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

NOTE: left division: A\b b÷ A right division: x/y x÷ y

Table 5: Relational and Logical Operators


LESSON No. 05
Plotting Curves
For more information on 2-D plotting, type help graph2d

Table 6: Matlab Built in Plotting Functions


Command Description
axis ([xmin xmax ymin Define minimum and maximum values of the axes
ymax])
plot (x,y) Generates a linear plot of the values of x (horizontal
axis) and y (vertical axis).
plot (x, y, w, z) Multiple curves can be plotted on the same graph by
using multiple arguments in a plot command. The
variables x, y, w, and z are vectors. Two curves will be
plotted: y vs. x, and z vs. w.
X-label(‘x axis label’) Add x-axis label
Y-label(‘y axis label’) Add y-axis label
Title(‘title of plot’) Title of the plot
grid on Adds dashed grids lines at the tick marks
grid off removes grid lines (default)
legend (‘string1’, ‘string2’,…) Used to distinguish between plots on the same graph
Hold Hold current graph in the figure
Pause Wait for user response
figure (n) Used in creation of multiple plot windows. Place
this command before the plot() command, and the
corresponding figure will be labeled as“figure n”
subplot (m, n, p) m by n grid of windows, with p specifying the current
plot as the pth window
Close Closes the figure n window
close all Closes all the figure windows
Table 7: Graph Formatting Tools

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;

% plotting the function b = x2,


subplot (2,2,2), plot (x,b);
title ('Squire/Quadratic Relation ');
xlabel (‘Time ( seconds )’)
ylabel ('Output'), grid;

% plotting the function c = 2 x3 ,


subplot (2,2,3), plot (x,c);
title ('Cubic Relation ');
xlabel (‘Time ( seconds )’)
ylabel ('Output'), grid;

% plotting the function d = √x ,


subplot (2,2,4), plot (x,d);
title ('Square root Relation ');
xlabel (‘Time ( seconds )’)
ylabel ('Output'), grid;

You might also like