function xNew = FalsePos(f, a, b, tol)
% FalsePos(f, a, b, tol) calculates root of equation f(x)=0 bracketed by
% [a,b] using False Position method, with tolerance tol

fprintf('\n *** False Position method solver *** \n\n');

if (f(a)*f(b) > 0)
   fprintf('Interval [%g,%g] does not bracket the equation root \n', a, b);
end

horzLine = '_________________________________________________';
fprintf(' n |        x(n)        |x(n)-x(n-1)|  f(x(n)) \n%s\n', horzLine);
xOld = a;

for n = 1:100
    xNew = a - f(a) * (b - a) / (f(b) - f(a));
 
    if ( f(xNew)*f(a) > 0 ) a = xNew;
    else b = xNew;
    end;
    
    fprintf('%2d | %18.12g | %9.2e | %9.2e \n', n, xNew, abs(xNew-xOld), f(xNew));  
    if  abs(xNew - xOld) < tol
        return
    end
    xOld = xNew;
end
fprintf('*** False position method failed to converge after 100 iterations');