You are on page 1of 9

format compact

% Boris Yekaterinoslavskiy
% 1342
% Section C3
% Math 250 MATLAB Lab Assignment #3
%
rand('seed', 1342)
%
% Question 1(a)
%
% We will begin by generating a random 3x3 matrix A and
% calculate its principal minors.
A = rand(3), A(1,1), det(A(1:2,1:2)), det(A)
A =
0.3238 0.5514 0.6294
0.9692 0.9865 0.7896
0.2306 0.7936 0.5374
ans =
0.3238
ans =
-0.2150
ans =
0.1229
% The three minors above are nonzero, so A = LU
% factorization is possible.
% We create a matrix U and set it equal to A so we can
% reduce it to an upper triangular matrix later on.
U = A;
% Using the col1.m file, we can create a lower triangular
% unit matrix based on U.
col1
L1 =
1.0000 0 0
-2.9933 1.0000 0
-0.7122 0 1.0000
% We use L1 above and multiply it with U to obtain the
% upper triangular matrix's first column of zeros below
% the main diagonal.
U = L1*U
U =
0.3238 0.5514 0.6294
-0.0000 -0.6639 -1.0942
0 0.4010 0.0892
% We used col1.m to perform row operations on a 3x3
% identity matrix using values from the old U to get a new
% U. The first operation is to take the second row and
% subtract the first row multiplied by u21/u11, into the
% second row. The second operation is to take the third
% row and subtract the first row multiplied by u31/u11,
% into the third row.
%
% Question 1(b)
%
% Now we will continue to reduce U so that the second
% column has a zero below the main diagonal. To do this,
% we use the col2.m file to produce L2 to multiply it with
% the matrix U from (a).
col2
L2 =
1.0000 0 0
0 1.0000 0
0 0.6040 1.0000
U = L2*U
U =
0.3238 0.5514 0.6294
-0.0000 -0.6639 -1.0942
-0.0000 0 -0.5717
% L2 is also a 3x3 identity matrix that we created to
% modify U. The only row operation is to take the third
% row and subtract the first row multiplied by u32/u22,
% into the third row.
% We can verify that U is indeed the upper triangular matrix
% of A by multiplying A with L2 and L1 in that order.
U = L2*L1*A
U =
0.3238 0.5514 0.6294
-0.0000 -0.6639 -1.0942
-0.0000 0.0000 -0.5717
%
% Question 1(c)
%
% To complete the A = LU factorization, we need the lower
% triangular matrix, L. We can take the inverses of L1 and
% L2 and multiply them together to form L.
inv(L1), inv(L2), L = inv(L1)*inv(L2)
ans =
1.0000 0 0
2.9933 1.0000 0
0.7122 0 1.0000
ans =
1.0000 0 0
0 1.0000 0
0 -0.6040 1.0000
L =
1.0000 0 0
2.9933 1.0000 0
0.7122 -0.6040 1.0000
% Now that we have L, we can multiply it with U and it
% should give us the original matrix A.
A = L*U
A =
0.3238 0.5514 0.6294
0.9692 0.9865 0.7896
0.2306 0.7936 0.5374
%
% Question 2(a)
%
% To find the inverse of L, we know that from 1(c),
% L = inv(L1)*inv(L2). Using one of the inverse properties,
% we know that inv(A*B) = inv(B)*inv(A) and that the inverse
% of an inverse is the original matrix. Therefore, to find
% the inv(L), we take L2 and multiply it by L1.
L2*L1
ans =
1.0000 0 0
-2.9933 1.0000 0
-2.5200 0.6040 1.0000
% We can confirm with inv(L) below.
inv(L)
ans =
1.0000 0 0
-2.9933 1.0000 0.0000
-2.5200 0.6040 1.0000
% We can also find the inverse of U below.
inv(U)
ans =
3.0884 2.5651 -1.5095
0.0000 -1.5063 2.8831
-0.0000 -0.0000 -1.7492
% We can note that the inverses of both matrices are still
% in their respective triangular forms.
%
% Question 2(b)
%
% We will generate a random vector b in R3, using the
% rvect function from the second lab.
b = rvect(3)
b =
1
8
5
% We can find a solution c from L*c = b by multiplying the
% inverse of L with b.
c = inv(L)*b
c =
1.0000
5.0067
7.3118
% Using c, we can calculate the solution x from U*x = c.
x = inv(U)*c
x =
4.8936
13.5387
-12.7897
% x should be the solution to A*x = b. To confirm this, we
% can multiply A with x and our result should match b.
A*x
ans =
1.0000
8.0000
5.0000
%
% Question 3(a)
%
% We will generate a random matrix A and a vector b both
% in R500. We will also calculate the LU decomposition of A.
A = rand(500); b = rand(500,1); [L U] = lu(A);
% We will rref the augmented matrix [A b] and set y as the
% reduced column b.
tic; R = rref([A b]); y = R(:,501); toc
Elapsed time is 3.984523 seconds.
% We will define the elapsed time as rreftime.
rreftime = 3.984523;
%
% Question 3(b)
%
% Now we will solve A*x = b using the LU decomposition
% method and record the calculation time as well.
tic; c = inv(L)*b; x = inv(U)*c; toc
Elapsed time is 0.158759 seconds.
% We will set the elapsed time as lutime.
lutime = 0.158759;
% We can check to see if x and y are the same up to a
% certain round-off error.
norm(x - y)
ans =
3.2757e-11
% We can see that the error is negligible and thus, x = y.
%
% Question 3(c)
%
% If the calculation time for Gaussian elimination method
% and LU decomposition method are 2*c*(n^3)/3 and
% 2*c*(n^2) respectively, then the theoretical ratio for
% both times (rreftime/lutime) is n/3.
% The theoretical ratio for n = 500 is:
500/3
ans =
166.6667
% The observed ratio using the times from (a) and (b) is:
rreftime/lutime
ans =
25.0979
% The difference between the theoretical and observed
% ratios differ by a factor of 6, but that can be expected
% with matrices in Rn with a very large n.
%
% Question 4(a)
%
% The function file cofactor.m calculates the matrix of
% cofactors of a square matrix. We will generate a random
% 4x4 matrix using rmat function from the second lab and
% use the cofactor function to calculate the matrix from it.
a = rmat(4,4)
a =
2 4 9 5
9 5 0 3
2 4 1 5
7 3 8 4
c = cofactor(a)
c =
-93.0000 297.0000 78.0000 -216.0000
8.0000 216.0000 0 -176.0000
5.0000 -177.0000 -78.0000 280.0000
104.0000 -312.0000 0 208.0000
% Now we can calculate four sums below.
a(1,1)*c(1,1) + a(1,2)*c(1,2) + a(1,3)*c(1,3) + a(1,4)*c(1,4)
ans =
624
a(2,1)*c(2,1) + a(2,2)*c(2,2) + a(2,3)*c(2,3) + a(2,4)*c(2,4)
ans =
624.0000
a(1,3)*c(1,3) + a(2,3)*c(2,3) + a(3,3)*c(3,3) + a(4,3)*c(4,3)
ans =
624.0000
a(1,4)*c(1,4) + a(2,4)*c(2,4) + a(3,4)*c(3,4) + a(4,4)*c(4,4)
ans =
624
% The above four sums all solve for the determinant of the
% 4x4 matrix a. All four sums represent the cofactor
% expansion of each row. Therefore, no matter what row we
% can expand on, we will get the same answer for the
% determinant of a.
% We can check using the internal MATLAB function det to see
% if the result is the same.
det(a)
ans =
624.0000
%
% Question 4(b)
%
% We will generate a random 5x5 matrix and its upper
% triangular matrix.
A = rmat(5,5), U = triu(A)
A =
0 8 5 7 0
2 0 3 5 6
8 9 5 5 9
1 0 2 8 6
2 5 3 4 0
U =
0 8 5 7 0
0 0 3 5 6
0 0 5 5 9
0 0 0 8 6
0 0 0 0 0
% We can calculate the product of the diagonals for both
% matrices.
A(1,1)*A(2,2)*A(3,3)*A(4,4)*A(5,5)
ans =
0
U(1,1)*U(2,2)*U(3,3)*U(4,4)*U(5,5)
ans =
0
% According to Theorem 3.2, the determinant of an upper or
% lower triangular matrix is the product of its diagonal
% entries. So the second product above tells us that the
% determinant of U is 0. The product of the diagonal entries
% of A does not tell us anything about the determinant of A.
% Even though the product is also 0, it is because three of
% the diagonal entries are 0.
% We can confirm the determinants of both matrices below.
det(A), det(U)
ans =
-2.2620e+03
ans =
0
%
% Question 4(c)
%
% We will generate another 5x5 matrix A and then swap the
% first and second rows to get a matrix B.
A = rmat(5,5)
A =
4 2 7 1 3
0 2 1 2 8
5 2 4 2 4
2 3 9 8 0
6 2 1 8 4
B = A; B(2,:) = A(1,:); B(1,:) = A(2,:)
B =
0 2 1 2 8
4 2 7 1 3
5 2 4 2 4
2 3 9 8 0
6 2 1 8 4
% If we swap two rows of A, then the determinant of B is
% the opposite sign of A. We can confirm below.
det(A), det(B)
ans =
-1.0620e+03
ans =
1.0620e+03
% We will form a matrix C by multiplying the first fow of A
% by 10 and adding it to the second row.
C = A; C(2,:) = A(2,:) + 10*A(1,:)
C =
4 2 7 1 3
40 22 71 12 38
5 2 4 2 4
2 3 9 8 0
6 2 1 8 4
% Adding a row multiplied by a scalar to another row will
% not change the determinant of the matrix. We can confirm
% below.
det(A), det(C)
ans =
-1.0620e+03
ans =
-1.0620e+03
% We will form a matrix D by multiplying the first row of A
% by 10.
D = A; D(1,:) = 10*A(1,:)
D =
40 20 70 10 30
0 2 1 2 8
5 2 4 2 4
2 3 9 8 0
6 2 1 8 4
% If we multiply a row by a scalar, the determinant of the
% matrix will be multiplied by the same number.
det(A), det(D)
ans =
-1.0620e+03
ans =
-1.0620e+04
%
% Question 4(d)
%
% We will generate another random 5x5 matrix A. Then we will
% set a11 and a21 equal to zero. Using the splu.m custom
% function, we can calculate the PA = LU factorization,
% where P is the permutation matrix.
A = rmat(5,5)
A =
0 8 6 2 0
1 2 8 7 4
2 4 4 0 2
7 0 7 2 4
0 0 6 6 6
A(1,1)=0; A(2,1) = 0
A =
0 8 6 2 0
0 2 8 7 4
2 4 4 0 2
7 0 7 2 4
0 0 6 6 6
[P, L, U, sign] = splu(A)
P =
0 0 1 0 0
0 1 0 0 0
1 0 0 0 0
0 0 0 1 0
0 0 0 0 1
L =
1.0000 0 0 0 0
0 1.0000 0 0 0
0 4.0000 1.0000 0 0
3.5000 -7.0000 -1.8846 1.0000 0
0 0 -0.2308 0 1.0000
U =
2.0000 4.0000 4.0000 0 2.0000
0 2.0000 8.0000 7.0000 4.0000
0 0 -26.0000 -26.0000 -16.0000
0 0 0 2.0000 -5.1538
0 0 0 0 2.3077
sign =
-1
% The sign as -1 tells us that there was an odd number of
% row interchanges. We can check below that P*A = L*U.
P*A
ans =
2 4 4 0 2
0 2 8 7 4
0 8 6 2 0
7 0 7 2 4
0 0 6 6 6
L*U
ans =
2 4 4 0 2
0 2 8 7 4
0 8 6 2 0
7 0 7 2 4
0 0 6 6 6
% The determinant of P is:
det(P)
ans =
-1
% We can take a look at the matrix P and see that the only
% change from an identity matrix is that the first and
% third rows were swapped, which is one row interchange.
% The sign of det(P) tells us whether there was and even or
% odd amount of row interchanges, and det(P) can either be
% -1 or +1. Therefore, from one row interchange, det(P) = -1.
%
% The determinant of L should be 1 because the product of its
% diagonals is 1. We can check det(L) below.
det(L)
ans =
1
%
% The determinants of A and U are as followed:
det(A), det(U)
ans =
480.0000
ans =
-480.0000
% The relationship between both determinants are opposite
% signs for the same reason as mentioned earlier.
%
% Question 5(a)
%
% We will use the custom house.m and plot2d.m functions to
% make a crude drawing of a mouse, where the matrix H will
% contain the coordinates of its endpoints.
H = house; plot2d(H), hold on
% We will generate a matrix Q.
t = pi/6; Q = [cos(t), -sin(t); sin(t), cos(t)]
Q =
0.8660 -0.5000
0.5000 0.8660
% We can use the plot2d function so that H can be
% multiplied by Q and see how the house changes.
plot2d(Q*H)
% When we multiply Q*H, we can see on the plot that the
% house figure has rotated by an angle.
det(Q)
ans =
1
% Q is a rotation matrix. Multiplying it with H only rotates
% the shape that H produces. We know that det(A*B) is equal
% to det(A)*det(B). det(Q) = 1 tells us that the area of the
% house does not change since det(Q)*det(H) will equal det(H).
t = pi/3; Q = [cos(t), -sin(t); sin(t), cos(t)]
Q =
0.5000 -0.8660
0.8660 0.5000
plot2d(Q*H)
det(Q)
ans =
1
% The above is the process repeated with another rotation
% matrix. Since det(Q) is still 1, the area of the house
% does not change, even though it rotated.
%
% Question 5(b)
%
% We will generate a matrix D below.
plot2d(H), hold on
r = .9; D = [r, 0; 0, 1/r ]
D =
0.9000 0
0 1.1111
plot2d(D*H)
% By plotting D*H, we can see that the shape of the house
% changed. It's y values shifted by +1 and the overall width
% has decreased.
det(D)
ans =
1
% The determinant of D is 1. This tells us that even though
% the shape of the house has dilated, the area still remains
% the same.
r = .8; D = [r, 0; 0, 1/r ]
D =
0.8000 0
0 1.2500
plot2d(D*H)
det(D)
ans =
1
% We repeated the process in the above but with r = 0.8 this
% time. The shape of the house is now stretched vertically
% even further, but since det(D) is 1, the area of the shape
% of the house has not changed.
%
% Question 5(c)
%
% We will generate a matrix T below.
plot2d(H), hold on
t = 1/2; T = [1, t; 0, 1]
T =
1.0000 0.5000
0 1.0000
plot2d(T*H)
% By plotting T*H, we can see that the shape of the house
% has skewed.
det(T)
ans =
1
% Since the determinant of T is 1, this tells us that the area
% inside the house has not changed.
t = -1/2; T = [1, t; 0, 1]
T =
1.0000 -0.5000
0 1.0000
plot2d(T*H)
det(T)
ans =
1
% The above process of setting t = -1/2 has skewed the shape
% of the house in the opposite direction. Since det(T) is
% still equal to 1, the area of the house has not changed.

You might also like