I need to alter my Matlab Gaussian elimination program to change the form of the final matrix before back substitution [closed]

回眸只為那壹抹淺笑 提交于 2020-04-07 06:19:07

问题


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/60261467/i-need-to-alter-my-matlab-gaussian-elimination-program-to-change-the-form-of-the

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!