Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. (This is a simplified version of Exercise 6 on page 162 of the book) Write a

ID: 3196097 • Letter: 1

Question

1. (This is a simplified version of Exercise 6 on page 162 of the book) Write a MATLAB function that receives a square matrix A as input, reduces A to row echelon form and returns the new matrix A matrix is in row echelon form if 1. all nonzero rows (rows with at least one nonzero element) are above any rows of all zeroes (all zero rows, if any, belong at the bottom of the matrix), and the leading coefficient (the first nonzero number from the left, also called the pivot) of a nonzero row is always strictly to the right of the leading coefficient of the row above it. 2.

Explanation / Answer

% matlab code for row echelon form of a matrix
% a is nxn matrix
% Example
% >> a=[1 2 3;4 5 6; 7 8 7]
% >> ref(a)
% result show a row-echelon-form
function m = ref(a)
if length(a(1,:)) ~= length(a(:,1))
error('Matrix is not square matrix or wrong number of input arguments')
end
n=length(a(1,:));
j=1;
for i = 1:n-1
if (i==j)
if (a(i,j)==0)
c = a(i,:); a(i,:)= a(i+1,:); a(i+1,:) = c;
end
a(i,:)= (1/a(i,j))* a(i,:);
for k=i+1:n
a(k,:)=(-a(k,j)* a(i,:)) + a(k,:);
end
end
j=j+1;   
end
a(n,:)= (1/a(n,n))* a(n,:);
m=a;
end