#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#define EPS 1E-5 /* ending value of iteration  */

// Gobal variable

float f(float x);

int main()

{

FILE * logfile; 

int i;

float y,initial;

// open file

logfile= fopen("output.txt","w");

    if(logfile==NULL)

       {

       printf("ERROR: log file  could not be opened.\n");

       system("PAUSE");

       exit(0);

       }

fprintf(logfile,"\n Find the root of  f(x)=x^2+x-1=0  \n");

/*Direct-iterative Method */

printf("Enter the initial approx. x_0= \n");

scanf("%f",&initial);

y=f(initial);

i=0;

fprintf(logfile,"\n ## Table : Direct Iterative Method \n");

fprintf(logfile,"\n ## We consider  the intial approx. x_0= %f \n",initial);

fprintf(logfile,"\n|------------------|\n");

fprintf(logfile,"|n \t |    x_n+1|\n");

fprintf(logfile,"|------------------|\n");

for(i=0;fabs(y-initial) > EPS;i++)

{

printf("%d \t %f\n",i,y);

fprintf(logfile,"|%d \t | %f|\n",i,y);

  initial=y;

  y=f(initial);

                         

       }

fprintf(logfile,"|------------------|\n");       

   

fclose(logfile);

return 0;

}

 

float f( float x)

{

float a;

//f(x)=0: x^2+x-1=0, x=phi(x)=1/(1+x)

a= 1/(1+x);

printf("%f\n",a);

return a;

 

     }