필터 지우기
필터 지우기

How to complete Gaussian elimination?

조회 수: 1 (최근 30일)
재훈
재훈 2024년 4월 21일
답변: Sulaymon Eshkabilov 2024년 4월 21일
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);

답변 (1개)

Sulaymon Eshkabilov
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]);
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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by