wap to implement newton raphson method to find the root of x*sin(x)+cos(x).

#define f(x) x*sin(x)+cos(x)
#define df(x) sin(x)+x*cos(x)-sin(x)
#include<stdio.h>
# include<math.h>
 main()
{
float i,a,b,c;
for(i=0;;i++)
{
if(f(i)>0&&f(i+1)<0)
{
a=i;b=i+1;break;}
if((f(i)<0)&&(f(i+1)>0))
{
a=i+1;b=i;break;}
}
for(i=0;;i++)
{
if(f(b)>0&&df(b)<0)
c=b+(f(b)/fabs(df(b)));
if(f(b)<0&&df(b)>0)
c=b+(fabs(f(b)/df(b)));
if((f(b)>0&&df(b)>0)||(f(b)<0&&df(b)<0))
c=b-(fabs(f(b))/fabs(df(b)));
if(fabs(c-b)<.00001)
break;
b=c;
}
printf("the root is %f",c);
}


No comments:

Post a Comment

Implemet Stack in python

  class Stack : def __init__ ( self , data ): self . stack = [] if ( data ): self . stack . append ( da...