Skip to content

Instantly share code, notes, and snippets.

@senhorinha
Last active August 29, 2015 14:17
Show Gist options
  • Save senhorinha/db7b9c9634885b86e693 to your computer and use it in GitHub Desktop.
Save senhorinha/db7b9c9634885b86e693 to your computer and use it in GitHub Desktop.
Método de jacobi
clc
clear
format long
% Jacobi para função:
% 3*x1 - x2 - x3 = 1
% x1 + 3*x2 + x3 = 5
% 2*x1 - 2*x2 + 4*x3 = 4
function xi = jacobi(numeroPassos, criterio)
passo = 0;
xi = [0,0,0];
dif = 1;
while(passo < numeroPassos && dif > criterio)
x(1) = (1 + xi(2) + xi(3))/3;
x(2) = (5 - xi(1) - xi(3))/3;
x(3) = (4 - 2*xi(1) + 2*xi(2))/4;
dif = max(abs(x - xi));
xi = x;
passo++;
end
end
xAproximadoMenosPreciso = jacobi(30, 1e-4)
xAproximadoMaisPreciso = jacobi(60, 1e-8)
erroTruncamentoEstimado = max(abs(xAproximadoMaisPreciso - xAproximadoMenosPreciso))
% erroArredondamento = max(abs(valorAproximadoEmFloat - valorAproximadoEmDouble))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment