Skill Lab-I SE-D9, VESIT
Experiment -1: MATLAB Basics
Date: _________
1. Aim: To study MATLAB basic features and built in MATLAB functions.
2. Requirements: PC with Windows operating system, MATLAB
3. Pre-Experiment Exercise
Theory: Introduction to MATLAB
MATLAB (MATrix LABoratory) is a high performance language for technical
computing. It is a mathematical software package, which is used extensively in both academia and
industry. It is an interactive program for numerical computation and data visualization, which along with
its programming capabilities provides a very useful tool for almost all areas of science and engineering.
It integrates computation, visualization and programming in an easy-to-use environment where
problems and solutions are expressed in familiar mathematical notation.
MATLAB is primary tool for matrix computations. MATLAB deals mainly with
matrices. A scalar is a 1-by-1 matrix and a row vector of length say 5, is a 1-by-5 matrix. One of
the many advantages of MATLAB is the natural notation used. It looks a lot like the notation that
is encountered in a linear algebra. This makes the use of the program especially easy and it is
what makes MATLAB a natural choice for numerical computations. It can be used for simple
mathematical manipulation with matrices for understanding and teaching basic mathematical and
engineering concepts and even for studying and simulating actual technical problems. The
purpose of this experiment is to familiarize MATLAB, by introducing the basic features and
commands of the program.
MATLAB comprises of lot of optional tool boxes and block set like control system,
neural network, image processing, wavelet etc.
3.1 TYPICAL USES INCLUDE
Math and computation.
Algorithm development.
Modeling, simulation and prototype.
Data analysis, exploration and Visualization.
Scientific and engineering graphics.
Application development, including graphical user interface building.
3.2 GETTING STARTED WITH MATLAB
To open the MATLAB applications double click the MATLAB icon on the desktop.
This will open the MATLAB window space with MATLAB prompt as shown in the Fig.1.
Skill Lab-I SE-D9, VESIT
Fig. 1: MATLAB window space
To end your MATLAB session, select File > Exit MATLAB in the desktop, or type quit in the
Command Window.
In the MATLAB® environment, a matrix is a rectangular array of numbers. Special meaning is
sometimes attached to 1-by-1 matrices, which are scalars, and to matrices with only one row or
column, which are vectors. MATLAB has other ways of storing both numeric and nonnumeric
data, but in the beginning, it is usually best to think of everything as a matrix. The operations in
MATLAB are designed to be as natural as possible. Where other programming languages work
with numbers one at a time, MATLAB allows you to work with entire matrices quickly and
easily.
Operators in MATLAB:
Arithmetic operators Relational/Logical operators
+ addition == Equal to
- subtraction ~= Not equal to
* multiplication < Strictly smaller
/ division > Strictly greater
^ power <= Smaller than or equal to
‘ complex conjugate transpose >= Greater than equal to
.* element-by-element multiplication & And operator
./ element-by-element division | Or operator
.^ element-by-element power
Skill Lab-I SE-D9, VESIT
i) Declaration of scalars/variables
x=1
This command both allocates a space in memory for the variable x, if x has not already been
declared, and then stores the value of 1 in the memory location associated with this variable. It
also writes to the screen "x = 1". We can make the command "invisible" by ending it with a
semi-colon. e.g. x=2; this changes the value of x but does not write to the screen. Then, we
display the value of x by typing "x" without a semi-colon.
>>y = 2*x; initializes the value of y to twice that of x
>>x = x + 1; increases the value of x by 1.
>>z = 2*x; declares another variable z.
z does not equal y because the value of x changed between the times when we declared each
variable.
>>difference = z - y
To see the list of variables that are stored in memory, use the command "who".
>>who;
We can get more information by using "whos".
>>whos;
ii) Vectors
To create the vector a, enter into the MATLAB command window
>>a = [1 2 3 4 5 6 9 8 7]
MATLAB should return:
a=
1 2 3 4 5 6 9 8 7
To create a vector with elements between 0 and 20 evenly spaced in increments of 2 (this method
is frequently used to create a time vector):
>>t = 0:2:20
t=
0 2 4 6 8 10 12 14 16 18 20
Manipulating vectors is almost as easy as creating them. To add 2, to each of the elements in
vector 'a' equation is
>>b = a + 2
b=
3 4 5 6 7 8 11 10 9
To add two vectors together of the same length,
>>c = a + b
c=
4 6 8 10 12 14 20 18 16
Subtraction of vectors of the same length works exactly the same way.
3.3 Functions in MATLAB
i) Scalar functions
Certain MATLAB functions are essentially used on scalars, but operate element-wise when applied to a
matrix (or vector). They are summarized below.
sin trigonometric sine
cos trigonometric cosine
tan trigonometric tangent
Skill Lab-I SE-D9, VESIT
asin trigonometric inverse sine (arcsine)
acos trigonometric inverse cosine (arccosine)
atan trigonometric inverse tangent (arctangent)
sinh hyperbolic sin
cosh hyperbolic cosine
tanh hyperbolic tangent
exp exponential
log natural logarithm
abs absolute value
sqrt square root
rem remainder
round round towards nearest integer
floor round towards negative infinity
ceil round towards positive infinity
ii) Vector functions
Some of the functions essentially on vectors returning a scalar value are given below:
sum Sums the content of the variable passed
prod Multiplies the content of the variable passed
mean Calculates the mean of the variable passed
median Calculates the median of the variable passed
mode Calculates the Mode of the variable passed
std Calculates the standard deviation of the variable passed
sqrt Calculates the square root of the variable passed
max Finds the maximum of the data
min Finds the minimum of the data
size Gives the size of the variable passed
iii) Matrix functions
eye identity matrix size size of a matrix
zeros matrix of zeros det determinant of a square matrix
ones matrix of ones inv inverse of a matrix
diag extract diagonal of a matrix rank rank of a matrix
or create diagonal matrices rref reduced row echelon form
triu upper triangular part of a matrix eig eigenvalues and eigenvectors
tril lower triangular part of a matrix poly characteristic polynomial
rand randomly generated matrix norm norm of matrix (1-norm, 2-norm)
lu LU factorization
qr QR factorization
svd singular value decomposition
Entering matrices into MATLAB is the same as entering a vector, except each row of elements is
separated by a semicolon (;) or a return:
>>B = [1 2 3 4; 5 6 7 8;9 10 11 12]
B=
1 2 3 4
5 6 7 8
9 10 11 12
Skill Lab-I SE-D9, VESIT
Matrices in MATLAB can be manipulated in many ways.
>>C = B'
C=
1 5 9
2 6 10
3 7 11
4 8 12
Two matrices B and C can be multiplied together. Remember that order matters when
multiplying matrices.
>>D = B * C
D=
30 70 110
70 174 278
110 278 446
>>D = C * B
D=
107 122 137 152
122 140 158 176
137 158 179 200
152 176 200 224
Another option for matrix manipulation is to multiply the corresponding elements of two
matrices using the .* operator (the matrices must be of the same size).
>>E = [1 2 ; 3 4]
>>F = [2 3 ; 4 5]
>>G = E .* F
E=
1 2
3 4
F=
2 3
4 5
G=
2 6
12 20
For square matrix, like E, multiply it by itself as many times as required by raising it to a given
power.
>>E^3
ans =
37 54
81 118
Skill Lab-I SE-D9, VESIT
If wanted to cube each element in the matrix, just use the element-by-element cubing.
>>E.^3
ans =
1 8
27 64
You can also find the inverse of a matrix:
>>X = inv(E)
X=
-2.0000 1.0000
1.5000 -0.5000
or its eigenvalues:
>>eig(E)
ans =
-0.3723
5.3723
0.8
iv) Plotting Functions
0.6
0.4
The MATLAB environment provides a wide variety
0.2
of techniques to display data graphically. Interactive
0 tools enable you to manipulate graphs to achieve
-0.2 results that reveal the most information about your
-0.4 data. The plot of a sine wave as a function of time is
-0.6
shown below.
-0.8
>>t=0:0.2:20;
-1
0 2 4 6 8 10 12 14 16 18 20 >>y = sin(t);
>>plot(t, y)
Some more useful plotting functions are:
plot3 3-d line plot
semilogx/semilogy semi-logarithmic plot
subplot create axes in tiled position
v) Polynomials
In MATLAB a polynomial is represented by a vector. To create a polynomial in MATLAB,
simply enter each coefficient of the polynomial into the vector in descending order. For instance,
let's say you have the following polynomial:
Skill Lab-I SE-D9, VESIT
To enter this into MATLAB, just enter it as a vector in the following manner:
>>x = [1 3 -15 -2 9]
x=
1 3 -15 -2 9
MATLAB can interpret a vector of length n+1 as an nth order polynomial. Thus, if your
polynomial is missing any coefficients, you must enter zeros in the appropriate place in the
vector. E.g.
would be represented in MATLAB as:
>>y = [1 0 0 0 1]
The value of a polynomial can be found using the polyval function. For example, to find the
value of the above polynomial at s=2,
>>z = polyval([1 0 0 0 1],2)
z=
17
Roots of polynomial can also be extracted in case of high-order polynomial such as
Finding the roots would be as easy as entering the following command;
>>roots([1 3 -15 -2 9])
ans =
-5.5745
2.5836
-0.7951
0.7860
4. MATLAB exercise
i. Given the matrix A = [2 4 1; 6 7 2; 3 5 9], provide the commands needed to
a) assign the first row of A to a vector called x1
b) assign the last 2 rows of A to an array called y
c) compute the sum over the columns of A
d) compute the sum over the rows of A
ii. Generate two matrices of order 3 x 3 and perform following operations on
them:
(a) addition
(b) subtraction
(c) Multiplication
(d) bitwise multiplication
(e) Find inverse of the matrix
(f) Find rank of the matrix
Skill Lab-I SE-D9, VESIT
iii. Let x = [2 5 1 6]
a) Add 16 to each element
b) Add 3 to just the odd-index elements
c) Compute the square root of each element
d) Compute the square of each element
iv. Given x = [3 15 9 12 -1 0 -12 9 6 1], provide the command(s) that will
a) set the values of x that are positive to zero
b) set values that are multiples of 3 to 3
c) multiply the values of x that are even by 5
d) extract the values of x that are greater than 10 into a vector called y
e) set the values in x that are less than the mean to zero
f) set the values in x that are above the mean to their difference from the mean
v. Given the vector x = [1 8 3 9 0 1], create a short set of commands that will
a) Add up the values of the elements
b) Computes the running sum (for element j, the running sum is the sum of the
elements from 1 to j, inclusive).
c) Computes the sine of the given x-values (should be a vector)
5. Printout
Create a common M-File for above tutorial questions. Attach the following things.
A. printout of M-File
B. printout of M-File output
6. Conclusion
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________