Você está na página 1de 26

Experiment No.

01

Title: Introduction to basic programming for
control in MATLAB
Instructor:
M. Asghar Khan
Electrical Engineering Dept.
CIIT, Abbottabad Campus
asgharkhan@ciit.net.pk
9/16/2014 1
Objectives
The purpose of this LAB is to get familiar with:

Matrices
Arrays
Graphics
Data Analysis and
Mathematics in MATLAB
9/16/2014 2
Task 01
Working with
Matrices and
Arrays
9/16/2014 3
Task 01: Working with Matrices
Matrix
A Matrix is a collection of numbers arranged into a fixed
number of rows and columns






In describing Matrices, the format is:
rows
e.g., 3 3
9/16/2014 4
Task 01: Working with Matrices
Defining a Matrix in Matlab

A=[1 3 2 11; 5 12 15 8; 10 6 5 12; 14 16 1 19]

A =
1 3 2 11
5 12 15 8
10 6 5 12
14 16 1 19
9/16/2014 5
Task 01: Working with Matrices
Transpose of a Matrix
A Matrix formed by turning all the rows of a
given matrix into columns and vice-versa.
A_transpose=A'
A_transpose =
1 5 10 14
3 12 6 16
2 15 5 1
11 8 12 19
9/16/2014 6
Task 01: Working with Matrices
Produces a column vector containing the row sums
A_Sum=sum(A)'

A_Sum =

30
37
23
50
9/16/2014 7
Task 01: Working with Matrices
Gives the diagonal elements of a Matrix
A_Diagonal=diag(A)
A_Diagonal =

1
12
5
19
9/16/2014 8
Task 01: Working with Matrices
To produce the sum of diagonal elements

>> sum_diagonal_elements=sum(A_Diagonal)

sum_diagonal_elements =

37
9/16/2014 9
Task 01: Working with Matrices
To access the element of a Matrix i.e., A(i, j)

>> A(1, 4)

ans =

11
9/16/2014 10
Task 01: Working with Matrices
Sum of selected elements of a Matrix
>> A(1, 4)

ans =

11
9/16/2014 11
Task 01: Working with Matrices
Cancatenation of a Matrix
>> B=[A A+32; A+48 A+16]
B =
1 3 2 11 33 35 34 43
5 12 15 8 37 44 47 40
10 6 5 12 42 38 37 44
14 16 1 19 46 48 33 51
49 51 50 59 17 19 18 27
53 60 63 56 21 28 31 24
58 54 53 60 26 22 21 28
62 64 49 67 30 32 17 35
9/16/2014 12
Task 01: Working with Matrices
You can delete rows and columns from a matrix using just a
pair of square brackets. Start with
>> X(:, 2)=[]

X =

1 2 11
5 15 8
10 5 12
14 1 19
9/16/2014 13
Task 01: Working with Arrays
An ordered arrangement of things OR
An Array is a subset of Matrix, where either row are one in
number OR
Columns are one in number

>> Y=[1 2 3 4 5 6]

Y =

1 2 3 4 5 6
9/16/2014 14
Task 01: Working with Arrays
>> Z=[1; 2; 3; 4; 5; 6]

Z =
1
2
3
4
5
6
9/16/2014 15
Task 02
Working with
Plots and
Graphics
9/16/2014 16
Task 02: Working with Plots
Plot the following data
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
grid on
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the Sine Function','FontSize',12)


9/16/2014 17
Task 02: Working with Graphics
Plotting Multiple Data Sets in One Graph...
x = 0:pi/100:2*pi;
y = sin(x);
y2 = sin(x-.25);
y3 = sin(x-.5);
plot(x,y,x,y2,x,y3)
Legend('sin(x)','sin(x-.25)','sin(x-.5)')
grid on


9/16/2014 18
Task 02: Working with Graphics
9/16/2014 19

Color Specifiers
Specifier Color
r Red
g Green
b Blue
c Cyan
m Magenta
y Yellow
k Black
w White
Task 02: Working with Graphics
9/16/2014 20

Line Style Specifiers
Specifier LineStyle
'-' Solid line (default)
'--' Dashed line
':' Dotted line
'-.' Dash-dot line
Task 02: Working with Graphics
9/16/2014 21

Marker Specifiers
Specifier Marker Type
'+' Plus sign
'o' Circle
'*' Asterisk
'.' Point
'x' Cross
'square' or 's' Square
'diamond' or 'd' Diamond
'^' Upward-pointing triangle
'v' Downward-pointing triangle
'>' Right-pointing triangle
'<' Left-pointing triangle
'pentagram' or 'p' Five-pointed star (pentagram)
'hexagram' or 'h''' Six-pointed star (hexagram)
Task 02: Working with Graphics
Placing marker at every tenth data point

You might want to use fewer data points to plot the markers
than you use to plot the lines...

This example plots the data twice using a different number
of points for the dotted line and marker plots:




9/16/2014 22
Task 02: Working with Graphics
x1 = 0:pi/100:2*pi;
x2 = 0:pi/10:2*pi;
plot(x1,sin(x1),b:',x2,sin(x2),'r+')





9/16/2014 23
Task 02: Working with Graphics
Displaying multiple plots in one figure

x1 = 0:pi/100:2*pi;
y1=sin(x1);
y2=sin(2.5*x1);
y3=sin(5*x1);
y4=sin(7.5*x1);
subplot(2,2,1)
plot(x1,y1);xlabel('radians');ylabel('Sine of x')
subplot(2,2,2)
plot(x1,y2);xlabel('radians');ylabel('Sine of 2.5*x')
subplot(2,2,3)
plot(x1,y3);xlabel('radians');ylabel('Sine of 5*x')
subplot(2,2,4)
plot(x1,y4);xlabel('radians');ylabel('Sine of 7.5*x')






9/16/2014 24
Task 02: Working with Graphics
Displaying multiple plots in one figure







9/16/2014 25
Task 03
Its time to practice Task 01 and Task 02!

Plot the function
y =
sin(5)
+ cos(6 ) sin(4 )
Where t=0 to t=5 sec

i. Properly label the figure in x and y axis
ii. Also add the suitable title

9/16/2014 26

Você também pode gostar