[go: up one dir, main page]

0% found this document useful (0 votes)
219 views11 pages

Math 012/012B Numerical Methods and Analysis Matlab Activity 3.1 Matrices

This document discusses matrices and arrays in MATLAB. It provides examples of how to generate, manipulate, and perform operations on matrices and arrays through MATLAB syntax and commands. Key points covered include generating matrices using functions like ones, zeros, and rand; concatenating and indexing matrices; applying different display formats; and performing basic matrix operations like addition and scalar multiplication.

Uploaded by

roseleen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
219 views11 pages

Math 012/012B Numerical Methods and Analysis Matlab Activity 3.1 Matrices

This document discusses matrices and arrays in MATLAB. It provides examples of how to generate, manipulate, and perform operations on matrices and arrays through MATLAB syntax and commands. Key points covered include generating matrices using functions like ones, zeros, and rand; concatenating and indexing matrices; applying different display formats; and performing basic matrix operations like addition and scalar multiplication.

Uploaded by

roseleen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

MATH 012/012B NUMERICAL METHODS AND ANALYSIS

MATLAB ACTIVITY 3.1

MATRICES

NAME :
SCORE:

SECTION: ACTIVITY NO:

INSTRUCTOR: DATE PERFORMED/SUBMITTED

The MATLAB environment uses the term matrix to indicate a variable containing real or complex numbers
arranged in a two-dimensional grid. An array is, more generally, a vector, matrix, or higher dimensional grid of
numbers. All arrays in MATLAB are rectangular, in the sense that the component vectors along any dimension are
all the same length.

Generating and Deleting a matrix and an array


1. To create an array with several elements in a single row, type the elements and separate each element with
either a comma (,) or a space and enclose it with a square bracket. This type of array is a row vector.

Try and Verify!!!


>> a= [1 2 3]
a =
1 2 3
2. To create a matrix that has multiple rows, type the element for each row and separate these rows with
semicolons; then enclose the matrix with square brackets.

Try and Verify!!!


>> a=[1 2 3;4 5 6;7 8 9]

a =
1 2 3
4 5 6
7 8 9

3. Matlab has different functions that create different kinds of matrices One way to create a matrix is to use
functions, such as ones, zeros, randn, and rand. The ones function will generate a matrix with all elements
equal to 1. The zeros function on the other hand will generate a matrix with all elements equal to 0. The
function rand generates a matrix with uniformly distributed elements while the randn function generates
normally distributed elements.
To generate a matrix of zeros type zeros(a, b) where a are the number of rows and b are the number of
columns or type zeros (x) where x is the order of the matrix for square matrices. The same format is used for
the other commands.

Try and Verify!!!


>> zeros(1,5)
ans =
0 0 0 0 0

>> ones(3)
ans =
1 1 1
1 1 1
1 1 1

>> randn(1,2)
ans =
0.7172 1.6302

>> rand(1,3)
ans =
0.9649 0.1576 0.9706

4. Another function that will generate matrices are pascal and magic. The pascal function generates symmetric
matrices while the magic function will generate non- symmetric matrices. To create symmetric matrices type
pascal(x) where x is the order of your matrix . Do the same to create non-symmetric matrices but using the
magic function . These functions generate square matrices only.

Try and Verify!!!


>> pascal(3)

ans =

1 1 1
1 2 3
1 3 6

>> magic(3)
ans =
8 1 6
3 5 7
4 9 2

5. An nxn identity matrix is denoted by In. MATLAB has a command to generate In with the syntax eye( ).

Try this and Verify!!!!!


