lab1 (1)
lab1 (1)
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
• 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.
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.
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
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
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
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
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
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
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.
(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 ?
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
(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?
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:
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