You are on page 1of 15

% Math 550 MATLAB Lab Assignment #1

% Initializing Random Number Generator


rand('seed',5230)
% Checking newly created script/function 'rvect()'
v = rvect(3)
v =
5
3
5
u =rvect(3)
u =
0
0
3
% Checking newly created script/function 'rmat()'
A = rmat(3,5)
A =
5
3
1

8
9
2

9
2
7

6
7
5

8
1
0

% Setting format of Diary


format compact
% Question 1 (a)
% Generating a random 3x4 matrix
A = rmat(3,4)
A =
7
1
3

1
8
9

1
0
1

5
1
4

% Creating a variable R and setting it equal to A


R = A
R =
7
1
3

1
8
9

1
0
1

5
1
4

% Normalizing R(1,1)
R(1,:) = R(1,:)/R(1,1)
R =
1.0000
1.0000

0.1429
8.0000

0.1429
0

0.7143
1.0000

3.0000

9.0000

1.0000

4.0000

% Setting R(2,1)=0 by row operation


R(2,:) = R(2,:) - R(2,1)*R(1,:)
R =
1.0000
0
3.0000

0.1429
7.8571
9.0000

0.1429
-0.1429
1.0000

0.7143
0.2857
4.0000

% Setting R(3,1)=0 by row operation


R(3,:) = R(3,:) - R(3,1)*R(1,:)
R =
1.0000
0
0

0.1429
7.8571
8.5714

0.1429
-0.1429
0.5714

0.7143
0.2857
1.8571

0.1429
-0.0182
0.5714

0.7143
0.0364
1.8571

% Question 1 (b)
% Normalizing R(2,2)
R(2,:) = R(2,:)/R(2,2)
R =
1.0000
0
0

0.1429
1.0000
8.5714

% Setting R(1,2)=0 by row operation


R(1,:) = R(1,:) - R(1,2)*R(2,:)
R =
1.0000
0
0

0
1.0000
8.5714

0.1455
-0.0182
0.5714

0.7091
0.0364
1.8571

% Setting R(3,2)=0 by row operation


R(3,:) = R(3,:) - R(3,2)*R(2,:)
R =
1.0000
0
0

0
1.0000
0

0.1455
-0.0182
0.7273

0.7091
0.0364
1.5455

0.1455
-0.0182
1.0000

0.7091
0.0364
2.1250

% Question 1 (c)
% Normalizing R(3,3)
R(3,:) = R(3,:)/R(3,3)
R =
1.0000
0
0

0
1.0000
0

% Setting R(1,3)=0 by row operation


R(1,:) = R(1,:) - R(1,3)*R(3,:)
R =
1.0000
0

0
1.0000

0
-0.0182

0.4000
0.0364

1.0000

2.1250

% Setting R(2,3)=0 by row operation


R(2,:) = R(2,:) - R(2,3)*R(3,:)
R =
1.0000
0
0

0
1.0000
0

0
0
1.0000

0.4000
0.0750
2.1250

% Question 1 (d)
% Checking if calculated R is same as obtained by using rref(A)
rref(A)
ans =
1.0000
0
0

0
1.0000
0

0
0
1.0000

0.4000
0.0750
2.1250

% R obtained manually is same as rref(A)


% Question 1 Complete!
% Question 2 (a)
%
%
A
A

Generating a random Matrix A


Generating a random 3x3 Matrix A
= rand(3)
=

0.5137
0.3300
0.5678
% Setting U =
U = A
U =

0.0892
0.0996
0.3001
A

0.5139
0.3829
0.1927

0.5137
0.3300
0.5678

0.0892
0.0996
0.3001

0.5139
0.3829
0.1927

% Question 2 (b)
% What row operations are applied to the identity matrix to obtain the matrix L1
?
% Ans: To find L1, we set L1 to be identity matrix and append its inverse to U o
n the left side.
% Now we perform row operation on this [inv(L) U] matrix to set the entries belo
w diagonal of U to be zero.
col1
L1 =
1.0000
0.6424
1.1052

0
1.0000
0

0
0
1.0000

% How are the matrix entries of inv(L1) related to the those of L1?
% Since L1 is lower triangular and has entries in only its first column, its inv
erse has same values but

% with signs changed below the diagonal.


inv(L1)
ans =
1.0000
-0.6424
-1.1052
%
%
U
U

0
1.0000
0

0
0
1.0000

How is the new value of U obtained from the old value of U by row operations?
By simply row operating on U to get U(2,1) = 0 & U(3,1) = 0
= inv(L1)*U
=
0.5137
0
0

0.0892
0.0423
0.2015

0.5139
0.0528
-0.3752

% Question 2 (c)
%How are the matrix entries of inv(L2) obtained from those of L2?
% Since L2 is lower triangular and has entries on in one column below diagonal,
its inverse has same
% values but with signs changed below the diagonal.
inv(L1)
col2
L2 =
1.0000
0
0
inv(L2)
ans =

0
1.0000
4.7679

0
0
1.0000

1.0000
0
0

0
1.0000
-4.7679

0
0
1.0000

%
%
U
U

How is the new value of U obtained from the old value of U by row operations?
By simply row operating on U to get U(3,2) = 0
= inv(L2) * U
=
0.5137
0
0

0.0892
0.0423
0

0.5139
0.0528
-0.6269

% Question 2 (d)
% How are the entries of L obtained from those of L1 and L2?
% Since L1 and L2 are lower triangular with 1's on the diagonal and L1 has non z
ero entries in the
% first column below diagonal and L2 has non zero entries in the second column b
elow diagonal,
% multiplying L1 and L2 just simply collect the terms below diagonal from L1 and
L2.
L= L1 * L2
L =
1.0000
0.6424

0
1.0000

0
0

1.1052
L * U
ans =

4.7679

1.0000

0.5137
0.3300
0.5678

0.0892
0.0996
0.3001

0.5139
0.3829
0.1927

0.5137
0.3300
0.5678

0.0892
0.0996
0.3001

0.5139
0.3829
0.1927

A
A =

% Question 2 Complete!.
% Question 3 (a)
% Which entries in inv(L) and inv(U) must be 0?
% Ans: All entries above the diagonal of matrix inv(L) are 0. All entries below
the diagonal of matrix
% inv(U) are 0
% Which entries in inv(L) and inv(U) must be 1?
% Ans: In both matrices inv(L) and inv(U) will have 1's on the diagonal.
L
L =
1.0000
0.6424
1.1052

0
1.0000
4.7679

0
0
1.0000

0
1.0000
-4.7679

0
0.0000
1.0000

inv(L)
ans =
1.0000
-0.6424
1.9576

% For the matrices L1 and L2, you found in Question #2 that the inverse matrices
are simply
% obtained by putting a minus sign in front of the entries below the main diagon
al.
% Does this method give the correct inverse matrix for the matrix L that you gen
erated?
% Ans: This method does not work with L has nonzero entries in both columns belo
w the main diagonal.
% It is also because L1 * L2 is equal to L2 * L1.
% Question 3 (b)
b = rand(3,1)
b =
0.8147
0.9058

0.1270
c = inv(L) * b
c =
0.8147
0.3824
-2.5968
x = inv(U) * c
x =
-3.2299
3.8746
4.1421
A * x
ans =
0.8147
0.9058
0.1270
b
b =
0.8147
0.9058
0.1270
% It is quite clear from above results that Axis infact equal to b.
% Question 3 (c)
% Calculation L & U before the solution for x by LU decomposition.
A = rand(500);
b = rand(500,1);
[L U] = lu(A);
% Measuring calculation time by RREF method.
tic; R = rref([A b]); y = R(:,501); toc
Elapsed time is 11.476508 seconds.
rreftime = 11.476508;
% Measuring calculation time by LU decomposition method.
tic; c = inv(L) * b; x = inv(U) * c; toc
Elapsed time is 0.045623 seconds.
lutime = 0.045623;
% Making sure the asnwer of both is coming out to be same.
norm(x-y)
ans =
3.2315e-12

% i) Ratio of the number of calculation by the given two methods comes out to be
n/3.
500/3
ans =
166.6667
% ii) Actual measure ratio of the calculation times are calculated as
rreftime/lutime
ans =
251.5509
% There is considerable difference between the estimated ratio of the times of t
he
% calculations performed in both methods. As stated in the lab guide, LU decompo
sition
% is still considerably faster than RREF method.
% Question 3 Complete!
% Question 4 (a)
% Generating a random 3x3 integer matrix.
B = rmat(3,3)
B =
8
9
2