eye(2) - displays a 2 x 2 identity matrix
eye(5) - displays a 5 x 5 identity matrix
t=10; eye(t) - displays a 10 x 10 identity matrix
eye(size(A)- displays an identity matrix the same size as A.
6. MATLAB has a command to build diagonal matrices when only the diagonal entries are inputted. The
command is diag(), and also works to "extract" a set of diagonal entries.

Try and Verify!!!


A =
8 1 6
3 5 7
4 9 2
>> b=diag(A)
b =
8
5
2
>> c=diag(diag(A))
c =
8 0 0
0 5 0
0 0 2

7. A matrix name can be reused. In such a case the "old" contents are lost.

8. To determine the matrix names that are in use , use the who command.

9. To delete a matrix, use the clear command, followed by a space and then the matrix name.

10. To determine the number of rows and columns in a matrix, use the size command.

Note: Notice that no brackets are displayed and that MATLAB has assigned this matrix the name ans. Every matrix
in MATLAB must have a name. If you do not assign a matrix name, then MATLAB assigns it with ans which is the
default variable name. Name each matrix with different variable names otherwise MATLAB will over write them.

If the matrix is quite large, the screen display will scroll too fast for you to see the matrix.
Display Formats
The format function controls the numeric format of the values displayed. The function affects only how
numbers are displayed, not how MATLAB software computes or saves them.

The different formats are format short, format long, format rat, format bank, format hex. The default format in
MATLAB is short.

Try and Verify!!!


>> format short
>> x = [4/3 1.2345e-6]
x =
1.3333 0.0000
>> format long
>> x = [4/3 1.2345e-6]
x =
1.333333333333333 0.000001234500000

>> format long e


>> x = [4/3 1.2345e-6]

x =
1.333333333333333e+00 1.234500000000000e-06

>> format short e


>> x = [4/3 1.2345e-6]
x =
1.3333e+00 1.2345e-06

>> format rat


>> x = [4/3 1.2345e-6]
x =
4/3 1/810045

>> format bank


>> x = [4/3 1.2345e-6]
x =
1.33 0.00

>> format hex


>> x = [4/3 1.2345e-6]
x =
3ff5555555555555 3eb4b6231abfd271

Concatenation
Concatenation is the process of joining arrays to make larger ones. A pair of square brackets [ ] is the concatenation
operator.
1. To concatenate horizontally use commas.
2. To concatenate vertically use semicolons.
Try and Verify!!!
a =
1 3
4 2
b =
1 1
1 2
>> A=[a,b]
A =
1 3 1 1
4 2 1 2
>> B=[a;b]
B =
1 3
4 2
1 1
1 2
Indexing
Every variable in MATLAB is an array that can hold many numbers. When you want to access selected elements of
an array, use indexing.

Try and Verify!!!


A =
8 1 6
3 5 7
4 9 2
There are two ways to refer to a particular element in an array. The most common way is to specify row and column
subscripts
>> A(3,2)
ans =
9

Another way is by the use of Linear Indexing where a single subscript that traverses down each column, in order, to
the desired element is used.
>> A(6)
ans =
9

To refer to multiple elements of an array, use the colon operator, which allows you to specify a range of the form
start:end.

>> A(1:2,3)
ans =
6
7

>> A(2,:)
ans =
3 5 7
The colon operator also allows you create an equally spaced vector of values using the more general form
start:step:end

>> B= 0: 10:100
>> B=

0 10 20 30 40 50 60 70 80 90 100
NAME :
SCORE:

SECTION: ACTIVITY NO:

INSTRUCTOR: DATE PERFORMED/SUBMITTED

TASK 1

Write the syntax that will create the following matrices. Use the rational format of MATLAB unless stated
otherwise.

MATRIX MATLAB SYNTAX

1 1 1 1 1
1.
0 0 0 0 0
1 1 1 1 1
5 0 0
2. 0 5 0
0 0 5

2 2 2
3. 2 2 2
2 2 2

TASK 2

Create a 3x 3 magic matrix A concatenate horizontally with a 3 x 3 pascal matrix B . Write the syntax and
output that will…

1. Extract the element in row 3 column 4

2. Extract column 3

3. Extract the diagonal

4. Create a diagonal matrix using the result in no. 3 as the diagonal.


MATLAB Syntax MATLAB Output
Concatenated Matrix A and B

1.

2.

3.

4.

Task 3
1. In MATLAB , enter the following matrices

5 i 2 3 4*2 2/3
3
A 0 1 B 1 / 201 5 8.2
4
1 0.0001 (9 4) / 3
4i i
3
2. Display matrix B in long format and matrix A in rational format

3. Compare the new format of A and B to the original format, how do they differ?

MATLAB Syntax MATLAB Output


1.

2.

3.
MATRIX AND ARRAY OPERATIONS

Matrix is a rectangular array of numbers denoted by:

The dimension /order of a matrix with m rows and n columns is denoted by mxn.
MATRIX OPERATIONS
Addition of Matrices
The sum A+B of two m-by-n matrices A and B is calculated entry wise (A + B)i,j = Ai,j + Bi,j, where 1 ≤ i ≤ m and 1 ≤
j ≤ n.

If A and B are m x n matrices, we write A + (-)B as A – B and call this the difference between A and B .
Scalar Multiplication

The scalar multiplication cA of a matrix A and a number c (also called a scalar) is given by multiplying every
entry of A by c: (cA)i,j = c · Ai,j.

Transpose

The transpose of an m-by-n matrix A is the n-by-m matrix AT (also denoted Atr or AT) formed by turning rows into
columns and vice versa: (AT)i,j = Aj,i.

Multiplication
Is defined only if the number of columns of the left matrix is the same as the number of rows of the right matrix. If A
is an m-by-n matrix and B is an n-by-p matrix, then their matrix product AB is the m-by-p matrix whose entries are
given by the dot-product of the corresponding row of A and the corresponding column of B:

Note:

BA may not be defined; this will take palce if n≠m.


a) If BA is defined, which means that m= n, then BA is p x p while AB is m x m; thus if m≠p, AB and BA are of
different sizes.

