Nov 8, 2008

Method of False Position.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

float f(float a)
{

float b
b=a*a-25;
return b;
}

float abso(float c)
{
if (c<0) return -c;
else return c;
}

int main()
{
float x0,x1,x2,f0,f1,f2,e,m;
int i,n,a=0;

printf("Enter the value of x0,x1,e and n\n");
scanf("%f%f%f%d",&x0,&x1,&e,&n);
f0=f(x0);
f1=f(x1);
printf("Iteration x0,x1,x2,f0,f1,f2\n");
for(i=0;i<=n;i++)
{
x2=((x0*f1-x1*f0)/(f1-f0));
f2=f(x2);
printf("%d %f %f %f %f %f %f\n",i,x0,x1,x2,f0,f1,f2);
if(abso(f2)<=e)
{
a=1;
printf("Convergent in %dth iteration at root %f\n", i,x2);
if (a==1)
{
exit(1);
}
}
if((f2*f0)<0)
{
x1=x2;
f1=f2;
}
else
{
x0=x2;
f0=f2;
}
}
printf("Does not converge in %d itetations.\n",i-1);
printf("x2=%f, f2=%f",x2,f2);
return 0;
}

0 comments: