// questao4.sce

// metodo de ponto fixo generico
// com estudo de erros

function [raiz, iter] = mpfe (phi, x0, xi, eps)
  iter = 0;
  x = x0;
  ek = abs(x0 - xi);
  mprintf ("it | xk | ek | ek1 | ek1 / (ek)^2\n");
  while abs(x - phi(x)) >= eps do
    x = phi(x);
    iter = iter + 1;
    ek1 = abs(x - xi);
    mprintf ("%d %f ", iter, x);
    mprintf ("%f %f %f\n", ek, ek1, (ek1 / (ek)^2));
    ek = ek1;
  end
  raiz = x;
endfunction

// definindo a funcao de iteracao do Metodo de Newton
// para f(x) = (x - 2)^2 * x

function y = phi(x)
  f = (x - 2)^2 * x;
  f1 = 2 * x * (x - 2) + (x - 2)^2;
  y = x - f / f1;
endfunction

// executando o metodo

[raiz, iter] = mpfe(phi, 1.5, 2, 0.0000001)

// RESPOSTAS OBTIDAS:
// raiz = 1.9999998
// iter = 21
