function false_position_algorithm_ploting
clc
clear all
close all

%% Parameters
f=@(x) x^3+2*x^2-3*x-1;  %function
a=1;   %left bound
b=2;   %right bound
ee=1e-10;   % error tolerance
Nmax=10; %maximum iteratoins

%% Gerearte Points for Ploting
h=figure;
xx=linspace(1,2,100);
yy=zeros(1,100);
for t=1:100
    yy(1,t)=feval(f,xx(1,t));
end

%% Algorithm
k=0;  % Start Iteration counter 
pp=1.2; ppp=12; X=13;
sfa=sign(feval(f,a));
fprintf('Starting Calculatins \n ----------------------\n k\t   a\t\t\t  b\n')
fprintf('%3.f \t %1.10f\t %1.10f \n',k,a,b)
while X>ee
    bb=feval(f,b);
    aa=feval(f,a);
    p=b-bb*(b-a)/(bb-aa);
    plott(a,b,p,k,aa,bb,xx,yy,h)
    sfp=sign(feval(f,p));
    if (sfa*sfp)<0
          b=p;
    else
         a=p;
         sfa=sfp;
    end
     pppp=ppp; %pppp=p(n-2)
    ppp=pp;  %ppp=p(n-1)
    pp=p;   %pp=p(n)
    hh=(pp-ppp)/(ppp-pppp);  %lambda
    X=abs(hh/(hh-1))*abs(pp-ppp);  % error calculation for stopping
        if k>Nmax   %stopping criteria for max iterations
             fprintf('\n\n\n!!!!Max Iterations Reached!!!!!!!\n') 
               break
        end
        k=k+1;
  fprintf('%3.f \t %1.10f\t %1.10f \n',k,a,b)
end
 if k<Nmax
             fprintf('\nRoot=%20.10f\n',p)
 end
fprintf('Root=%6d\n',p)
end

%% Plotting each Iteration
function plott(a,b,p,k,aa,bb,xx,yy,h)
clf
plot(xx,yy,'k-')
hold on
plot([a,b],[aa,bb],'o--b')
plot([p],[0],'or')
plot([0,100],[0,0],'--','Color',[.5,0.5,0.5])
hold off
axis([1,2,-2,9]);
title('\fontsize{17}False Position Algorithm {\fontsize{9}Dylan Walsh}')
xlabel('X','FontSize',14)
ylabel('Y','FontSize',14)
annotation(h,'textbox',...
    [0.650 0.18 0.2 0.2],...
    'String',{'Iteration' k},...
    'FontWeight','bold',...
    'FontSize',16,...
    'FontName','Times New Roman',...
    'FitBoxToText','off',...
    'EdgeColor',[1 1 1]);
pause(1)
end