How to complete Gaussian elimination?

4 Ansichten (letzte 30 Tage)
재훈
재훈 am 21 Apr. 2024
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);

Antworten (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 21 Apr. 2024
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]);
2 0 1 8 -2 4 1 0 -1 -1 3 2
% 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
2 0 1 8 0 4 2 8 -1 -1 3 2 2.0000 0 1.0000 8.0000 0 4.0000 2.0000 8.0000 0 -1.0000 3.5000 6.0000 2 0 1 8 0 4 2 8 0 0 4 8
% 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: ');
FINAL Solution:
disp(x);
3 1 2

Kategorien

Mehr zu Event Functions finden Sie in Help Center und File Exchange

Produkte


Version

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by