b) If AB and BA are both of the same size, they may be equal

c) If AB and BA are both of the same size, they may be unequal


Array Operations :

Array operations work on corresponding elements of arrays with equal dimensions. For vectors, matrices,
and multidimensional arrays, both operands must be the same size. Each element in the first input gets matched up
with a similarly located element from the second input; evaluated element by element.
Matrix Operations in MATLAB

The operations of addition, subtraction, and multiplication of matrices in MATLAB follow the same definitions
If A and B are m x n matrices that have been entered into MATLAB, then
1. The sum in MATLAB is computed by the command A+B

2. The difference by the command A-B

( Spaces can be used on either side of + or - .)

3. If A is m x n and C is n x k, then the product of A and C in MATLAB must be written as A *C

4. In M ATLAB, * must be specifically placed between the names of matrices to be multiplied.

5. In MATLAB, writing AC does not perform an implied multiplication. MATLAB considers AC a new matrix
name, and if AC has not been previously defined, an error will result from entering it.

6. Scalar multiplication 5A denotes scalar multiplication in math language while 5*A is required in MATLAB

7. In MATLAB the transpose operator (or symbol) is the single quotation mark, or prime, '.

Array Operations in MATLAB

Matrix operations follow the rules of linear algebra. By contrast, array operations execute element by element
operations and support multidimensional arrays. The period character (.) distinguishes the array operations from
the matrix operations. However, since the matrix and array operations are the same for addition and subtraction,
the character pairs .+ and .- are unnecessary.
a .* b multiplies each element of a by the respective element of b
a ./ b divides each element of a by the respective element of b
a .\ b divides each element of b by the respective element of a
a .^ b raise each element of a by the respective b element
a.' array transpose (non-conjugated transpose)

Try and Verify!!!

>> x = [1 2 3]; y = [4 5 6];

>> x ‘* y

ans= 32

>> x .* y

ans = [4 10 18]
TASK 3
1. In MATLAB enter the following matrices with rational format.

1 1/ 2 3
4 5/ 4 9/ 4
A 1/ 3 1/ 4 B 5 2 C D 1
1 2 3
1/ 5 1/ 6 4
Using MATLAB commands compute the following expressions, if possible, using matrix operations.
Given MATLAB Syntax MATLAB Output
1. Product of the transpose of
A and half of D.

2. Matrix B multiplied to the


transpose of the sum of A
and transpose of C

3. Six times of D subtracted


from transpose of twice C
multiplied to B.

4. Sum of the product of A and


its transpose and the
product of the transpose of
C and C.

5. Square of the product of


thrice A and B.
TASK 4

In MATLAB enter the following matrices with rational format.

1/ 2 3 / 4 1 3i 2 5i
5i 2 2i 4 5/ 4 9/ 4
A 1 1/ 5 B C D 1 i 1
1 4 4i 2 1 2 3
0 2 4 i 2 3i

Using MATLAB commands compute the following expressions, if possible, using array operations.

Given MATLAB Syntax MATLAB Output


1. Multiply A and D then add
the product to the
transpose of B.

2. Sum of A, transpose of B,
transpose of the square of
C and twice D.

3. Six times of transpose


of A subtracted from thrice
C divided by B.

4. Divide C by the transpose


of A then add the result to
the matrix obtained when
the transpose of D is
divided by the negative of
B.

5. Product of A and B
subtracted from the
product of C and D.

You might also like