Nov 18, 2008

Newton Raphson Method

Q.This program is to solve y=x2-25.

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

float x0,x1,f0,f1,f01,epsilon,delta,m;
int i,n;

float f(float a)
{
float b;
b=a*a-25;
return b;
}

float g(float c)
{
float d;
d=2*c;
return d;
}

float fun11()
{
printf("\nSlope too small. x0=%f, f0 =%f, f0'=%f0,i=%d\n",x0,f0,f01,i);
exit(1);
}

float fun13()
{
printf("\n%d %f %f %f %f %f",i+1,x0,x1,f0,f01,m);
printf("\n\nConvergent solution at %d iteration as x= %f\n",i,x1);
exit(1);
}
int main()
{
printf("Enter the values of x0,epsilon, delta, n\n");
scanf("%f%f%f%d",&x0,&epsilon,&delta,&n);
printf("Iteration x0 x1 f(x0) f'(x0) |(x1-x0)/x1|\n");
for(i=0;i<=n-1;i++)
{
f0=f(x0);
f01=g(x0);
if((fabs(f01))<=delta)
{
fun11();
}
x1=(x0-(f0/f01));
m=fabs((x1-x0)/x1);
if(m<=epsilon)
{
fun13();
}
printf("\n%d %f %f %f %f %f",i+1,x0,x1,f0,f01,m);
x0=x1;
}
printf("Slope too small x0= %f,f0=%f,f0'=%f,i=%d\n",x0,f0,f01,i);
return 0;
}

0 comments: