Lab Report 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

DEPARTMENT OF MECHATRONICS

ENGINEERING

UNIVERSITY OF ENGINEERING AND TECHNOLOGY,


PESHAWAR
MTE-328L CONTROLL SYSTEMS LAB, 6th SEMESTER
LAB REPORT 2
SUBMITTED BY: TAIMOOR KHAN
REG NO.: 20PWMCT0739
SUBMITTED TO: PROFF DR. RYAZ AKBAR SHAH
SUBMISSION DATE: 15-03-2022
LAB TITLE  INTRODUCTION TO MATLAB (“MATRICES AND
POLYNOMIAL”)
LAB REPORT RUBRICS:
2
LAB NO.2
LAB TILTLE
INTRODUCTION TO MATLAB (“MATRICES AND
POLYNOMIAL”)
OBJECTIVES
 To get familiar with MATLAB environment and analyze the interface of MATLAB.
 To get familiar with Matrix Operations in the MATLAB.
 To get familiar with Polynomial Operations in the MATLAB.
 To know about different commands for different operation in the MATLAB.

THEORY
MATLAB (Matrix laboratory)
 MATLAB (matrix laboratory) is a fourth-generation high-level programming language
and interactive environment for numerical computation, visualization and programming.
 It allows matrix manipulations; plotting of functions and data; implementation of
algorithms; creation of user interfaces; interfacing with programs written in other
languages, including C, C++, Java, and FORTRAN; analyse data; develop algorithms;
and create models and applications.
It can perform the following functions

Dealing with Matrices and Arrays


2-D and 3-D Plotting and graphics
Linear Algebra
Algebraic Equations
Non-linear Functions
Statistics
Data Analysis
Calculus and Differential Equations
Numerical Calculations
Integration
Transforms
Curve Fitting
Various other special functions [1]

3
IN LAB TASKS
In this lab we study about different command of MATLAB use to perform operation on the
matrices and the polynomials.
The commands with their functions are listed below
1. WHO
this command shows the variable you have used for example, if you have used 50 variables and
now you want to know about all of those

2. WHOS
This command tells you about format of variable.

3. CLEAR
This command clear all the variable

4. CLEAR X
this command clear that variable x

5. USE OF SEMICOLON (;)


With (;) variable is not display in command window

6. ADDITION, SUBTRACTION, MULTIPLICATION AND DIVISION

a =2; b =3;
 a +b;
 a-b;
 a*b);
 a/b;

7. CONTROLLING FLOATING POINT


Format short (4 decimal places)

4
a=3;
a/5= 0.600
format long (15 decimal places)
a/5= 0.600000000000

8. CONTROLLING SPACE
 format loose,
 format compact

9. MULTIPLE STATEMENT PER LINE


(Use semicolon e.g. a=2; b=3; c=4)

10. ENTERING A VECTOR


Row vector a= [1 2 3 4];
Column vector b= [1
2
3
4];
Or b= [1;2;3;4];

11. ENTERING A MATRIX,


a= [1 2 3;2 3 4]
or a= [ 1 2 3
3 4 5];

5
12. MATRIX INDEXING
To access single element
a= [3 2 1
765
9 4 8]
a(3)
ans=9;

13. TO ACCESS BLOCK OF ELEMENT


a(1:3)
ans= 1 2 3;

14. TO ACCESS SINGLE ROW


a(3,:)
Ans= 9 4 8;

6
15. TO ACCESS SINGLE COLUMN
a(:,3)
Ans=1
5
8

16. TO ACCESS SUB MATRIX


from second column to third

a(:,2:3)
Ans= 2 1
65
48
from second row to third

a(2:3,:)
Ans= 2 6 5
948

7
17. DELETING A ROW
A(2,:)=[]

18. Deleting a column,


A(:,2)=[]
19. CORRECTING AN ENTRY V (2,1) =5
20. ENTER AN ARRAY WITH STEP SIZE
C=1:2:9

21. CONVERTING ROW TO COLUMN


A(:)

8
22. Transpose of matrix
b=a. '

23. Dimension of matrix


length(A),
size(A)

24. eye(n) Returns an n-by-n square identity matrix


Eye(4)
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

25. zeros(m,n) Returns an m-by-n matrix of zeros


zeros(2,3)
ans =
0 0

0 0
0 0

9
26. ones(m,n) Returns an m-by-n matrix of ones
ones(2,3)
ans =
1 1 1
1 1 1

27. rand(m,n) Returns an m-by-n matrix of random numbers


rand(4,3)
 ans =
0.814723686393179 0.632359246225410 0.957506835434298
0.905791937075619 0.097540404999410 0.964888535199277
0.126986816293506 0.278498218867048 0.157613081677548
0.913375856139019 0.546881519204984 0.970592781760616

10
28. Concatenation of matrices
Matrices can be constructed in a block form. With C defined by
b=[6 7;1 2]
b=
6 7
1 2
>> c = [b zeros(2); ones(2) eye(2)]
c=
6 7 0 0
1 2 0 0
1 1 1 0
1 1 0 1

11
CREATING A POLYNOMIAL USING COEFFICIENTS:
The ‘poly’ function in MATLAB can be used to create a polynomial using a vector of coefficients.
The vector of coefficients should be in descending order of power of the polynomial.
For example, the vector [2, 4, 3] would represent the polynomial 2*x^2 + 4*x + 3.
 Root of polynomial
r= roots(P)
 Polynomial from root
P=poly(r)
 Addition of polynomial
 Subtraction of polynomial

 For multiplication and division use Dot operator (.)


a.*b
a./b
p1 =

6 5 3

>> p2=[9,8,7]

p2 =

9 8 7

>> p1.*p2

ans =

12
54 40 21

>> p1./p2

ans =

0.666666666666667 0.625000000000000 0.428571428571429

 Root of polynomial
r= roots(P)
 Polynomial from root
P=poly(r)

13
REFERENCES
1.https://www.tutorialspoint.com/matlab/matlab_overview.htm

14

You might also like