Professional Documents
Culture Documents
Lecture 4 Matlab Basics II
Lecture 4 Matlab Basics II
Matlab Basics II
Kang ZHOU
1
Use iterations to develop generally applicable algorithms
2 3 6 1 1.5 3 1 1.5 3 1 0 −3 1 0 0
5 4 1 → 5 4 1 → 0 −3.5 −14 → 0 1 4 → 0 1 0
7 2 3 7 2 3 0 −8.5 −18 0 0 16 0 0 1
How to check if three vectors are independent?
2
We use a simpler example first
function demo2 function demo2 2 3
v1 = [2 5 7]'; v1 = [2 5 7]'; They can handle any 5 4
v2 = [3 4 2]'; v2 = [3 4 2]';
number of vectors 7 2
A = [v1 v2];
A = [v1 v2]; R = re(A);
function demo2
end
4
Make the function more robust - consider special cases
0 3 6 If pivot is zero, we need to exchange the row of pivot with a row below
10−15 4 1 it.
−7 50 3
↓ How to test if pivot is zero? We do not use pivot == 0 because pivot is
−7 50 3 often not zero due to truncation error of numerical computation even
10−15 4 1 when it is supposed to be zero. It may be 10-15. So we usually test if
0 3 6 absolute value of pivot is smaller than a threshold. If so, pivot is
↓ considered to be zero.
1 −7.1429 −0.4286
0 4 1
When pivot is zero, which row below the row of pivot should be selected
0 3 6
for exchange? The basic requirement is that the pivot after the exchange
should not be zero. Matlab’s algorithm is selecting the row which can
maximize the absolute value of pivot after the exchange.
5
function demo3
6
Make the function more robust - consider special cases
1 3 6
Pay attention to the movement of pivot
0 0 1
0 0 3
↓
1 3 6 When pivot is zero, the row above the pivot cannot be exchanged with the row of
0 0 1 pivot, because the exchange would change the columns on the left of the column
0 0 3 of pivot and would result in matrix that does not satisfy requirements of RREF.
↓
1 3 6 When pivot is zero and none of the rows below it has non-zero element in the
0 0 1 column of pivot, the pivot position will be moved to the next column but not to
0 0 3 the next row.
↓
1 3 0
0 0 1
0 0 0
7
function demo4
A = [1 3 6; 0 0 1; 0 0 3]; R = re3(A);
end
function A = re3(A)
pi = 1; [m,n] = size(A); threshold = 1e-10;
for pj=1:n
pivot = A(pi,pj);
if abs(pivot) < threshold
end
8
The last special case we need to consider
2 3 4 5 2 1 1.5 2 2.5 1 1 0 −7 −2.75 −1.25
6 7 0 8 3 0 −2 −12 −7 −3 0 1 6 3.5 1.5
↓
9 0 10 11 4 0 −13.5 −8 −11 −5 0 0 73 35.75 15.25
0 12 13 14 5 0 12 13 14 5 0 0 −59 −28 −13
↓
0 0 1 0.4897 0.2089 0 0 1 0 0.5785
0 0 0 0.8938 −0.6747 0 0 0 1 −0.7548
When all the rows are processed, RREF is obtained and the algorithm should be terminated.
9
function demo5 function A = re4(A)
pi = 1; [m,n] = size(A); threshold = 1e-10;
A = [ 2 3 4 5 for pj=1:n
6 7 0 8 pivot = A(pi,pj);
9 0 10 11 if abs(pivot) < threshold
0 12 13 14]; A = rearrange(A,pi,pj);
end
b = [2 3 4 5]'; pivot = A(pi,pj);
if abs(pivot) < threshold
Ab = [A b];
end
continue
rref is a built-in
R = re4(Ab) the_row = A(pi,:)/pivot; function that
A(pi,:) = the_row; converts a matrix
end for i = 1:m
if i ~= pi into its RREF
Use Matlab codes
A(i,:) = A(i,:) - A(i,pj)*the_row
end
to implement the
end
if pi == m
ideas described on
break
end
end
10
Use Matlab to check if Ax=b has solution
No solution
11
Use Matlab to check if Ax=b has solution
function demo5 >> demo5
A = [ 2 3 4 5 R =
2 3 4 5 𝑥𝑥 2
6 7 0 8 6 7 0 8 𝑥𝑥1 3
1 0 0 0 0
9 0 10 11
0 12 13 14 0 1 0 0 0 9 0 10 11 𝑥𝑥2 = 4
15 0 0 16]; 0
0
0
0
1
0
0
1
0
0
0 12 13 14 𝑥𝑥3 5
b = [2 3 4 5 6]'; 0 0 0 0 1 15 0 0 16 4 6
Ab = [A b]; >> Write it in a form that
is easier to understand
R = re4(Ab)
R is reduced row echelon 1 0 0 0 𝑥𝑥1 0
end form of Ab. 0 1 0 0 0
𝑥𝑥2
There is inconsistency, so
0 0 1 0 = 0
𝑥𝑥3
there is no solution. 0 0 0 1 𝑥𝑥4 0
0 0 0 0 1
12
If there is a solution, use Matlab to find it
function demo5
2 3 4 5 𝑥𝑥1 2
A = [ 2 3 4 5
6 7 0 8 6 7 0 8 𝑥𝑥2 3
=
9 0 10 11
0 12 13 14];
9 0 10 11 𝑥𝑥3 4
0 12 13 14 𝑥𝑥4 5
b = [2 3 4 5]';
1 0 0 0 𝑥𝑥1 0.7241
Ab = [A b];
0 1 0 0 𝑥𝑥2 0.6705
R = re4(Ab) =
0 0 1 0 𝑥𝑥3 0.5785
end
0 0 0 1 𝑥𝑥4 −0.7548
>> demo5 𝑥𝑥1 0.7241
R = 𝑥𝑥2 0.6705
=
1.0000 0 0 0 0.7241 𝑥𝑥3 0.5785
0
0
1.0000
0
0
1.0000
0
0
0.6705
0.5785
𝑥𝑥4 −0.7548
0 0 0 1.0000 -0.7548
>> 13
What about this case?
𝑥𝑥1
2 3 4 5 𝑥𝑥 2
6 7 0 8 𝑥𝑥2 = 3
9 0 10 11 𝑥𝑥3 4 How many solutions are
4 there? We will discuss it in
𝑥𝑥1 the next lecture.
1 0 0 0 𝑥𝑥 0.7241
0 1 0 0 𝑥𝑥2 = 0.6705
0 0 1 0 𝑥𝑥3 0.5785
4
14