Determinant and MatLab
Determinant and MatLab
Note that the command rand(3,5) generates a 3 5 matrix with random numbers. Note that each random number is between 0 and 1. The rand command selects numbers from a uniform distribution, so named because each random number between 0 and 1 has an equal probability of being selected. Technically, any number between 0 and 1, including 0, has an equal chance of being selected, but the number 1 has no chance of being selected. Therefore, each random number r selected by the rand command satisfies the inequality 0 r < 1. Suppose that you multiply each element of the matrix A by 10 to form the matrix B.
>> B=10*A B = 1.5638 1.2212 7.6267 7.2180 6.5164 7.5402 6.6316 8.8349 2.7216 4.1943 2.1299 0.3560 0.8116 8.5057 3.4020
Note that each entry of the matrix B could be floating point number between 0 and 10, possibly including 0 but not 10. That is, each entry b in the matrix B satisfies the inequality 0 b < 10. Subtract 5 from each element of the matrix B to form the matrix C.
>> C=B-5 C = -3.4362 -3.7788 2.6267 2.2180 1.5164 2.5402 1.6316 3.8349 -2.2784 -0.8057 -2.8701 -4.6440 -4.1884 3.5057 -1.5980
A Scientific Report
Note that each entry in the matrix C could be a floating point number between ?5 and 5, possibly including ?5 but not 5. There is an easy way to track the size of the random entries in the matrices A, B, and C. Remember, each element r of matrix A satisfied the inequality 0 r < 1. Then we multiplied by 10 and subtracted 5, as follows: 0r<1 0 10r < 10 ?5 10r ? 5 < 5 Consequently, the elements of matrix C can be random floating point numbers between ?5 and 5, possibly including ?5 but not 5.
ans = -4 -4 2 2 1 2 1 3 -3 -1 -3 -5 -5 3 -2
Note that each entry in C is rounded down to the nearest integer. ceil(X) rounds the elements of X to the nearest integers towards positive infinity.
>> C, ceil(C) C = -3.4362 -3.7788 2.6267 2.2180 1.5164 2.5402 1.6316 3.8349 -2.2784 -0.8057 -2.8701 -4.6440 -4.1884 3.5057 -1.5980
ans = -3 -3 3 3 2 3 2 4 -2 0 -2 -4 -4 4 -1
Note that each entry in C is rounded up to the nearest integer. round(X) rounds the elements of X to the nearest integers.
>> C, round(C) C =
A Scientific Report
ans = -3 -4 3 2 2 3 2 4 -2 -1 -3 -5 -4 4 -2
Note that each entry in C is rounded to the closest integer. Finally, fix(X) rounds the elements of X to the nearest integers towards zero.
>> C, fix(C) C = -3.4362 -3.7788 2.6267 2.2180 1.5164 2.5402 1.6316 3.8349 -2.2784 -0.8057 -2.8701 -4.6440 -4.1884 3.5057 -1.5980
ans = -3 -3 2 2 1 2 1 3 -2 0 -2 -4 -4 3 -1
Note that negative numbers are rounded up and positive numbers are rounded down.
Replace the first row of A with 5 times the first row of A. Then take the determinant of the result.
>> A(1,:)=5*A(1,:) A =
4
0 4 -3 >> det(A) ans = -220 20 2 4 0 5 -1
A Scientific Report
In the space that follows, explain what happens to the determinant of a matrix if you multiply its first row by 5. Conjecture.
How would your conjecture change if you multiplied (Try A=round(10*rand(3)-5),det(A),A(:,3)=5*A(:,3),det(A)) the third column by 5?
A Scientific Report
-8 >> det(A) ans = 10638 -9 -3 -5
In the space that follows, explain what happens to the determinant of a matrix if you exchange two rows. Conjecture.
How would your conjecture change if you exchanged two columns (Try A=round(20*rand(4)-10),det(A),A=A(:,[3 2 1 4]),det(A))?
Replace a Row with the Sum of Itself and a Scalar Multiple of Another Row
Enter the following matrix A. Then take the determinant of A.
>> A=[1 2 0;-2 1 3;4 1 1] A = 1 -2 4 >> det(A) ans = 26 2 1 1 0 3 1
Replace the second row of A with the sum of itself and 2 times the first row of A. Then take the determinant of the result.
>> A(2,:)=A(2,:)+2*A(1,:) A = 1 0 4 >> det(A) 2 5 1 0 3 1
6
ans = 26
A Scientific Report
The determinant is still 26. Lets replace the third row of A with the sum of itself and ?4 times the first row of A.
>> A(3,:)=A(3,:)-4*A(1,:) A = 1 0 0 >> det(A) ans = 26 2 5 -7 0 3 1
The determinant is still 26. Are we just lucky? Lets replace the third row of A with the sum of itself and 7/5 times the second row of A.
>> A(3,:)=A(3,:)+7/5*A(2,:) A = 1.0000 0 0 >> det(A) ans = 26.0000 2.0000 5.0000 0 0 3.0000 5.2000
This cant be luck! In the space provided below, explain what happens to the determinant of a matrix if you replace a row with the sum of itself and a scalar multiple of another row. Conjecture.
What would happen to the determinant if you replaced a column with the sum of itself and a scalar multiple of another column (Try A=[1 -2 0;3 1 1;2 2 2],det(A),A(:,2)=A(:,2)+2*A(:,1),det(A))?
Triangular Matrices
Upper Triangular Matrices. For an example of an upper triangular matrix, try
>> U=triu(round(2*rand(5)+1))
A Scientific Report
Each entry of an upper triangular matrix that falls below the main diagonal is zero. Repeat this experiment by replaying the following command several times.
>> U=triu(round(2*rand(5)+1)),det(U)
In the space that follows, explain how to find the determinant of an upper triangular matrix. Conjecture.
Each entry of a lower triangular matrix that lies above the main diagonal is zero. Repeat this experiment by replaying the following command several times.
>> L=tril(round(2*rand(5)+1)),det(L)
In the space that follows, explain how to find the determinant of a lower triangular matrix.
8 Conjecture.
A Scientific Report
Identity Matrices
Lets investigate the determinant of identity matrices.
>> I=eye(5) I = 1 0 0 0 0 >> det(I) ans = 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
Repeat this experiment by replaying the following command for several different values of n.
>> n=3,I=eye(n),det(I)
In the space that follows explain how to find the determinant of an n n identity matrix. Also, explain how you can use your conjecture about upper and lower triangular matrices to calculate the determinant of an n n identity matrix. Conjecture.
Permutation Matrices
A permutation is a reordering of numbers. For example, repeat the following command several times to capture random permutations of the numbers 1 through 10.
>> p=randperm(10)
A square matrix with a single 1 in every row and every column is called a permutation matrix. Permutation matrices are built by permuting the rows (or columns) of the identity matrix.
>> I=eye(5)
A Scientific Report
I = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
Note that the rows of P are a rearrangement of the rows of I. Each time you exchange a pair of rows you negate the determinant. Start with the matrix I and exchange rows 2 and 5. Then exchange rows 4 and 5 of the result and you get the matrix P. The determinant of the identity matrix I is 1 and two row exchanges negates the determinant twice. Consequently, detP = detI = 1.
>> det(P) ans = 1
several times then make a conjecture about the determinant of a permutation matrix. Conjecture.
10
0 B = 1 0 0 ans = 1 -1 1 -1 2 1 0 -2 2
A Scientific Report
Sum the matrices A and B and take the determinant of the result.
>> det(A+B) ans = 23
LU Factorization
There are a number of techniques in linear algebra designed to aid in expressing a matrix as a product of two or more matrices. One of the more important factorizations is called LU decomposition.
A Scientific Report
A = 2 0 1 >> [L,U]=lu(A) L = 1 0 1/2 U = 2 0 0 1 1 0 -1 -1 1 0 1 -1/2 0 0 1 1 1 0 -1 -1 1
11
Note that L is lower triangular (with 1s on its main diagonal) and U is upper triangular. Check to see that A = LU.
>> A,L*U A = 2 0 1 ans = 2 0 1 1 1 0 -1 -1 1 1 1 0 -1 -1 1
Note that A and LU are equal. Finally, because L and U are triangular matrices, their determinants are computed by taking the product of their diagonal elements. 1 detL = det 0 0 1 0 0 = 111 = 1
Use A = LU to Calculate detA. Hopefully, you conjectured earlier that detAB = detA detB. That is, the determinant of a product of two matrices is the product of the determinants. Once you have the LU decomposition of matrix A you can use this multiplicative property of determinants to your advantage.
12
A Scientific Report
Note again that the matrix L has 1s on its main diagonal. Check to see that PA = LU.
>> P*A,L*U ans = 2 1 0 ans = 2 1 -2 1 1 0 -2 1 1 1 0 5
A Scientific Report
0 1 5
13
Also, note that exchanging the first and second rows of the identity matrix produces the matrix P. One exchange of rows negates the determinant. Therefore, detP = ? detI = ?1.
>> det(P) ans = -1
Use PA = LU to Calculate detA. PA = LU detPA = detLU detP detA = detL detU ?1 detA = 1112221/4 detA = ?21 Of course, you should always check your work.
>> det(A) ans = -21
ans = 189033
The Matlab command flops counts the cumulative number of floating operations to date. By setting the flops to zero, executing your routine, then making a final call to flops, you can find the number of floating point operations (Each floating point operation requires a certain number of cylcles of your computers CPU. Type help flops to get a short description of how Matlab counts floating point operations) required to perform your routine. For comparison, lets count the number of floating point operations required to perform an
A Scientific Report
ans = 82075
You can also record the amount of time required for Matlab to execute an instruction (Type help tic to get a description of how these Matlab commands work). You might want to use your wristwatch to time the execution of the following commands (elapsed time will vary with different processor speeds).
>> A=10*rand(50)-5; >> tic,rref(A);toc elapsed_time = 32.1900 >> A=10*rand(50)-5; >> tic,[L,U,P]=lu(A);toc elapsed_time = 0.1100
As you can see, Matlabs lu command is quick and efficient. Consequently, Matlab uses this powerful command in a number of its routines.