0% found this document useful (0 votes)
16 views

LAB # 03 (Numerical Analysis)

This document discusses vectors, matrices, and their implementation in MATLAB. It covers defining scalars, vectors, and matrices; entering data into arrays; built-in functions for handling and analyzing arrays like length(), size(), mean(), max(), and dot(); and matrix operations. The objective is to learn about different matrix commands/functions in MATLAB.

Uploaded by

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

LAB # 03 (Numerical Analysis)

This document discusses vectors, matrices, and their implementation in MATLAB. It covers defining scalars, vectors, and matrices; entering data into arrays; built-in functions for handling and analyzing arrays like length(), size(), mean(), max(), and dot(); and matrix operations. The objective is to learn about different matrix commands/functions in MATLAB.

Uploaded by

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

LAB # 03

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:

An array is a list of numbers arranged in rows and/or columns. A one-dimensional array is


a row or a column of numbers and a two dimensional array has a set of numbers arranged in
rows and columns. An array operation is performed element by element.

Scalars and Vectors:

A scalar is a 1 × 1 matrix containing a single element only. A column vector is an × 1


matrix that has m-number of rows but a single column only. A row vector is a 1 × matrix
which has n-number of columns and has only one row.

Assigning data to elements of a Scalar/Vector:


Row vector:

In a row vector, the elements are entered with a space or a comma between the elements

inside the square brackets e.g. or

. = [ , , , ] or = [ ]

Numerical Analysis 1 Department of Chemical Engineering


LAB # 03

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

Creation of evenly spaced Row-vectors:


A row vector with evenly spaced, real elements can be created by using the following
command:

 K is the initial value of the row vector


 L is the step-size or increment
 M is the final value of row vector

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.

Commands for Evenly Spaced Row-Vectors:


Line space:
A row vector can also be created by using linepsace command. The syntax is given as
follows:
( , , )

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:

= [0 2.5 5.0 7.5 10.0]

Numerical Analysis 2 Department of Chemical Engineering


LAB # 03

Built-In Functions for Handling Arrays:


Some of the built-in functions available in MATLAB for managing and handling arrays are listed
in table below:
Function Description Example

>>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:

Functions Description Example


mean(A) If ‘A’ is vector, returns the mean value >>A= [3 7 2
of elements 16];
>>mean(A)
c=max(A) If ‘A’ is a vector, C is the largest element >>ans
>>A= =[3147 2 16 9 5
in ‘A’ 18 13 0 4];
>>c=max(A)
>>c=18
[d,n]=max(A) If ‘A’ is a vector ‘d’ is the largest element >>[d,n]=max(
in ‘A’, ‘n’ is the position of the element A) >>d=18
(the first if several have the max value) n=7
min(A) The same as max(A), but for the >>A=[3 7 2
[d,n]=min( smallest element 16]; >>min(A)
A) The same as [d,n]= max(A) but for >>ans=2
the smallest element

Numerical Analysis 3 Department of Chemical Engineering


LAB # 03

sum(A) If ‘A’ is a vector, return the sum of >>A=[3 7 2


the element of the vector 16]; >>sum(A)
>>ans=28
median(A) If ‘A’ is a vector, return the median >>A=[3 7 2
value of the element of vector 16];
>>median(A)
std(A) If ‘A’ is a vector return the standerd >>ans=5
>>A=[3 7 2
derivation of elements of vector 16]; >>std(A)
>>ans=6.3770
det(A) Return the determinant of a square matrix ‘A’ >>A=[3 7 2 16];
>>det(A) >>ans=-
2
dot(A,B) Calculates the scalar (dot) product of two vectors >>A=[5 6 7];
A & B. The vector can each be row or column
vectors >B=[4 3 2];
>>dot(a,b)
cross(A,B) Calculates the cross product of two vectors A & B >>ans=52
(AxB). The two vectors must have three elements. >>A=[5 6 7];
>B=[4 3 2];
>>cross(a,b)
>>ans=-9 18 -9

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.

To enter the matrix



= [1 3
4 ]

In MATLAB input command is:

