% Secant Method in MATLAB
clearvars
clc
% se introduce ecuatia neliniara de forma f(x)=0
%f=@(x) sqrt(x^2+2*x+2)-2*sin(x)-x+0.94;
f=@(x)exp(x)-3*x*x;
% se introduc cele doua aproximatii initiale determinate grafic (analitic)
x(1)=1;
x(2)=2;
% toleranta de calcul
eps=1e-6;
% numarul maxim de iteratii
lnit=1000;



% start initializare iteratii
iteratie=0;
fprintf ( ' Nr. iteratie     x(i)                |x(i) - x(i-1)| \n');     
fprintf('   \n');
for i=3:lnit
      x(i) = x(i-1) - (f(x(i-1)))*((x(i-1) - x(i-2))/(f(x(i-1)) - f(x(i-2))));
      iteratie=iteratie+1;
      fprintf(' %3.0f        %4.16f      %4.16f \n ', iteratie,x(i),abs(x(i)-x(i-1)));
      
      if abs(x(i)-x(i-1))<eps
        radacina=x(i);
        %iteratie=iteratie
        disp(' ');
        fprintf(' Solutia ecuatiei in toleranta de calcul ceruta = %4.16f \n ', radacina);
        disp(' ');
        break
      end
end
%proba in ecuatia f(radacina)=0
fprintf(' Verificarea solutiei obtinute f(radacina)= %1.20f \n', f(radacina));