LAB # 03 (Numerical Analysis)
LAB # 03 (Numerical Analysis)
LAB # 03
IMPLEMENTATION OF VECTORS MATRICES IN MATLAB
Objective:
The learning objectives are:
Knowledge of scalar, vectors and matrices and basic operations such as product and
transpose
Useful commands/functions related to vectors and matrices
Entering data to a matrix, line continuation, matrix indices, dimension statement.
Study of different matrix operations.
Knowledge of special matrices.
Study of different commands/functions used with matrices.
Introduction:
Arrays:
In a row vector, the elements are entered with a space or a comma between the elements
. = [ , , , ] or = [ ]
Column vector:
In a column vector, the elements are entered with a semicolon between the elements inside
the square brackets e.g.
. = [ ; ; ; ]
Scalar:
A scalar does not need any square brackets, for example a scalar = 10 can be entered as:
= 10
Care should be taken that the initial and final values must be appropriate. If a step-size is
not specified, +1 is taken as default value of the step-size.
It generates a linearly space vector of length ‘n’ that is ‘n’ equally spaced points between and
b. e.g.
= (0,10,5)
When executed, produces a vector having 5 equally spaced elements between the limits 0 and
10 given as follows:
>>A=[5 9 2
Returns the number of elements in
length(A) 4];
the Vector A.
>>length(A)
>>ans = 4
size(A) >>size(A)
Returns a row vector [m,n], where m
o >>ans = 1 4
r and n are the size mxn of the array A
(means 1 row and 4
[m,n]=size(A) columns)
diag(V) >>V=[3 2 1];
When ‘V’ is a vector, creates a square >>A=diag(V)
matrix with the elements of ‘V’ in
diagonals. >>A = 3 0 0
020
sort(x,’mole >>x=[5 -3 10 -10]
’) It lists the elements of the vector ‘x’ in 001
ascending or descending order. >>a=sort(x)
sort(x,’asce
nd’) >>a=[-10 -3 5 10]
If we only write sort(x), it will sort the
sort(x,’desc array in ascending order automatically. >>sort(x,’descend’)
end’)
Built-In Functions for Analyzing Arrays: >>b=[10 5 -3 -10]
Some of the many built-in functions available in MATLAB for analyzing arrays are listed below:
Matrix:
A matrix is a two dimensional array which has n-number of rows and columns. A matrix is
entered row-wise with consecutive elements of a row separated by a space or a comma, and the
columns are separated by semi-colons or carriage returns. The entire matrix is enclosed with in
square brackets. The elements of the matrix may be real numbers or complex numbers.
e.g.
= [1
−5 2 + 7 sin 3
5 − 13
3
Matrix Subscripts/Indices:
The elements of a matrix can be specified/ accessed by specifying their respective row and
column numbers. The first index refers to the row numbers and second index refers to column
number. The elements in row and column of a matrix A is denoted by. For example refers to the
element in the second row and the third column of the matrix A.
Command Description
(: , ) Refers to the elements in all the rows of the column ‘n’ of the matrix ‘A’.
( , :) Refers to the elements in all the columns of the row ‘m’ of the matrix ‘A’.
(: , : ) Refers to the element in all the rows between column ‘m’ and ‘n’ of
the matrix ’A’.
( : , :) Refers to the elements in all the columns between row ‘m’ and ‘n’ of
matrix ‘A’
( : , : ) Refers to the elements in rows ‘m’ through ‘n’ and columns ‘p’ through
‘q’ of matrix ‘A’.
1. Zeros (m,n)
2. Ones (m,n)
3. Eye (m,n)
It returns a rectangular × matrix with ones on the main diagonal and zeros elsewhere.
Matrices:
Introduction:
In MATLAB it is possible to work with complete matrix simultaneously. This feature is very
important as it removes the un-necessary loops and repetition of same statements. The program,
therefore, becomes short, concise and easily understandable. In MATLAB, matrix is chosen as a
basic data element. All variables when used as a single data element are treated as single element
matrix that is a matrix with one row and one column.
Matrix Manipulation:
Reshaping matrices as a vector
Reshaping matrices as a differently sized matrix
Expanding the matrix size
Appending a Row/Column to a Matrix
ILLUSTRATIVE PROGRAMS:
Task#01:
a) P*Q
b) Q*P
c) P T *Q T
Program:
Output:
Task#02:
Program:
Output:
Task#03:
Generate the following 3*3 matrices of common use with the help of MATLAB functions:
b) Null matrix.
c) Matrix with all elements equal to one where P=3 & Q=4.
Program:
Output:
Task#04:
a) Determinants.
Program:
Output:
Task#05:
Program:
Output:
Task#06:
Program:
Output:
Task#07:
a) A+B
b) A-B
d) A/B
Program:
Output:
Task#08:
Program:
Output:
Task#09:
Column vector.
(4*3) matrix.
(6*2) matrix
b) Given a column vector, x = & a row vector, y = [0 2 3 4 ] append these column/row vector
to the matrix P, given above.
Program:
Output:
Task#10:
c) Polynomial (A).
d) Eigen (A).
Program:
Output:
Task#11:
Program:
Output:
Task#12:
Create a 4*4 matrix of random numbers, multiply all the elements of matrix by 10 & then round
off all the elements to integers using appropriate commands.
Program:
Output:
Conclusion:
In this lab we have read the matrix operation and also the basic operations of scalar,
matrix, and vector. Vectors and matrix-related variables have been used and run programs using
different commands, from which we have been able to run and operate the programs.