Nov 18, 2008

Method of Successive Approximation

Q.This program is for f(x)=x2+4x+4


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

float g(float m)
{
float n;
n=-4/(m+4);
return n;
}

int main()
{
int n,i;
float x0,x1,e;
printf("Enter the value of x0,e,n");
scanf("%f%f%d",&x0,&e,&n);
x1=g(x0);
for(i=1;i<=n;i++)
{
x0=x1;
x1=g(x0);
printf(" i=%d x1=%f\n",i,x1);
if(fabs((x1-x0)/x1)<=e)
{
printf("\nConverge to root at iteration %d and x=%f\n",i,x1);
exit(1);
}
}
printf("Doesnot converge to a root\n");
return 0;
}

1 comments:

Unknown said...

What does that function "g" return?