% 2013-02-14 18:15 UPDATE : String formating from Jenna % 2013-02-13 17:54 UPDATE : changed how the code measures error % Thanks to AARON. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Run this script to test your code problem #4 % to use backward substitution change a REF matrix % to a RREF matrix. % % Warning: this is just an example testing script. % Just because your function passes these tests DOES NOT % ENSURE that it's flawless. User beware. % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% num_tries = 400; % number of matrices to test scale = 7; % largest element size of random matrices m = 8; % number of matrix rows for n =7:9 disp('Running random trials'); t = 1; while t < num_tries pivots=unique((0:m).*(rand(1,n)>(1.0/m))); pivots=pivots(2:end); Z = zeros(m,n); for i = 1:length(pivots) Z(i,pivots(i):end) = 1; end A = floor(rand(m,n)*scale-(scale-1)/2).*Z; for i = 1:length(pivots) if 0 == A(i,pivots(i)) A(i,pivots(i)) = (floor(rand(1)*scale)+1)*((rand(1)>0.5)*2.-1); if i < m A(i+1:end,(pivots(i)+1)) = 0; end end end if rank(A) ~= length(pivots) continue end RREF_exact = rref(A); RREF = transform_ref_to_rref(A); er = max(max(abs(RREF - RREF_exact))); assert( abs(er) < 1e-10 ); t = t + 1; end disp('All trials passed!');