问题
I have made this Matlab function that performs Gaussian elimination however, I need to alter my code so that the final matrix (C) before back substitution is in the form of the linked picture named 'Required form of matrix'(where it says A = ).
Right now my code performs Gaussian elimination however the matrix is in the form of the other picture (with u's).
I have tried to change the two for loops in the beginning by saying:
for i = n-1:1
for k = n:i+1
However, this has not worked.
Please can you help me
My code:
function x = gaussElimination(A, b)
A = [3 1 -1; 1 -4 2; -2 -1 5]; % Inputting the value of coefficient matrix
b = [3; -1; 2]; % Inputting the value of coefficient matrix
[n, n] = size(A); % Find size of matrix A
[n, k] = size(b); % Find size of matrix b
x = zeros(n,k); % Initialize x
for i = 1:n-1
for k = i+1:n
m = -A(k,i)/A(i,i); % multiplier
A(k,:) = A(k,:) + m*A(i,:);
b(k,:) = b(k,:) + m*b(i,:);
end
end
C = [A b] % Final form of matrix before back substitution
% Use back substitution to find unknowns
x(n,:) = b(n,:)/A(n,n);
for i = n-1:-1:1
x(i,:) = (b(i,:) - A(i,i+1:n)*x(i+1:n,:))/A(i,i);
end
end
来源:https://stackoverflow.com/questions/60270242/i-need-to-alter-this-matlab-gaussian-elimination-program-to-change-the-form-of-t