Secant iteration method

by

 

secant iteration alorithm to find a function's root

Secant_Iteration.m
%///Secant iteration formula////
disp(' Enter the function(Sampling Function(x^2-4*x+4))') 
fx=input('f(x)= ','s');
f=inline(fx);
disp(   ' ----   Function   ---'   )
fx
%maximum value of relative difference
tolerance=input('tolerance=  ');
x0=input('1st initial value of x= ');
x1=input('2nd initial value of x= ');
x(1)=x0;
x(2)=x1;
 format long e 
 disp(' Iterasyon No:               x               Relative difference')
 for i=1:inf;
         x(i+2)=(x(i)*f(x(i+1))-x(i+1)*f(x(i)))/(f(x(i+1))-f(x(i)));
       a(i)=abs((x(i+1)-x(i))/(x(i)));
        disp('----------------------------------------------------')      
       fprintf('%d    %30.16e     %25.16e\n',i,x(i),a(i))
       
       if abs(a(i))<tolerance%Iteration stops 
           break
       end
 end
 %writing to txt file
       fid =fopen('Secant_Iteration.txt','w');
       
fprintf(fid,'\nIterasyon no:                 x:                    Relative difference:\n');            
      for j=1:i; 
          
fprintf(fid,'\n%4d             %3.17e       %3.17e\n',j,x(j),a(j));
      end 
      %plotting
 ezplot(f,-90,90)
grid on
ylabel('f(x)')
text(x(i),0,'----->f(x)=0 point')
 

Contact us