SIN1001: MATLAB Tutorial 2: Help Lookfor
SIN1001: MATLAB Tutorial 2: Help Lookfor
It is recommended that you write down the expect outputs before you run the following
statements in MATLAB. Confirm your answers by executing them in MATLAB. Also, use
the commands help, doc, and/or lookfor whenever you need more info about some com-
mands or functions.
1. Initialize the matrices
3 1 4 2 6 5 9 7 9
A= , B= , and C =
1 5 9 3 5 8 3 2 3
and compute each of the following matrices, provided it is well defined, in MATLAB:
(a) A + B + C
(b) AB T
(c) BAT
(d) AB
Warning: Note that A.' and A' are the same, but B.' and B' are different. Why?
4. Determine the sizes of the following matrices in MATLAB. Note that the later matrices
may depend on the definitions of matrices given earlier in this exercise.
(a) u = [10 20*i 10+20];
(b) v = [-1; 20; 3];
(c) w = [1 0 -9; 2 -2 0; 1 2 3];
(d) x = [u' v];
(e) y(3,3) = -7;
(f) z = [zeros(4,1) ones(4,1) zeros(1,4)'];
(g) v(4) = x(2,1);
What are the values of w(2,1), x(2,1), y(2,1), and v(3) after the statements above
have been executed?
Note that the content of c may depend on the operations on c run earlier in this exercise.
(a) What is the size of c?
(b) What are the contents of the following sub-matrices:
i. c(2,3)
ii. c(2,:)
iii. c(:,end)
iv. c(6)
v. c(4:end)
vi. c(1:2,2:end)
vii. c([1 3], 2)
viii. c([2 2], [3 3])
4.5
(c) Append the column vector −2.1 to the c.
0.3
(d) Replace the second last row of c with the row vector 1.2 2.3 3.4 4.5 3.2 .
(e) Insert the row vector 1.4 2.3 −3.1 2.3 3.4 as the third row of c.
Page 2
(f) Find out the indices of all entries of c each of whose value is 0.6.
(g) Find out the indices of all entries of c each of whose value lies between 0.5 and
1.5 inclusively.
Note: The function find can be used to find out the indices of all entries with a specified
value or within a specified range of values. For part (f), we can type [i j] = find(c==0.6).
For part (g), we can type [i j] = find((c>=0.5)&(c<=1.5)). The operator & is
a logical AND operator.
https://www.mathworks.com/help/matlab/ref/find.html
6. The capacity to define a new variable using already defined variables as parts makes it
easy to construct a recursive sequence in MATLAB. For instance, Fibonacci numbers
{Fn }∞
n=0 are defined recursively in accordance with the following relation:
(a) Define a row vector F containing the first two Fibonacci numbers F0 and F1 .
(b) Re-define F by typing F = [F F(end)+F(end-1)]. What is the content of F(end)
after the execution of the statement?
(c) We will talk about for loop later. However, it suffices to know that the following
block of statements simply means we re-do part (b) for another ten times.
for m = 1:10
F = [F F(end)+F(end-1)];
end
What is the content of F(end) after the execution of the above for loop?
a = 20; b = -2; c = 0; d = 1;
in the MATLAB Command Window. What is the returned value of each of the following
MATLAB expressions?
(a) a > b
Page 3
(b) b > d
(c) a > b && c > d
(d) a ˜= b
(e) a && b > c
(f) ˜˜b
>> a = 2;
>> b = [1 -2; 0 10];
>> c = [0 1; 2 0];
>> d = [-2 1 2; 0 1 0];
in the MATLAB Command Window. What is the output of each of the following MAT-
LAB expressions?
(a) ˜(a > b)
(b) a > c && b > c
(c) a > c & b > c
(d) c <= d
(e) logical(d)
3x + 8y + z = 62,
2x − y − 3z = −5,
4x + y − 2z = 21.
(a) In matrix notation, this system has the form Ax = b, where x is a column vector
of unknowns. Define the coefficient matrix A and the non-homogeneous term b in
MATLAB.
(b) Solve the system using \, linsolve, and inv.
(c) Solve the system with the aid of rref to obtain the reduced row echelon form of
the augmented matrix [A b].
x − 2y + z = −2,
2x − 5y + z = 1,
3x − 7y + 2z = −1.
Page 4
(a) Define the coefficient matrix A and the non-homogeneous term b in MATLAB as
in the previous problem. Then compute the determinant of A by typing det(A).
(b) Try to solve the linear system using \, linsolve, or inv. What warnings do you
get from MATLAB?
(c) Solve the linear system with the aid of rref.
x + 2y + 3z = 1,
3x + 6y + 9z = 1,
4x + 5y + 6z = 1.
(a) Define the coefficient matrix A and the non-homogeneous term b in MATLAB as
in the previous problem. Then compute the determinant of A by typing det(A).
(b) Try to solve the linear system using \, linsolve, or inv. What warnings do you
get from MATLAB?
(c) Solve the linear system, if possible, with the aid of rref.
Page 5