function pplap_tb (hamso, x0, saiso, figure)
    %
    if (nargin >3) 
        haveplot=1;
        naxis=[];
    else
        haveplot=0;
        naxis=[];
       
    end;
    tol=1e-7;
    
    scs=socscx(saiso);
    nformat=['%' sprintf('%1u',scs+5) '.' sprintf('%1u',scs+2)];
    [ xtt, ss, dg] = lap1b (hamso,x0,tol);
    
    if dg
        % in tieu de bang tinh toan 
        plusstring(1:(scs+5)*5+17)='-';
        disp(plusstring);
        disp(['| buoc | ' printstr('x0',scs+5) ' | ' printstr('x0',scs+5) ' | ' printstr('g(x0)',scs+5) ' | ' printstr('sai so',scs+5) ' |']);
        disp(plusstring);
        % in hang dau tien cua bang
        inra = print1line(scs+5,1,x0, xtt,abs(ss));
        disp(inra);
disp "ok"
        %ve ham so ----------------- can viet lai
        if haveplot==1
            cla;
            naxis = plotpplap(hamso,x0,xtt);
            if ~ exist('oldaxis')
                oldaxis = naxis;
            else
                oldaxis(1)=min(oldaxis(1),naxis(1));
                oldaxis(2)=max(oldaxis(2),naxis(2));
                oldaxis(3)=min(oldaxis(3),naxis(3));
                oldaxis(4)=max(oldaxis(4),naxis(4));
            end;
        end;
        %keyboard
        buoc = 2;
        x0=xtt;
    else
        disp (' Ham khong thoa man dk de giai bang phuong phap lap');
    end;
            
           
    
    while (abs(ss)>saiso)
        % in bang tinh
        inra = print1line(scs+5,buoc,x0,xtt,ss);
        disp(inra);    
        % ve ham so     
        if haveplot==1
            plotpplap(hamso,xtt,x0);
        end;

        % in bang tinh
        [ xtt, ss, dg ] = lap1b (hamso,x0,tol);
        inra = print1line(scs+5,buoc,x0,xtt,abs(ss));
        disp (inra);
      
        
        if dg
            disp('LOI: Ham khong thoa man dk dao ham nho hon 1');
            break;
        end;
        buoc=buoc+1;
        if buoc > 50 
            disp('ham khong hoi tu sau 50 buoc');
            break;
        end;
        x0=xtt;
    end;
    
    if dg
        disp(inra);        
        % in vien bang 
        disp(plusstring);
        disp(' ');
        % in ket qua
        scs=socscx(ss);
        nformat=['%' sprintf('%1u',scs+5) '.' sprintf('%1u',scs)];
        inra=sprintf([' Nghiem la: ' nformat 'f %1s ' nformat 'f'], xtt,char(177), ss);
        disp (inra);
        if haveplot==1
            plotpplap(hamso,x0,xtt);
<<<<<<< HEAD
=======
            xlabel('x','fontsize',32);
            ylabel(str_ham(hamso),'fontsize',32);
            title('Giải hàm 1 ẩn bằng phương pháp lặp','fontsize',32);
>>>>>>> 80f1f15b8f9b51ea852b90b0d34bef797d8a7da5
        end;
        %axis(oldaxis);
    end;
        
    
        
function naxis = plotpplap(hamso,x0,x1);
    %
    khoangve=[x0 x1];
    
    if diff(khoangve)<0
        khoangve=fliplr(khoangve);
    end;
    khoangve = khoangve + diff(khoangve)*[-1 1];
    
    plotfunc(hamso,khoangve);
    hold on;
    plot(khoangve,khoangve,'b-');
    x2 = feval(hamso,x1);
    max01=max(x0,x1);
    plot([x0 x0],[0 max(x0,x1)],'k--');
    plot([x1 x1],[0 x1],'k--');
    plot([0 x1],max(x1,x2)*[1 1],'k--');

    naxis = [khoangve min(x0,x1,x2) max(x0,x1,x2)];

function [ xtt, ss, dg] = lap1b (hamso,x0,tol)
    xtt = feval(hamso,x0);
    ss =  abs(xtt-x0);
    dg = abs((xtt-feval(hamso,x0+tol))/tol)<1 ;
    
<<<<<<< HEAD
function y=diff(x)
    y=x(2:end)-x(1:end-1);
=======
%function y=diff(x)
%    y=x(2:end)-x(1:end-1);
>>>>>>> 80f1f15b8f9b51ea852b90b0d34bef797d8a7da5
