Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Lecture 4

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);

pivot = A(1,1); end


the_row = A(1,:)/pivot;
A(1,:) = the_row; function A = re(A)
A(2,:) = A(2,:) - A(2,1)*the_row; pi = 1;
A(3,:) = A(3,:) - A(3,1)*the_row; [m,n] = size(A);
for pj=1:n
pivot = A(2,2); pivot = A(pi,pj);
the_row = A(2,:)/pivot; the_row = A(pi,:)/pivot;
A(2,:) = the_row; A(pi,:) = the_row;
A(1,:) = A(1,:) - A(1,2)*the_row; for i = 1:m
A(3,:) = A(3,:) - A(3,2)*the_row if i == pi
continue
end These codes can only end
handle two vectors A(i,:) = A(i,:) - A(i,pj)*the_row
end
pi = pi + 1;
end
end 3
We revisit the example on Slide 3

function demo2

v1 = [2 5 7]'; v2 = [3 4 2]'; v3 = [6 1 3]';


A = [v1 v2 v3]; R = re(A);

end

Pay attention to the movement of pivot.

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

A = [0 3 6; 1e-15 4 1; -7 50 3]; R = re2(A); Use Matlab codes to


end implement the ideas
function A = re2(A) described on the
previous slide
pi = 1; [m,n] = size(A); threshold = 1e-10;
for pj=1:n
pivot = A(pi,pj);
if abs(pivot) < threshold
A = rearrange(A,pi,pj);
end
pivot = A(pi,pj); function A = rearrange(A,pi,pj)
the_row = A(pi,:)/pivot; upper = A(1:pi-1,:);
A(pi,:) = the_row; [m,n] = size(A);
for i = 1:m lower = A(pi:m,:);
if i ~= pi column = A(pi:m,pj);
A(i,:) = A(i,:) - A(i,pj)*the_row column_abs = abs(column);
end [value,index] = max(column_abs);
end memory = lower(1,:);
pi = pi + 1; lower(1,:) = lower(index,:);
end lower(index,:) = memory;
A = [upper;lower];
end
end

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

Use Matlab codes to


A = rearrange(A,pi,pj);
end

implement the ideas described


pivot = A(pi,pj);
if abs(pivot) < threshold

on the previous slide


continue
end
the_row = A(pi,:)/pivot;
A(pi,:) = the_row;
for i = 1:m
if i ~= pi
A(i,:) = A(i,:) - A(i,pj)*the_row
end
end
pi = pi + 1;
end

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

1 0 0 0.6781 0.2123 1 0 0 0 0.7241


0 1 0 0.5616 0.2466 0 1 0 0 0.6705


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

the previous slide


pi = pi + 1;
end

end

10
Use Matlab to check if Ax=b has solution

2𝑥𝑥1 + 3𝑥𝑥2 + 4𝑥𝑥3 + 5𝑥𝑥4 = 2 2 3 4 5 𝑥𝑥 2


Write it in a
6𝑥𝑥1 + 7𝑥𝑥2 + 8𝑥𝑥4 = 3 matrix form 6 7 0 8 𝑥𝑥1 3
9𝑥𝑥1 + 10𝑥𝑥3 + 11𝑥𝑥4 = 4 9 0 10 11 𝑥𝑥2 = 4
12𝑥𝑥2 + 13𝑥𝑥3 + 14𝑥𝑥4 = 5 0 12 13 14 𝑥𝑥3 5
15𝑥𝑥1 + 16𝑥𝑥4 = 6 15 0 0 16 4 6
𝐴𝐴𝐴𝐴 = 𝑏𝑏
Is 𝑏𝑏 in 𝐶𝐶(𝐴𝐴)?
At least one Yes
solution No

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

You might also like