Amazon Affiliate

Tuesday 17 April 2012

PROGRAM FOR NEWTON RAPHSON METHOD IN C..


//Program for newton raphson method in c.....

include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x1,x2,xn,fx1,fx2,fxn,dx2,dxn,con=-99;
int i=0;
clrscr();

printf("enter the value of x1:");
scanf("%f",&x1);

printf("enter the value of x2:");
scanf("%f",&x2);

fx1=pow(x1,3)+3*x1*x1-3;
fx2=pow(x2,3)+3*x2*x2-3;
printf("fx1 : %.4f\nfx2 : %.4f\n",fx1,fx2);

if(x1>x2)
dx2=3*x2*x2+6*x2;
else
{
dx2=3*x1*x1+6*x1;
x2=x1;
fx2=fx1;
}

while(fabs(con-x2)>.0001)
{
i=i+1;
if(i>15)
goto last;
con=x2;
xn=x2-(fx2/dx2);
fxn=pow(xn,3)+3*xn*xn-3;
dxn=3*xn*xn+6*xn;
printf("x%d : %f\tfx%d : %f\n",i+1,xn,i+1,fxn);
x2=xn;
fx2=fxn;
dx2=dxn;
}
last:
printf("Root of equation : %f",xn);
getch();
}

No comments:

Post a Comment