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

SIN1001: MATLAB Tutorial 2: Help Lookfor

This document provides a series of MATLAB exercises involving matrix operations and solving systems of linear equations. The exercises cover topics such as: - Defining and manipulating matrices - Computing transposes, submatrices, and other operations - Finding solutions to well-posed and singular systems of linear equations using backslash, linsolve, inv, and rref. - Recursively generating the Fibonacci sequence - Evaluating logical expressions and determining outputs

Uploaded by

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

SIN1001: MATLAB Tutorial 2: Help Lookfor

This document provides a series of MATLAB exercises involving matrix operations and solving systems of linear equations. The exercises cover topics such as: - Defining and manipulating matrices - Computing transposes, submatrices, and other operations - Finding solutions to well-posed and singular systems of linear equations using backslash, linsolve, inv, and rref. - Recursively generating the Fibonacci sequence - Evaluating logical expressions and determining outputs

Uploaded by

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

SIN1001: MATLAB Tutorial 2

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: What is the error message?

2. Suppose A = [9 8 7; 3 2 1] and B = [i 2+i 5-3i; 2 -i 7+i]. What are


the outputs of the following statements?
(a) A.'
(b) A'
(c) B.'
(d) B'

Warning: Note that A.' and A' are the same, but B.' and B' are different. Why?

3. Determine the contents of a = [1 2 3; 4 5 6; 7 8 9]; after each of the follow-


ing statements is executed.
(a) a([3 1],:) = a([1 3],:);
(b) a([1 3],:) = a([2 2],:);
(c) a = a([2 2],:)
(d) a(:,:) = -1
(e) a = -1

Note: You can initialize a by typing a=reshape(1:9, 3, 3).'


Warning: Note the difference of parts (d) and (e).

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?

5. Define the following matrix in MATLAB as c:


 
1.1 −3.2 3.4 0.6
0.6 1.1 −0.6 3.1 .
1.3 0.6 5.5 0.0

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.

MATLAB documentation by MathWorks on find is available at

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:

Fn = Fn−1 + Fn−2 where F0 = 0 and F1 = 1.

(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?

Note: You may want to use a script m-file.


(d) Find out the value of F30 by modifying the above block of statements.

7. Suppose one has entered

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

8. Suppose one has

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

9. Consider the following linear system of equations.

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

10. Consider the following linear system of equations

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

Warning: In fact, A is singular and thus det(A) should be 0. However, MATLAB


is likely to give you only a number very close to 0 with a warning.

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

11. Consider the following linear system of equations

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

You might also like