% >> m1019Lab7. prompts for input lines not shown
% https://sites.google.com/site/keadyperthunis/home/teaching-1/m1019mtl
% https://sites.google.com/site/keadyperthunis/home/teaching-1/lab7solutions
% https://au.mathworks.com/products/matlab-online.html
% MATH1019 Linear Algebra and Statistics for Engineers
% Laboratory Session 7
% Solutions
% From row operations to rank and solving linear systems
% Exercises
AI =eye(3)
AI =
1 0 0
0 1 0
0 0 1
AI([1 3],:) = AI([3 1],:) % swaps row1 and row3
AI =
0 0 1
0 1 0
1 0 0
P=AI
% The matrix P is called a permutation matrix.
P =
0 0 1
0 1 0
1 0 0
% Show the P^2 is the identity matrix.
PSquared = P*P
PSquared =
1 0 0
0 1 0
0 0 1
M = [1,2,3,4;5,6,7,8;9,10,11,12]
M =
1 2 3 4
5 6 7 8
9 10 11 12
% Let's now do some other row operations, performing them on the matrix M.
M0=M; % save a copy of M as the next commands modify M, and will use M0 in a later exercise
M(2,:) = M(2,:)-5*M(1,:) % row2 <- row2-5*row1
M =
1 2 3 4
0 -4 -8 -12
9 10 11 12
M(3,:) = M(3,:)-9*M(1,:) % row3 <- row3 -9*row1
M =
1 2 3 4
0 -4 -8 -12
0 -8 -16 -24
M(3,:) = M(3,:)-2*M(2,:) % row3 <- row3-2row2
M =
1 2 3 4
0 -4 -8 -12
0 0 0 0
% The rank of a matrix is the number of nonzero rows in the matrix when it’s in row echelon form.
rank(M)
ans = 2
% 2 This agrees with what you have found with your row operations.
% 2.
% Define the matrices
M1= [1,2,3,4;5,6,7,8;9,10,11,11]
M2= [1,2,3,4;5,6,7,8;9,10,12,12]
M1 =
1 2 3 4
5 6 7 8
9 10 11 11
M2 =
1 2 3 4
5 6 7 8
9 10 12 12
% What are their ranks?
rank(M1)
ans =3
rank(M2)
ans = 3
% 3.
% The system of linear equations Ax=b can be solved by reducing the augmented matrix A|b to row echelon form.
% Consider next systems defined as follows
% 3.
% The system of linear equations Ax=b can be solved by reducing the augmented matrix A|b to row echelon form.
% Consider next systems defined as follows
A0=M0(:,[1:3]); b0=M0(:,4)
b0 =
4
8
12
A1=M1(:,[1:3]); b1=M1(:,4)
b1 =
4
8
11
A2=M2(:,[1:3]); b2=M2(:,4)
b2 =
4
8
12
% Use the rank command to decide which of the systems have solutions.
rank(M0)
rank(A1)
% ans = 2
rank(A2)
% ans = 3
% A2 is full rank
% Solving using Gaussian Elimination and row operations
M2(2,:) = M2(2,:)-5*M2(1,:)
M2 =
1 2 3 4
0 -4 -8 -12
9 10 12 12
M2(3,:) = M2(3,:) - 9*M2(1,:)
M2 =
1 2 3 4
0 -4 -8 -12
0 -8 -15 -24
M2(3,:) = M2(3,:) - 2*M2(2,:)
M2 =
1 2 3 4
0 -4 -8 -12
0 0 1 0
% By hand backward substitution could then be used to solve for x
% Solving using the command x=A\b
x2= A2\b2
x2 =
-2.0000
3.0000
-0.0000
% 4.
% Start with Gaussian elimination
A=[8,3;4,6]; A0=A; b=[-2;8]; A(2,:)= A(2,:)-(1/2)*A(1,:)
A =
8.0000 3.0000
0 4.5000
% This gives a row echelon form (an upper triangular matrix).
% Gaussian elimination in matlab is lu factorization:
[L,U,P] = lu(A0)
L =
1.0000 0
0.5000 1.0000
U =
8.0000 3.0000
0 4.5000
P =
1 0
0 1
% Look at the upper triangular factor, U it’s the same as you found with the row operation above.
% If one solves Ly=b and then Ux=y the x so found solves Ax=b
y=L\b
y =
-2
9
x=U\y
x =
-1
2
>>