Pages - Menu

Tuesday, 22 July 2014

Implementation of Newton Rapson method in C.

Here is the code..

//AIM:TO SOLVE GIVEN EQUATION BY NEWTON RAPHSON METHOD

#include<stdio.h>

float fx(float );
float fx1(float );
void main()
{
float a,b,x,i;
int j,n;

printf("the given equation fx = (x*x)-12\n");
for(i=1;fx(i)<0;)
i+=0.2;
a=i;
for(i=1;fx(i)>0;)
i-=0.2;
b=i;
x= (a+b)/2.0;
printf("enter no. of itrations\n");
scanf("%d",&n);
for(j=0;j<n;j++)
{
x=(x-(fx(x)/fx1(x)));
printf("root after %d iteration is %f",(j+1),x);
}
printf("hence the root is %f",x);

}
float fx(float d)
{
return(((d*d)-12));
}
float fx1(float e)
{
return((2*e));
}
For Turbo C users.
//AIM:TO SOLVE GIVEN EQUATION BY NEWTON RAPHSON METHOD

#include<stdio.h>
#include<conio.h>
float fx(float );
float fx1(float );
void main()
{
float a,b,x,i;
int j,n;
clrscr();
printf("the given equation fx = (x*x)-12\n");
for(i=1;fx(i)<0;)
i+=0.2;
a=i;
for(i=1;fx(i)>0;)
i-=0.2;
b=i;
x= (a+b)/2.0;
printf("enter no. of itrations\n");
scanf("%d",&n);
for(j=0;j<n;j++)
{
x=(x-(fx(x)/fx1(x)));
printf("root after %d iteration is %f",(j+1),x);
}
printf("hence the root is %f",x);
getch();
}
float fx(float d)
{
return(((d*d)-12));
}
float fx1(float e)
{
return((2*e));
}

No comments:

Post a Comment