9
2
7

6
7
5

% Confirming that rank of B is actually 3.


rank(B)
ans =
3
% Defining A as a 3x5 matrix with columns 3 & 4 as linear combination of columns
1 & 2 of B.
A = [B(:,1), B(:,2), 2 * B(:,1) + 3 * B(:,2), 4 * B(:,1)- B(:,2), B(:,3)]
A =
8
9
2

9
2
7

43
24
25

23
34
1

6
7
5

% Explain why you could predict the location of the pivot columns in R and all t
he entries in the
% nonpivot columns of R directly from the definition of A without any calculatio
n. (Hint: Use the linear
% correspondence principle for relations between columns of A and columns of R.)
% Ans: The column correspondence principle (CCP) states that any linear dependen

ce of the columns
% of A is also found in the columns of rref(A). Hence we can predict the locatio
ns of the pivot columns
% in rref(A) and also depending upon how the columns are dependent,we can predic
t the values of the
% entries of nonpivot volumns.
% Calculating the RREF of the newly formed matrix A.
R = rref(A)
R =
1
0
0

0
1
0

2
3
0

4
-1
0

0
0
1

% Question 4 (b)
% (i) In the system of linear equations Ax = 0, what are the free variables?
% Ans: Observing the rref(A), the free 3rd and 4th variables are the free variab
les.
%
%
f
%

(ii) What is dim V ?


Ans: Since V is the null space of A, its dimension is the same as the number o
free variables.
So the dim V = 2

% Question 4 (c)
N = nulbasis(A)
N =
-2
-3
1
0
0

-4
1
0
1
0

v1 = N(:,1)
v1 =
-2
-3
1
0
0
v2 = N(:,2)
v2 =
-4
1
0
1
0

% (i) Which component of v1 must be 1 for every random B?


% Ans: The component v1(3,1) will always be 1 regardless of any random value of
B.
% (ii) Which component of v1 must be 0 for every random B?
% Ans: The component v1(4,1) will always be 1 regardless of any random value of
B.
% The above two answers are based on the fact that these two vectors v1 & v2 for
m the basis of the
% null space of A. By their nature, the two vectors are independent of each othe
r.
% Similarly for v2
% (i) Which component of v2 must be 1 for every random B?
% Ans: The component v1(4,1) will always be 1 regardless of any random value of
B.
% (ii) Which component of v2 must be 0 for every random B?
% Ans: The component v1(3,1) will always be 1 regardless of any random value of
B.
% Conforming that v1 & v2 are in the null space of A.
A * v1
ans =
0
0
0
A * v2
ans =
0
0
0
% Question 4 (d)
% Generating a random coefficient for the vector v1 & v2 to confirm that any lin
ear
% conbination of v1 & v2 still exists in the null space of A.
x= rand(1) * v1 + rand(1) * v2
x =
-3.7970
-0.7812
0.4944
0.7020
0
A * x
ans =

1.0e-15 *
0
0
-0.4441
% (i) Explain (without Matlab) why x also satisfies Rx = 0. Then confirm this by
Matlab.
% Ans: Since we have defined R as the rref(A). Any basis that forms the null spa
ce of A,
% by principle, also lies in the null space of rref(A). This is because rref(A)
if formed
% only by performing row operation on A.
% Confirmation through Matlab
R * x
ans =
0
0
0
% Question 4 Compelete!
% Question 5 (a)
% Generating Coefficient matrix A for an underdetermined system
A = rmat(3,5)
A =
5
3
5

0
0
3

5
3
1

8
9
2

9
2
7

% Calculating its RREF


R = rref(A)
R =
1.0000
0
1.0000
0
3.0952
0
1.0000 -1.3333
0 -2.2857
0
0
0
1.0000 -0.8095
% It can be observed from rref(A) that 3rd and 5th variables are the free variab
les
% or 3rd and 5th columns are the nonpivot columns.
% Generating a random 3x1 matrix
b = rmat(3,1)
b =
6
7
5
% Finding x such that A * x = b where free variable are set to 0
x = partic(A, b)

