Escolar Documentos
Profissional Documentos
Cultura Documentos
function x = eliminacaoGauss(A, b)
Ab = [A, b];
n = size(A, 1);
for k = 1:n
[~, pivot] = max(abs(Ab(k:n, k)));
pivot = pivot + k - 1;
Ab([k, pivot], :) = Ab([pivot, k], :);
Ab(k+1:end, :) = Ab(k+1:end, :) - Ab(k+1:end, k) / Ab(k, k) * Ab(k,
:);
end
x = zeros(n, 1);
for i = n:-1:1
x(i) = (Ab(i, end) - Ab(i, i+1:n) * x(i+1:n)) / Ab(i, i);
end
end
[n, m] = size(A);
if n ~= m
error('A matriz A deve ser quadrada.');
end
if length(b) ~= n
error('O vetor b deve ter o mesmo número de linhas que A.');
end
Ab = [A, b];
for i = 1:n
[~, idx_pivo] = max(abs(Ab(i:n, i)));
idx_pivo = idx_pivo + i - 1;
for j = i+1:n
fator = Ab(j, i) / Ab(i, i);
Ab(j, i:end) = Ab(j, i:end) - fator * Ab(i, i:end);
end
end
x = zeros(n, 1);
for i = n:-1:1
x(i) = (Ab(i, end) - Ab(i, i+1:end-1) * x(i+1:end)) / Ab(i, i);
end
end
Gauss-Seidel
function [x, iter] = gaussSeidel(A, b, tol, max_iter)
[n, ~] = size(A);
x = zeros(n, 1);
iter = 0;
for i = 1:n
sigma1 = A(i, 1:i-1) * x(1:i-1);
sigma2 = A(i, i+1:end) * x_ant(i+1:end);
x(i) = (b(i) - sigma1 - sigma2) / A(i, i);
end
iter = iter + 1;
end
if iter == max_iter
warning('O método de Gauss-Seidel atingiu o número máximo de
iterações.');
end
end
Jacobi
[n, ~] = size(A);
x = zeros(n, 1);
x_ant = x;
iter = 0;
x_ant = x;
iter = iter + 1;
end
if iter == max_iter
warning('O método de Jacobi atingiu o número máximo de iterações.');
end
end
Bissecção
clear all
format short;
f = inline(fun);
for k = 1:cont
c = (a + b) / 2;
e = abs((b - a) / 2);
iter = iter + 1;
end
raiz = x;
if iter == max_iter
warning('O Método de Newton atingiu o número máximo de iterações.');
end
end
Newton-Raphson
function [raiz, historial] = newtonRaphson(f, df, x0, tolerancia, max_iter)
for i = 1:max_iter
x = x - f(x) / df(x);
historial(i) = x;
raiz = x;
figure;
plot(1:i, historial(1:i), '-o');
title('Convergência do Método de Newton-Raphson');
xlabel('Iteração');
ylabel('Aproximação da Raiz');
grid on;
end