#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(x) x*x+x-2
int main ()
{
float x,x1,x2,x0,fx1,fx2,fx0,e=0.0001,c=0,a=1.0;
clrscr();
printf("Enter x1 and x2: ");
scanf("%f %f",&x1,&x2);
//xm=(x1+x2)/2;
while(a>e)
{
//xm=(x1+x2)/2;
x=x1;
fx1=F(x);
x=x2;
fx2=F(x);
x0=x1-(fx1*(x2-x1)/(fx2-fx1));
x=x0;
fx0=F(x);
if ((fx0*fx1)<0)
{
fx2=fx0;
x2=x0;
}
else
{
fx1=fx0;
x1=x0;
}
a=fabs((x2-x1)/x2);
c++;
}
printf("\n\nItteration: %.0f times.\nAns: %f",c,x0);
getch();
return 0;
}
Related posts: