function p4
  newton(0, @f, @fprime, 10, 1./3);
end
function y = f(x)
  y = 27*x^4+162*x^3-180*x^2+62*x-7;
end
function y = fprime(x)
  y = 108*x^3+486*x^2-360*x+62;
end

function newton(x, f, fprime, itcount, true)
  disp(sprintf('Iteration:%d value:%.10g error:%.10e', 0, x, abs(x-true)))
  for i=1:itcount
    x = x - f(x)/fprime(x);
    disp(sprintf('Iteration:%d value:%.10g error:%.10e', i, x, abs(x-true)))
    errors(i) = abs(x-true);
  end

  order_of_convergence(errors)
end

function order_of_convergence(seq)
  for at=1:length(seq)-1
    order = log(seq(at+1)) / log(seq(at+0));
    disp(sprintf('Apparent order of convergence at iteration %d:%g', at, order))
  end
end
