function newton_tb (hamso, xbd, saiso, figure)
    %
    if (nargin >3) 
        haveplot=1;
        clf;
        naxis=[];
    else
        haveplot=0;
        naxis=[];
       
    end;

    tol = 1e-5;
    scs=socscx(saiso)+3;
    minscs=length(' f(x0)/f''(x0) ');
    scs=max(scs,minscs);

    xtd  = xbd+tol;
    fxtd = feval(hamso,xtd);
    [ fxbd, dhtb, ss, xtt] = newton1b (hamso,xbd,fxtd,xtd,tol);
    daohamdoidau=(1>0);
    
    if daohamdoidau
        % in tieu de bang tinh toan 
        plusstring(1:scs*5+7)='-';
        disp(plusstring);
        disp(['| buoc |' printstr(' x0 ',scs) '|' printstr(' f(x0) ',scs) '|' printstr(' f''(x0) ',scs) '|' printstr(' f(x0)/f''(x0) ',scs) '|' printstr(' sai so ',8) '|']);
        disp(plusstring);
        % in hang dau tien cua bang
        inramh = print1line(scs-1,1,xbd, fxbd,dhtb,-ss);
        disp([inramh  insaiso(abs(ss),7) ' |']);
            
    %        
      %keyboard
        buoc = 2;
        xtd=xbd;
        xbd=xtt;
        fxtd=fxbd;
        dhtbtd=dhtb;

    else
        disp (' Ham khong thoa man dk de giai bang phuong phap lap');
    end;
            
           
    
    while (daohamdoidau) & (abs(ss)>saiso)
        tol=ss/1e5;
        [ fxbd, dhtb, ss, xtt] = newton1b (hamso,xbd,fxtd,xtd,tol);
        
        inramh = print1line(scs-1,buoc,xbd, fxbd,dhtb,-ss);
        disp([inramh  insaiso(abs(ss),7) ' |']);
      
        if haveplot==1
            naxis = plotnewton(hamso,xtd,xbd,fxtd,fxbd);
          
        end;
        
        daohamdoidau = (dhtbtd*dhtb>0);
        if ~daohamdoidau
            disp('LOI: Ham khong don dieu');
            break;
        end;
        buoc=buoc+1;
        if buoc > 100 
            disp('ham khong hoi tu sau 100 buoc');
            break;
		end;
        xtd=xbd;
        xbd=xtt;
        dhtbtd=dhtb;
        fxtd=fxbd;
    end; 
    
    if daohamdoidau
        % in vien bang 
        disp(plusstring);
        disp(' ');
        % in ket qua
        scs=socscx(ss);
        nformat=['%' sprintf('%1u',scs+5) '.' sprintf('%1u',scs)];
        inramh=sprintf([' Nghiem la: ' nformat 'f %1s '], xtt,char(177));
        disp ([inramh ' ' insaiso(abs(ss),6) ]);
        if haveplot==1
            naxis = plotnewton(hamso,xtd,xbd,fxtd,fxbd);
            xlabel('x','fontsize',32);
            ylabel(str_ham(hamso),'fontsize',32);
            title('Giải hàm 1 ẩn bằng phương pháp tiếp tuyến','fontsize',32);
        end;
    end;
        
    
        
function naxis = plotnewton(hamso,x0,x1,fx0,fx1);
    %
    khoangve=[x0 x1];
    
    if diff(khoangve)<0
        khoangve=fliplr(khoangve);
    end;
    khoangve = khoangve + diff(khoangve)*0.2*[-1 1];
    
    plotfunc(hamso,khoangve);
    hold on;
    plot([x0 x1],[fx0 0],'r--','LineWidth',3);
    plot([x0 x0],[fx0 0],'k-.','LineWidth',3);
    naxis=axis;
    plot(xlim,[0 0],'k-','LineWidth',3);

function [ fx0, dhtb, ss, xtt] = newton1b (hamso,x0,fxtd,xtd,tol)
    fx0  = feval(hamso,x0);
    xdh  = x0 - (x0-xtd)/10;
    fxdh  = feval(hamso,xdh);
    if abs(x0-xtd)<=tol
        dhtb = (fx0 - fxtd)/(x0-xtd);
    else
        %if abs(fx0) > abs(fxdh)
         %   xdh = x0 + 
        fxdh  = feval(hamso,x0-tol);
        dhtb = (fx0 - fxdh)/tol;
    end;
    ss   = - fx0 / dhtb;
    xtt  = x0 + ss; 
function y=diff(x)
    y=x(2:end)-x(1:end-1);