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

    scs=socscx(saiso)+6;
    ss=diff(kxdn)/2;
    buoc = 1;
    
    kxdn = sort(kxdn)
    xm = kxdn(1);
    fkxdn = feval(hamso,kxdn)
    %fkxdn(2)  = 45
    if prod(fkxdn)>0
        disp(['Khoang ('  num2str([kxdn]) ') khong phai la khoang xac dinh nghiem']);
    else
    % in tieu de bang tinh toan 
    plusstring(1:scs*7+7)='-';
    disp(plusstring);
    disp(['|buoc|' printstr(' a ',scs) '|' printstr(' x ',scs) '|' printstr(' b ',scs) '|' printstr(' f(a) ',scs) '|' printstr(' f(x) ',scs) '|' printstr(' f(b) ',scs) '|' printstr(' ss ',6) '|']);
    disp(plusstring);
    scs=scs-1;
    while abs(ss)>saiso

        [ kxdnm, xm, ss, fkxdnm,fx] = daycung1b (hamso,kxdn,fkxdn,xm);
        inra = print1line(scs,buoc,kxdn(1), xm,kxdn(2),fkxdn(1),fx,fkxdn(2));
        disp([inra  insaiso(abs(ss),7) '|']);
      
        if haveplot==1
            plotdaycung(hamso,kxdn,fkxdn,xm,fx);       
        end;
        
        buoc=buoc+1;
        if buoc > 100 
            disp('ham khong hoi tu sau 100 buoc');
            break;
        end;
        kxdn=kxdnm;
        fkxdn=fkxdnm;
    end;
    
    % 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 '], xm,char(177));
    disp ([inra ' ' insaiso(abs(ss),6) ]);
    if haveplot==1
        plotdaycung(hamso,kxdnm,fkxdnm,xm,fx);
        xlabel('x','fontsize',32);
        ylabel(str_ham(hamso),'fontsize',32);
        title('Giải hàm 1 ẩn bằng phương pháp dây cung','fontsize',32);
    end;
        
end;
        
function plotdaycung(hamso,kxdn,fkxdn,xn,fxn);
    %
    khoangve=kxdn;
    
    if diff(khoangve)<0
        khoangve=fliplr(khoangve);
    end;
    khoangve = khoangve + diff(khoangve)*0.1*[-1 1];
    
    plotfunc(hamso,khoangve);
    hold on;
    plot(kxdn,fkxdn,'r--');
    plot([xn xn],[0,fxn],'k-.');
    plot(xlim,[0 0],'k-');

function [kxdn, xm, ss, fkxdn,fx ] = daycung1b (hamso,kxdn,fkxdn,x)
    xm  = kxdn(1)-fkxdn(1)*diff(kxdn)/diff(fkxdn);
    ss  = xm -x ;
    fx  = feval(hamso,xm);
    if fx*fkxdn(1)<0
        kxdn(2) = xm;
        fkxdn(2) = fx;
    else
        kxdn(1) = xm;
        fkxdn(1) = fx;
    end;
function y=diff(x)
    y=x(2:end)-x(1:end-1);