function newton_failure

% % oscillation - not C2
% f = @(x) (x>0).*(abs(x).^(1/2)) - (x<0).*((-x).^(1/2));
% fp = @(x) (x>0).*(1./(2*x.^(1/2))) - (x<0).*(-1./(2*(-x).^(1/2)));
% xroot = 0;

% divergence - not C2
f = @(x) x.^(1/3);
fp = @(x) 1./(3*x.^(2/3));
xroot = 0;

% % doesn't any sets of conditions, but converges linearly 
% f = @(x) x.^(2/3);
% fp = @(x) 2./(3*x.^(1/3));
% % fpp = @(x) -2./(9*x.^(4/3));
% xroot = 0;


x0 = 2;

% plot
x = linspace(-10,10,2500); 
plot(x,f(x),'linewidth',2); 
hold on; 
plot(x,fp(x),'-.','linewidth',2); 
% plot(x,1e-2*fpp(x),'.','linewidth',2); 
plot(x,0*x,'--','linewidth',2); 

tol = 1e-14;
maxit = 100;
k = 1;
while (abs(f(x0))>tol && (k<maxit))
    plot(real(x0),real(f(x0)),'o','markersize',16)
    legend('f(x)','f`(x)','Zero','f(x_k)')
    pause
    
    fprintf('Newton error at iterate %3d: %20.14f %10.7e\n', k, x0, f(x0));    
    x0 = x0 - f(x0)/fp(x0);
    xhist(k) = x0;       
    k = k+1;
end
plot(x0,f(x0),'o','markersize',16)
    
xhist(k) = x0;

return
