
clc 
clear all
close all
format short 


% Given function 
f=@(x) x.^3 - 2*(x.^2) + 4*x + 1;
%initial guesses and accuracy
a = 2;
b = 4;
tol = 10^-3;       % setting error tolerance correct to 4 decimal places


    
    i = 0;
    g = 1;
    disp('Iteration       a          b          x_i          f(a)          f(b)             f(x_i)');
    disp('=========    =======    =======     =======    ==========    ==========       ========');
    while(g > tol)
        i = i + 1;
        if (f(a) == f(b))
            fprintf('Function has the same value on a and b on iteration %d\n', Iter);
            disp('Denominator in Regula falsi is zero');
            root = 'Regula falsi can''t compute the root';
            beep
            disp('Go for another iteration');
            return;
        end
        c = a - ((f(a)*(b-a))/(f(b) - f(a)));
        if(f(c)*f(a) > 0)
            b = c;
            g = f(b);
            root = b;
        else
            a = c;
            g = f(a);
            root = a;
        end
        fprintf('%3d%17.4f%11.4f%11.4f%14.4f%14.4f%14.4f\n', i,a,b,c,f(a),f(b),f(c));
    end
        
    