x =
-0.0952
1.2857
0
0.8095
0
% Generating another vector b2.
b2 = rmat(3,1)
b2 =
8
1
0
% Finding x such that A * x = b where free variables are set to 0.
x = partic(A, b)
x =
-0.0952
1.2857
0
0.8095
0
%
%
%
%

(i) What entries in x are zero both times?


Ans: As expected, the 3rd and 5th entry of x is found out to be zero.
This is because the 3rd entry and the 5th entry are the values corresponding
to the free variable and we are setting free varaibles equal to 0.

% (ii) Which columns of R correspond to free variables?


% Ans: The 3rd and 5th columns of R correspond to free variables.
% Checking the rank of A and [A, b]
rank(A) , rank([A, b])
ans =
3
ans =
3
%
?
%
m
%

(iii) Does the equation Ax = b have a solution for every vector b in this case
Why?
Ans: Yes, the solution to A * x = b exists for every vector b, since the syste
is underdetermined and
the solution x is calculated keeping the free variables equal to 0.

% Question 5 (b)
% Generating the coefficient 5x3 matrix A for an overdetermined system.
A = rmat(5,3)
A =

9
1
0
1
5

1
4
7
2
6

1
0
4
7
3

% Generating a random 5x1 vector b.


b = rmat(5,1)
b =
7
1
3
1
8
% Attempting to find a particular solution to the overdetermined system.
x = partic(A,b)
x =
[]
% (i) Why is there no solution to Ax = b for a completely random choice of b?
% Ans: Since the system has more equations than the variables, the solution to t
his system only exists for special b's.
% For every other random b, the solution does not exist.
% Finding the ranks of A and the augmented [A, b] matrix.
rank(A)
ans =
3
rank([A, b])
ans =
4
% Generating a vector b that is linearly dependent on the vectors of A.
b = rand(1) * A(:,1) + rand(1) * A(:,2) + rand(1) * A(:,3)
b =
4.4233
3.1326
6.6351
4.8839
7.3194
x = partic(A,b)

x =
0.3648
0.6919
0.4479
% (ii) Explain why the special form of the vector b guarantees that there always
is a solution x for
% any choice of the random coecients.
% Ans: Since this special form of b is linearly dependent on the vectors of A, t
his reduces the rank of
% the augmented matrix and allows a solution of the system.
% (iii) Since there is a solution to Ax = b for this b, what is the rank of the
augmented matrix [A, b]?
% Ans: Since b is not linearly independent of columns of A, the rank of the augm
ented matrix shall be reduced to 3.
% Checking the rank of the augmented matrix [A,b].
rank([A, b])
ans =
3
% As expected, rank([A, b])=3.
% Question 5 (c)
A = rmat(3,5)
A =
4
7
3

1
5
7

2
4
0

2
0
0

8
5
0

b = rmat(3,1)
b =
9
6
0
N = nulbasis(A)
N =
-1.7500
0.7500
2.1250
1.0000
0

-4.8125
2.0625
4.5938
0
1.0000

% Finding a particular solution.


xp = partic(A,b)
xp =
5.2500

-2.2500
-4.8750
0
0
v1 = N(:,1)
v1 =
-1.7500
0.7500
2.1250
1.0000
0
v2 = N(:,2)
v2 =
-4.8125
2.0625
4.5938
0
1.0000
% Finding a general solution with random coefficients of basis of the nullspace.
x = xp + rand(1) * v1 + rand(1) * v2
x =
3.4870
-1.4944
-2.7485
0.9761
0.0114
A * x - b
ans =
1.0e-14 *
-0.1776
-0.0888
0
% A * x - b ~ 0 which signifies that x is the solution for Ax=b.
% Since any values can be assigned to the coefficients of the basis of nullspace
and the particular
% solution has entries corresponding to free variable set to bezero, we can get
the desired form of
% the solution by assigning -9 and 8 to the coefficients.
x = xp - 9 * v1 + 8 * v2
x =
-17.5000
7.5000
12.7500

-9.0000
8.0000
% Checking if the solution satisfies the equation A x - b = 0.
A * x - b
ans =
0
0
0
% The particular selected value of the solution indeed specifies the equation Ax
=b.
% All values of the coefficients of the vectors of the nullspace satisfies this
equation.
% Question 5 Complete!

You might also like