How to complete Gaussian elimination?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Sulaymon Eshkabilov
el 21 de Abr. de 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]);
% 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 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!