Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Lab 1 Introduction To Matlab:: Objectives

Uploaded by

Ahmed Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lab 1 Introduction To Matlab:: Objectives

Uploaded by

Ahmed Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY

Hamdard Institute of Engineering & Technology


Hamdard University

LAB 1
INTRODUCTION TO MATLAB

OBJECTIVES:
 To become familiar with MATLAB
 Basic concept of array, matrices and vector operations

EQUIPMENT / REQUIREMENT:

Hardware Requirement
 Personal computer.

Software Requirement
 MATLAB.

THEORY:
This laboratory manual contains laboratory exercises of MATLAB. The purpose of these exercises is to help
reconcile the declarative (what is) and imperative (how to) points of view on numerical computing. The
mathematical treatment that dominates in the associated text is declarative in the sense that it asserts properties of
signals and studies the relationships between signals that are implied by systems. This laboratory manual focuses on
an imperative style, where numerical methods are constructed procedurally.
MATLAB(Matrix Laboratory) and Simulink, distributed by The MathWorks, Inc., are chosen as the basis for these
exercises because they are widely used by practitioners in the field, and because they are capable of realizing
interesting systems. Why use both MATLAB and Simulink? Although they are integrated into a single package,
MATLAB and Simulink are two very different pieces of software with radically different approaches to modeling of
signals and systems. MATLAB is an imperative programming language, whereas Simulink is a block diagram
language. In MATLAB, one specifies the sequence of steps that construct a signal or operate on a signal to produce
a new signal. In Simulink, one connects blocks that implement elementary systems to construct more interesting
systems. The systems we construct are aggregates of simpler systems.
MATLAB fundamentally operates on matrices and vectors. Simulink fundamentally operates on discrete and
continuous-time signals. Finite discrete-time signals, of course, can be represented as vectors. Continuous-time
signals, how-ever, can only be approximated. Simulink, since it is a computer program, must of course approximate
continuous-time signals by a discrete time. But that approximation is largely transparent, and the user (the model
builder) can pretend that he or she is operating directly on continuous-time signals. There is considerable value in
becoming adept with these software pack-ages. MATLAB and Simulink are often used in practice for “quick-and-
dirty” prototyping of concepts. In a matter of a few hours, very elaborate models can be constructed. This contrasts
with the weeks or months that would often be required to build a hardware prototype to test the same concept. Of
course, a conventional programming language such as C++ or Java could also be used to construct prototypes of
systems. However, these languages lack the rich libraries of built-in functions that MATLAB and Simulink have. A
task as conceptually simple as plotting a waveform can take weeks of programming in Java to accomplish well.
Algorithms, such as the FFT or filtering algorithms, are also built in, saving considerable effort.
If you have no background in programming, these exercises will be difficult at first. MATLAB, at its root, is a fairly
conventional programming language, and it requires a clear understanding of programming concepts such as
variables and flow of control (for loops, while loops). As programming languages go, it is an especially easy one to
learn. Its syntax (the way commands are written) is straightforward and close to that of the mathematical concepts

Page 1
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University

that it emulates. Moreover, since it is an interpreted language (in contrast to a compiled language), you can easily
experiment by just typing in commands at the console and seeing what happens. Be fearless! The worst that can
happen is that you will have to start over. These labs assume the computer platform is Microsoft Windows, although
any platform capable of running MATLAB and Simulink will work, as long as it has full support for sound and
images.

PROCEDURE:

LAB ACTIVITY
To get started, click on the icon from the desktop or search windows for MATLAB

Version of MATLAB which you will use in this lab is MATLAB R2015a

Figure-0.1 How to Start MATLAB

This will open a MATLAB command window, which displays a prompt “>>” as shown in Figure-0.2. You type
commands at the prompt. The “>>” is the MATLAB prompt. You do not type that part. Explore the built-in demos
by typing demo. MATLAB provides an online help system accessible by using the help command. For example, to
get information about the function size, enter the following:

Page 2
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University

>> help size

There also is a help desk (formatted in HTML for viewing from a Web browser) with useful introductory material. It
is accessed from the Help menu. If you have no prior experience with MATLAB, see the topic “Getting Started” in
the help desk. Spend some time with this. You can find in the help desk all the information you need to carry out the
following exercises.

Figure-0.2 Command Window of MATLAB


WORKING WITH MATRICES IN MATLAB

Once we get into MATLAB, we meet a prompt >> called the MATLAB prompt. This prompt receives a user
command and processes it providing the output on the next line.

Example:

• Row vector:
>> A = [1 2 3]
A=
1 2 3

• Column vector:
>> B = [1; 2; 3]
B=
1
2
3

• Matrix 2 x 2:
>> C = [1 2; 3 4]
C=
1 2
3 4
• Matrix 3 x 3:
>> A = [1 2 3; 4 5 6; 7 8 9]

Page 3
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University

A=
1 2 3
4 5 6
7 8 9

• The element in the i’th row and j’th column of A is referred to in the usually way; A( i , j )
>> A(3,2)
ans =
8

SUB-MATRICES AND COLON OPERATOR

The colon operator is very useful for creating index arrays, creating vectors of evenly spaced values, and accessing
sub-matrices.

For example, the expression 1:6 is actually a row vector [1 2 3 4 5 6].

