Nov 18, 2008

Regula Falsi on False Position Method

#include
#include
#include

float f(float m)
{
float n;
n=m*m-25;
return n;
}

int main()
{
int n,i;
float x0,x1,x2,e,f0,f1,f2;
printf("Enter the values of x0,x1,e,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=1;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(fabs(f2)<=e)
{
printf("\n\nConvergent solution at %d iteration and x= %f\n",i,x2);
exit(1);
}
if((f0*f2)<0)
{
x1=x2;
f1=f2;
}
else
{
x0=x2;
f0=f2;
}
}
printf("\n\n Does not converge in %d iteration, x2= %f, f2= %f",i,x2,f2);
}

0 comments: