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

lab1 (1)

This document is a laboratory guide for MAT 343, focusing on matrix and vector computations using MATLAB. It introduces the MATLAB environment, basic operations, and commands for creating and manipulating matrices and vectors. The guide also includes exercises for practicing matrix algebra and using MATLAB functions.

Uploaded by

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

lab1 (1)

This document is a laboratory guide for MAT 343, focusing on matrix and vector computations using MATLAB. It introduces the MATLAB environment, basic operations, and commands for creating and manipulating matrices and vectors. The guide also includes exercises for practicing matrix algebra and using MATLAB functions.

Uploaded by

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

MAT 343 Laboratory 1

Matrix and vector computations in MATLAB

MATLAB is a computer software commonly used in both education and industry to solve a
wide range of problems. This Laboratory provides a brief introduction to MATLAB, and the
tools and functions that help you to work with MATLAB variables and files. More specifically,
we will learn how to

1. Create matrices and vectors.

2. Manipulate matrices and create matrices of special types

3. Add and multiply matrices

The MATLAB Environment


To start MATLAB double-click on the MATLAB shortcut icon. The MATLAB desktop will
open. The MATLAB default desktop consists of three windows: the command window, the
Current Folder browser and the Workspace browser.

• The command window is where MATLAB commands are entered and executed.

• The Current Folder browser allows you to view MATLAB and other files and to perform
file operations such as opening and editing or searching for files.

• The Workspace browser allows you to view and make changes to the contents of the
workspace.

Note that windows within the MATLAB desktop can be resized by dragging the separator
bar(s) and they can be closed by clicking on the × in the upper right corner of the window.

Basics And Help


Commands are entered in the Command Window.
F Basic operations are +, -, *, and /. The sequence

>> a=2; b=3; a+b, a*b


ans =
5
ans =
6

defines variables a and b and assigns values 2 and 3, respectively, then computes the sum a + b
and product ab. Each command ends with , (output is visible) or ; (output is suppressed).
The last command on a line does not require a ,.
F Standard functions can be invoked using their usual mathematical notations. For example

>> theta=pi/5;
>> cos(theta)^2+sin(theta)^2
ans =
1

THIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED
2021 v22 Copyright@ School of Mathematical and Statistical Sciences, Arizona State University
1
π
verifies the trigonometric identity sin2 θ + cos2 θ = 1 for θ = 5. A list of elementary math
functions can be obtained by typing
>> help elfun
F To obtain a description of the use of a particular function type help followed by the name
of the function. For example
>> help cosh
gives help on the hyperbolic cosine function.
F To get a list of other groups of MATLAB programs already available enter help:
>> help
F If you are looking for a function, use lookfor keyword to get a list of functions with the string
keyword in them. For example, typing lookfor ’identity matrix’ lists functions (there are
two of them) that create identity matrices.
F Another way to obtain help is through the help button in the toolbar.

F MATLAB is case-sensitive. For example


>> theta=1e-3, Theta=2e-5, ratio=theta/Theta
theta =
1.0000e-003
Theta =
2.0000e-005
ratio =
50
F The quantities Inf (∞) and NaN (Not a Number) also appear frequently. Compare
>> c=1/0
c =
Inf
with
>> d=0/0
d =
NaN

Matrices in MATLAB
Entering matrices in MATLAB is easy. For example, to enter the matrix
 
1 2 3 4
 5 6 7 8 
A=  9 10 11 12 

13 14 15 16
type A=[1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12; 13, 14, 15, 16]
or the matrix could be entered one row at a time:
A=[ 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16]

THIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED
2021 v22 Copyright@ School of Mathematical and Statistical Sciences, Arizona State University
2
Once a matrix has been entered, you can edit it. Here are some examples, which you can try
in the command window:
Input Output
A(1,3) = 5 A = Changes the third entry in the first
1 2 5 4 row of A to 5
5 6 7 8
9 10 11 12
13 14 15 16

C = A(2:3,2:4) C = Submatrix consisting of the en-


6 7 8 tries in rows 2 and 3 and columns
10 11 12 2 through 4

A(:,2:3) ans = Submatrix of A consisting of all


2 3 the elements in the second and
6 7 third columns
10 11
14 15

A(4,:) ans = Fourth row of A


13 14 15 16

E = A([1,3],[2,4]) E = Matrix whose entries are those


2 4 which appear only in the first and
10 12 third rows and second and fourth
column of A

Vectors
Vectors are special cases of matrices, with just one row or one column. They are entered the
same way as a matrix. For example
u = [1, 3, 9] produces a row vector
v = [1; 3; 9] produces a column vector.
Row vectors of equally spaced points can be generated with MATLAB’s : operation or using
the linspace command.
For example
1:5 produces the row vector 1 2 3 4 5
1:2:6 produces the row vector 1 3 5.
The most general form of the command is
<start>:<step>:<end>
where <start> is the first entry to be put in the vector, the next entry will be <start>+<step>,
the next entry <start>+2<step> and so on. The last entry in the vector is the largest values
of <start>+P<step> less than <end>. Generally we use integer values for <start>, <step>
and <end>, but you are allowed to use non-integer values. Negative values of the step are also
allowed. Below are some examples.

THIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED
2021 v22 Copyright@ School of Mathematical and Statistical Sciences, Arizona State University
3
Input Output
x = Row vector with in-
x = 2:6 2 3 4 5 6 teger entries from 2
to 6

x = Row vector with


x = 1.2:0.2:2 1.2000 1.4000 1.6000 1.8000 2.0000 stepsize 0.2

x = Row vector with de-


x = 10:-2:1 10 8 6 4 2 creasing stepsize −2

If we want to specify the number of entries in the vector (rather than the step between each
entry), then the linspace command is more convenient. The general form of the command is
linspace(<start>:<end>:<number of entries>)
For example: x = linspace(1.2,2,5) produces
x =
1.2000 1.4000 1.6000 1.8000 2.0000

a row vector with 5 equally spaced entries between 1.2 and 2.

Generating Matrices
We can also generate matrices by using the built-in MATLAB functions. For example, the
command

B=rand(4)

generates a 4 × 4 matrix whose entries are uniformly distributed random numbers between 0
and 1. The command

A=diag([1,7,5,3])

generates a 4 × 4 diagonal matrix with entries 1, 7, 5, 3 on the diagonal. Here is a list of the
most common built-in matrices:
rand(m,n) m by n matrix with random numbers between 0 and 1
eye(m,n) m by n matrix with 1’s on the main diagonal
zeros(m,n) m by n matrix of zeros
ones(m,n) m by n matrix of ones
triu(A) extracts the upper triangular part of the matrix A
tril(A) extracts the lower triangular part of the matrix A
diag(v,k) square matrix with the vector v on the kth diagonal

The first four commands above with a single argument, e.g. ones(m), produce a square matrix
of dimension m.
More special matrices:
There is also a set of built-in special matrices such as magic, hilb, pascal, toeplitz, and
vander.

The matrix building commands can be used to generate block of partitioned matrices. Here is

THIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED
2021 v22 Copyright@ School of Mathematical and Statistical Sciences, Arizona State University
4
an example:
Input Output

E =
E=[eye(2),ones(2,3);zeros(2),[1:3; 3:-1:1]]
1 0 1 1 1
0 1 1 1 1
0 0 1 2 3
0 0 3 2 1
 
1 2 3
Note that the command [1:3; 3:-1:1] generates the 2 × 3 matrix .
3 2 1

Addition and Multiplication of Matrices


Matrix arithmetic in MATLAB is straightforward. We can multiply our original matrix A times
B simply by typing A*B. The sum and difference of A and B are given by A + B and A - B,
respectively. The transpose of the real matrix A is given by A’.

Exponentiation
Powers of matrices are easily generated. The matrix A5 is computed in MATLAB by typing
A^5. We can also perform operations element-wise by preceding the operand by a period.
For instance, if V=[1,2; 3,4], then
Input Output
Equivalent to V*V
ans =
V^2
7 10
15 22

component-wise exponentiation
ans =
V.^2
1 4
9 16

Appending a row or a column


A row can be easily appended to an existing matrix provided the row has the same length of
the rows of the existing matrix. The same thing goes for the columns. The command A=[A, v]
appends the column vector v to the columns of A, while A = [A; u] appends the row vector
u to the rows of A.
   
1 0 0   2
Examples: If A = 0 1 0, u = 5 6 7 , and v = 3, then
0 0 1 4
 
1 0 0
0 1 0
• C = [A; u] produces C =  0 0 1, a 4 × 3 matrix

5 6 7
 
1 0 0 2
• D = [A, v] produces D = 0 1 0 3, a 3 × 4 matrix.
0 0 1 4

THIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED
2021 v22 Copyright@ School of Mathematical and Statistical Sciences, Arizona State University
5
Deleting a row or column
Let A=[1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15], then
Input Output
A(2,:) = [] A = deletes the 2nd row of matrix A
1 2 3 4 5
11 12 13 14 15

A(:,3:5) = [] A = deletes the 3rd through 5th


1 2 columns of A
6 7
11 12

A([1,3],:) = [] A = deletes the 1st and 3rd row of A


6 7 8 9 10

Columnwise Array Operators


MATLAB has a number of functions that, when applied to either a row or column vector x,
returns a single number. For example, the command max(x) will compute the maximum entry
of x , and the command sum(x) will return the value of the sum of the entries of x. Other
functions of this form are min, prod, mean. When used with a matrix argument, these functions
are applied to each column vector and the results are returned as a row vector.
 
−3 2 5 4
For example if A =  1 3 8 0 , then
−6 3 1 3
Input Output
min(A) ans = minimum entry in each column of A
-6 2 1 0

max(A) ans = maximum entry in each column of A


1 3 8 4

sum(A) ans = sum of the entries in each column of A


-8 8 14 7

prod(A) ans = product of the entries in each column of A


18 18 40 0

THIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED
2021 v22 Copyright@ School of Mathematical and Statistical Sciences, Arizona State University
6
EXERCISES
Use the provided livescript template for your lab write-up. Unless otherwise specified, your
write-up should contain the MATLAB input commands, the corresponding output, and the
answers to the questions that you have written. Once you have completed all the exercises,
make sure you save the livescript in pdf format and submit the pdf in canvas.

1. Matrix Algebra: Enter the following matrices:


     
5 −3 −1 2.5 3.2 3.4 −4 −6
A=  3 3 −3  B=  3.9 −0.2 0.1  , C= 4 0 
4 0 5 3.8 2.8 2.3 2 3

Compute the following:

(i) 3(A + B)
(ii) 3 + C
(iii) AC
(iv) AB
(v) CA
(vi) 3A + 3B
(vii) A + C
(viii) B + A
(ix) BA
(x) A + B

(a) Did MATLAB refuse to do any of the requested calculations? If so, which ones and
why?
(b) What did 3+C do?
(c) Does A + B = B + A?
(d) Does AB = BA?
(e) Does 3(A + B) = 3A + 3B ?

2. Check some linear algebra rules: Enter the following matrices:


     
2 1 −4 8 2 −6
A= , B= , C=
−4 −2 3 −6 1 −3

The following rules of algebra hold for real numbers. However, most of them are false
for matrices. Use the matrices A, B, C above to perform the appropriate operations in
MATLAB and determine which rules are false. Below, 0 denotes the 2 × 2 zero matrix.
Note that if a rule holds for these particular matrices, it does not mean that it is true in
general. However, you should be able to determine that from the theory you have learned
in class.

(i) If A2 = 0, then A = 0
(ii) (AB)2 = A2 B 2
(iii) (A + B)2 = A2 + 2AB + B 2

THIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED
2021 v22 Copyright@ School of Mathematical and Statistical Sciences, Arizona State University
7
(iv) A(B + C) = AB + AC
(v) (A − B)(A + B) = A2 − B 2
(vi) If BC = 0, then B = 0 or C = 0
(vii) A(B + C) = BA + CA

3. The transpose of a matrix: The transpose of a matrix A, denoted by AT , can be


computed in MATLAB using A’. A matrix A is symmetric if AT = A.
Enter the following matrices:
     
0 1 −2 6 −3 −1 −3
A= , B= , C=
5 0 6 2 4 3 5

Compute the following

(i) C T A
(ii) (AT )T
(iii) AC T
(iv) B T AT
(v) AT B T
(vi) B T
(vii) (AB)T

(a) Did MATLAB refuse to do any of the requested calculations? If so, which ones and
why?
(b) Does (AB)T = AT B T ? Does (AB)T = B T AT ?
(c) What is the relationship between (AT )T and A?
(d) Is B symmetric? Why or why not?

4. Matrix-matrix multiplication: Generate two 3 × 3 random matrices with integer en-


tries with the commands:

R =round(10*rand(3)), S=round(10*rand(3))

The command rand(3) generates a random 3 × 3 matrix with entries in between 0 and
1. We multiply that matrix by 10 so that it has entries between 0 and 10 and then we
use the command round to round to the nearest integer.
Compute the following:

(i) [R*S(:,1), R*S(:,2), R*S(:,3)]


(ii) [R(1,:)*S; R(2,:)*S; R(3,:)*S ]
(iii) Compare the results of parts (i) and (ii) to the product R*S
(iv) Explain how the matrices in (i) and (ii) are generated.

5. Create matrices with eye, ones, diag and triu: Create the following matrices with
the help of the matrix generation functions eye , ones, diag and triu. See the on-line
help on these functions if required (i.e. help eye).
       
8 8 8 5 0 0 7 0 0 6 6
M = 0 8 8 , N = 0 5 0 , P = 0 8 0 , Q = 6 6
0 0 8 0 0 5 0 0 9 6 6

THIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED
2021 v22 Copyright@ School of Mathematical and Statistical Sciences, Arizona State University
8
6. Create a big matrix with submatrices: The following matrix G is created by inserting
the matrices A, B, and C from Exercise 3, together with zero matrices and 2 × 2 identity
matrices in the appropriate position. Create the matrix using submatrices A, B, C, zeros
and eye (that is, you are not allowed to enter the numbers explicitly).
 
1 0 −3 −1 −3 0 1
 0 1 4 3 5 5 0 
G= −2

6 0 0 0 1 0 
6 2 0 0 0 0 1
7. Manipulate a matrix: Do the following operations on matrix G created above in
Problem 6.
(a) Extract the 3 × 3 submatrix of G consisting of columns 5 through 7 and rows 1
through 3 and store it in the matrix H, that is, create a matrix
 
−3 0 1
H= 5 5 0 
0 1 0
by extracting the appropriate rows and columns from the matrix G.
(b) Create the matrix E obtained from H by replacing H13 = 1 by 6. Do not enter
E explicitly. Hint: enter first E=H; to create a copy of the matrix H and then
manipulate the matrix E. The resulting matrix should be
 
−3 0 6
E= 5 5 0 
0 1 0
(c) Create the matrix F obtained by deleting the second row of the matrix H. Do not
enter F explicitly.
(d) What happens if you type G(:,:) and hit return? Do not include the output in
your lab report, but include a statement describing the output in words.
What happens if you type G(:) and hit return? Do not include the output in your
lab report, but include a statement describing the output in words.
(e) What happens if you type G(6,4) and hit return? Explain.
(f) What happens if you type max(G)? Explain. What happens if you type sum(G)?
Explain.
(g) What happens if you type G(G>3) and hit return? Can you explain how MATLAB
got that answer? What happens if you type G(G>3) = 100 and hit return? Can you
explain how MATLAB got that answer?
8. Perform row operations: The three elementary row operations can be performed in
MATLAB using the following commands
Type I: A([i,j],:)=A([j,i],:) interchanges row i and row j
Type II: A(i,:)=α*A(i,:) multiplies row i by α
Type III: A(i,:)=A(i,:)+ α*A(j,:) multiplies row j by α and adds it to row i
Enter the following matrix:
 
4 3 5
A = −16 −15 −16 
12 3 28
Perform row operations in MATLAB that reduce the matrix A to Row Echelon Form.
Use format rat.

THIS CONTENT IS PROTECTED AND MAY NOT BE SHARED, UPLOADED, SOLD OR DISTRIBUTED
2021 v22 Copyright@ School of Mathematical and Statistical Sciences, Arizona State University
9

You might also like