• >> 1:6
ans =
1 2 3 4 5 6

• >> 1:2:10
ans =
1 3 5 7 9

• >> 0.1:0.2:0.9
ans =
0.1000 0.3000 0.5000 0.7000 0.9000

• >> 0.9:-0.2:0.1
ans =
0.9000 0.7000 0.5000 0.3000 0.1000

MATRIX AND ARRAY OPERATIONS

MATLAB supports many useful arithmetic and matrix operations, such as:
+ Addition
- Subtraction
* Multiplication
.* Point Multiplication
./ Point wise Right division
.\ Point wise Left division
^ Power
.^ Point wise Power
‘ Transpose
Example:

Page 4
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University

• Transpose of a Matrix:
>> B = A'
B=
1 4 7
2 5 8
3 6 9

• Scalar addition to matrix:


>> B+1
ans =
2 5 8
3 6 9
4 7 10

• Scalar multiplication:
>> D=2*B
D=
2 8 14
4 10 16
6 12 18

• Matrix addition:
>> A+B
ans =
2 6 10
6 10 14
10 14 18

• Matrix multiplication:
>> A*B
ans =
14 32 50
32 77 122
50 122 194
• To perform point-wise operation (array operation) on matrices, we can use the “point-star” operator,
e.g. A.*B
>> A.*B
ans =
1 8 21
8 25 48
21 48 81

• Point wise right division operation:


>> A./B
ans =
1.0000 0.5000 0.4286
2.0000 1.0000 0.7500
2.3333 1.3333 1.0000

• Point wise left division operation:

Page 5
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University

>> A.\B
ans =
1.0000 2.0000 2.3333
0.5000 1.0000 1.3333
0.4286 0.7500 1.0000

• Point wise power operation:


>> A.^2
ans =
1 4 9
16 25 36
49 64 81

MATLAB BUILT-IN FUNCTIONS

MATLAB is a useful tool for matrix and vector manipulations. Collections of representative MATLAB matrix
functions are listed below

inv() inverse of a matrix


det() determinant of a matrix
length() length of a vector or number of columns in a matrix
eye(n) the n by n identity matrix
diag(n) diagonal components of a matrix
zeros(n,m)/ zeros(n) the n by m matrix consisting of all zero
ones(n,m) / ones(n) the n by m matrix consisting of all one
eig() eigen values /eigen vectors of a matrix
\ used to solve a set of linear algebraic equation
rand() uniformly distributed pseudorandom numbers

SCALAR, VECTOR AND MATRIX FUNCTIONS

MATLAB has some function operate essentially on scalar while operate element-by-element when applied to a
matrix. The most common scalar functions are

sin() sine
cos() cosine
tan() tangent
asin() inverse sine
acos() inverse cosine
atan() inverse tangent
exp() exponential
sign sign
rem remainder
abs absolute
sqrt square root

Page 6
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University

log natural logarithm (ln)


log10 logarithm (log)
round round off
floor Round toward negative infinity
ceil Round toward positive infinity

UING HELP FEATURE OF MATLAB

>>help sin

TO CLEAR SCREEN

>>clc

LAB EXERCISE:
Determine the output of the following?

• >> B = [60:0.5:70]
B=

• >> V = [3 3 3; 4 4 4; 5 5 5];
>> G = [3 5 6; 3 8 9; 9 5 4];
>> Y = V*G
Y=

• >>Y = V*G(1:3)'
Y=

• >> V(:,:)
Ans=

• >> V(:,2)
Ans=

• >> V(:,3) '


Ans=

• >> H=[2 3 4 6; 5 4 3 5; 6 7 8 3; 8 7 4 8]
H=
• >>H(2:3,3:4) '
H=

• >>X=[2 2 2; 3 3 3; 4 4 4];
>>T=H(1:3,1:3)+X
T=

• >> T=H(1:3,1:3)-X

Page 7
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University

T=

• >> K=eye(3)
K=

• >> T=[1 ones(1,5) zeros(1,3)]


T=

• >> T=rand(3)
T=

• >> S=magic(4)
S=

• >> R=eig(eye(4))
R=

• >> U=round([-1.2 -2.5 -3.5 1.2 3.5 3.9 4.6])


U=

• >> X=abs(U)
X=

• >> U=floor([-1.2 -2.5 -3.5 1.2 3.5 3.9 4.6])


U=

• >> U=ceil([-1.2 -2.5 -3.5 1.2 3.5 3.9 4.6])


U=

• Let two matrices of 3 x 3 name A and B and verify that:


a. A^2 = A * A
b. A ./ B = B .\ A

Note: A and B Matrices consist of real integers.

• Generate the following matrix (see Figure-0.3) show the output and determine its

• Size
• Determinant
• Inverse
• Length
• Diagonal
• Eigen Value
• Sin
• Sin-1
• Cos
• Cos-1
• Tan

Page 8
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University

• Tan-1
• Square Root
• Natural Log (Ln)
• Logarithm

A=

Figure-0.3

• Find the values of x, y, and z in the following equations?


3x + 2y + 4 z = 4
5x + 4y + 6 z = 6
3x + 5y + 7 z = 8

Hint: X = A\B
Denotes the solution to the matrix equation AX = B.

Page 9

You might also like