Newtonbad.m -- Example of cycle

Cf. Moler: NCM Ch. 4.3 A perverse example

Condition for cycle: x(n+1) - a = -(x(n) -a)
 Holds if f'(x)/f(x) = 1/(2(x-a)), integrating:
      f(x) = sign(x-a)sqrt(abs(x-a))
close all
newt1=@(f,df,x) x-f(x)/df(x)
a=2;
f=@(x) sign(x-a).*sqrt(abs(x-a))
ezplot(f,[0 4])
grid on; hold on
df=@(x) 1/2/sqrt(abs(x-a))
disp('Choose starting point')
[x0,y0]=ginput(1)
xcurr=x0;
X=[];
N=5;
for k=1:N
   X=[X xcurr];
   xcurr=newt1(f,df,xcurr)
   disp('ENTER continues')
   text(1,1.5,'ENTER continues')
   pause()
   plot([X(end) X(end) xcurr],[0 f(X(end)) 0],'-.r')
   %       old xcurr   new xcurr
   shg
    if k <=3
      text(X(end),-0.1,num2str(k-1))
    end
end
newt1 =

  function_handle with value:

    @(f,df,x)x-f(x)/df(x)


f =

  function_handle with value:

    @(x)sign(x-a).*sqrt(abs(x-a))


df =

  function_handle with value:

    @(x)1/2/sqrt(abs(x-a))

Choose starting point

x0 =

    0.8710


y0 =

   -1.1336


xcurr =

    3.1290

ENTER continues

xcurr =

    0.8710

ENTER continues

xcurr =

    3.1290

ENTER continues

xcurr =

    0.8710

ENTER continues

xcurr =

    3.1290

ENTER continues