= [1

−5 2 + 7 sin 3
5 − 13
3

Numerical Analysis 4 Department of Chemical Engineering


LAB # 03

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.

Colon for a Matrix:


The table below gives the use of a colon in addressing arrays in a matrix.

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’.

Element By Element Operations:


Element by element operations can only be done with arrays of the same size. Element
by element multiplication, division and exponentiation of two vectors or matrices is entered in
MATLAB by typing a period in front of arithmetic operator. Table below consist of list of these
operations.

Numerical Analysis 5 Department of Chemical Engineering


LAB # 03

Some Useful Commands Related To Matrices:


Command Description Example
det(A) It returns the determinant of a >>A=[1 2 ; 0 4]
matrix (| |). >>det(A)
>>ans=4
rank(A) It returns the of a given >>rank
rank rectangular (A)
>>ans=
matrix. (no. of 2
trace(A) non-zeros
It rows)sum of diagonal
returns the >>trace
elements of rectangular matrix. (A)
>>ans=
>>inv(A)
inv(A) It returns the inverse of non-singular 5
matrix (A-1)
norm(A) It returns the Euclidean norm of >>norm(A)
the given rectangular matrix ‘A’ of a >>ans=4.4
given vector ‘A’ 954
transpose( It returns the transpose of the >>A
A) /A’ specified rectangular matrix. ’
>>a 0
ns=
1 4
2

Generation Of Special Matrices:


The following MATLAB commands and functions which generate special matrices, are
often used in engineering computation involving matrices.

1. Zeros (m,n)

It returns a rectangular × matrix of all zeros.

2. Ones (m,n)

It returns a rectangular × matrix of all ones.

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.

Numerical Analysis 6 Department of Chemical Engineering


LAB # 03

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:

Given the row vector P = [1 -2 5] and a column Q = [ ]

Solve the following:

a) P*Q

b) Q*P

c) P T *Q T

d) Sum and mean of all the components of vectors P & Q.

e) Number of elements in vectors P & Q

f) Maximum & Minimum values in vectors P & Q.

g) Product of elements of vectors P & Q.

h) Arrange the elements of vectors P & Q in ascending order.

Program:

Numerical Analysis 7 Department of Chemical Engineering


LAB # 03

Output:

Numerical Analysis 8 Department of Chemical Engineering


LAB # 03

Task#02:

Given the matrix Write MATLAB statement to obtain:

a) All the elements of all rows but first column.

b) All the elements of first row but all columns.

c) Elements in the 2 nd row & 3 rd column.

Program:

Numerical Analysis 9 Department of Chemical Engineering


LAB # 03

Output:

Task#03:

Generate the following 3*3 matrices of common use with the help of MATLAB functions:

a) Unity or Identity matrix.

b) Null matrix.

c) Matrix with all elements equal to one where P=3 & Q=4.

Program:

Output:

Numerical Analysis 10 Department of Chemical


Engineering
LAB # 03

Task#04:

For the following matrices, find:

a) Determinants.

b) The inverse of each, if they exist & the corresponding product AA -1 BB -1

Program:

Numerical Analysis 11 Department of Chemical


Engineering
LAB # 03

Output:

Numerical Analysis 12 Department of Chemical


Engineering
LAB # 03

Task#05:

Determine the ranks of the following matrices:

Program:

Output:

Task#06:

Obtain the following products:


a) AB
b) BA
c) ATA
d) BTB
For the matrices given below:

Numerical Analysis 13 Department of Chemical


Engineering
LAB # 03

Program:

Output:

Task#07:

Evaluate using array operations:

a) A+B

b) A-B

c) A^3 & B^4

d) A/B

Numerical Analysis 14 Department of Chemical


Engineering
LAB # 03

Program:

Output:

Numerical Analysis 15 Department of Chemical


Engineering
LAB # 03

Task#08:

Given 3*4 matrix , using MATLAB commands:

a) Delete the 1 st row of matrix Q.

b) Delete the 1 st & 2 nd column of all the rows of matrix Q.

c) Extract the 2*2 sub-matrix from Q.

d) Replace the elements Q (2,2) with 800.

Program:

Output:

Numerical Analysis 16 Department of Chemical


Engineering
LAB # 03

Task#09:

Given the matrix P:

a) Reshape this matrix as a:

 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:

Numerical Analysis 17 Department of Chemical


Engineering
LAB # 03

Task#10:

Consider matrix A is given by:

Show the use of the following MATLAB command:

a) Diagonal (A), A is a matrix.

b) Diagonal (A, K), A is a matrix.

c) Polynomial (A).

d) Eigen (A).

Program:

Output:

Task#11:

Express the following sets of algebraic equations in matrix form, Ay=b

For this set of equations;

a) Find the inverse of the matrix, if it exists.

Numerical Analysis 18 Department of Chemical


Engineering
LAB # 03

b) Obtain the solution of the variables x, y&z.

c) Find the Eigen values and Eigen vectors of matrix A.

d) Find the trace of matrix A.

e) Find the transpose of matrix A.

Program:

Output:

Numerical Analysis 19 Department of Chemical


Engineering
LAB # 03

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.

Numerical Analysis 20 Department of Chemical


Engineering

You might also like