How to complete Gaussian elimination?
1 回表示 (過去 30 日間)
古いコメントを表示
Hello everyone, I am looking for an answer using Gaussian elimination, and the values diverge at the end. How do I fix it to get an accurate answer?
Here is my code.
clc; clear all; close all;
A = [2 0 1 ; -2 4 1 ;-1 -1 3];
b = [8 0 2]';
sz = size(A,1);
disp ([A b]);
for i = 2 :1: sz
for j = 1:1:i-1
k = A(j,j)/A(i,j);
A(i,:) = k * A(i,:) - A(j,:);
b(i) = k * b(i) - b(j);
disp([A b]);
pause(3)
end
end
for i = sz-1:-1:1
for j = sz:-1:i+1
k = A(j,j)/A(i,j);
A(i,:) = k*A(i,:)-A(j,:);
b(i) = k* b(i) - b(j);
disp([A b]);
pause(3)
end
end
x = b./diag(A);
disp(x);
0 件のコメント
回答 (1 件)
Sulaymon Eshkabilov
2024 年 4 月 21 日
Here is the corrected code:
A = [2 0 1; -2 4 1; -1 -1 3];
b = [8 0 2]';
sz = size(A, 1);
disp([A b]);
% Forward elimination
for i = 1:sz-1
for j = i+1:sz
k = A(j, i) / A(i, i);
A(j, :) = A(j, :) - k * A(i, :);
b(j) = b(j) - k * b(i);
disp([A b]);
pause(3);
end
end
% Back Substitution:
x = zeros(sz, 1);
for i = sz:-1:1
x(i) = (b(i) - A(i, i+1:end) * x(i+1:end)) / A(i, i);
end
disp('FINAL Solution: ');
disp(x);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!