Ok, I have your solution.
1. Your subtract function in the beginning did not return the subtraced matrix.
2. You needed to return the previously calculated matrix (as I commented in the first response). However, since the returned matrix is smaller than the current matrix, you have to account for that.
3. The whole pivot point vector is not needed. The current pivot point is calcuated in the beginning of each cycle, meaning that when later on you need to subtract out the other values of the current row, you just use the current row.
Now, in the solution, I assumed that you wanted the generalized format for solving systems of non-zero equations.
I.e. The format of the input M is assumed to be
[matrix A] = [Column solution B] - such that the last column of the input matrix is the vector B.
I'm using an older verion of matlab, so I commented out some of your debugging to make it easier on myself.
if isempty(M)
disp('found empty');
return;
end
[rows,cols] = size(M);
tolerance = length(M)*norm(M)*eps;
[x,p] = max(abs(M(:,1)));
if x>tolerance
M = Exchange(M,1,p);
M = Divide(M,1,M(1,1));
for i=2:rows
if M(i,1)==0
else
M = Subtract(M,1,i,M(i,1)/M(1,1));
end
end
disp('Recurring now.');
[new_sub_matrix] = echelon(M(2:rows,2:cols));
disp('Getting out of the recursion, M is...');
if(~isempty(new_sub_matrix))
M(2:rows,2:cols) = new_sub_matrix;
end
disp(M);
for z=2:rows
subtract_factor = M(1,z);
M = Subtract(M,z,1,subtract_factor);
end
fprintf('Check on m\n');
disp(M);
end
disp('At the end of the function, M is...');
disp(M);
Best Answer