Tuesday 20 September 2011

Newton Raphson Method


/* This Program finds root by Newton Raphson Method*/

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

float root(float x)
{
float f;
f=x*sin(x)+cos(x);
return(f);
}

float diff(float x)
{
float g;
g=x*cos(x);
return(g);
}

void main()
{
int i,n;
float a,x,e,t;
clrscr();
printf("Type a\n");
scanf("%f",&a);
printf("Type maximum number of iteration n\n");
scanf("%d",&n);
printf("Type the value of e\n");
scanf("%f",&e);
i=1;t=1;
while(t>e&&i<=n)
{
 if(diff==0)
 {
  printf("\n Root does not found");
  printf("Type another value of a\n");
 }
 else
 x=a-root(a)/diff(a);
 t=fabs(x-a);
    if(t>fabs(root(x)))
 t=fabs(root(x));
 a=x;
 i++;
 printf("\n x=%f",x);
}
if(i>n)
printf("Iteration does not converge");
else
 {
  printf("\n The approximate root=%8.4f\n",x);


 printf("The number of iteration=%d\n",i);
 }
getch();
}

No comments:

Post a Comment