You are on page 1of 1

NAME - USNISH MUKHERJEE ROLL NO.

S-476
NEWTON-RAPHSON METHOD

#include<stdio.h>
#include<math.h>
float func(float x); /*prototype of function*/
float func1(float x); /*prototype of derivative of function*/
void main()
{
float xn,xn1,h;
float diff,epsilon=1.0e-07;
float x0,x1;
printf("Give initial guess: ");
scanf("%f",&x0);
xn=x0;
h=-func(x0)/func1(x0);
xn1=xn+h;
printf("\nx(n) \t\t x(n+1) \n");
printf("_________________________");
printf("\n%f \t %f",xn,xn1);
/*to check for convergence*/
diff=fabs(xn1-xn);
while (diff>=epsilon) {
xn=xn1;
h=-func(xn)/func1(xn);
xn1=xn+h;
diff=fabs(xn1-xn);
printf("\n%f \t %f",xn,xn1);
}
printf("\nRequired root (correct upto 6D) is %f",xn1);
}
/*function definition*/
float func(float x) {
float fval;
float k=3.1;
fval=x*x*tanh(x) - exp(k*sin(x)) -4;
return fval;
}
/*function derivative definition*/
float func1(float x) {
float fval;
float k=3.1;
fval=2*x*tanh(x) + ((x*x)/pow(cosh(x),2)) - (k*cos(x)*exp(k*sin(x)));
return fval;
}

Output:

Give initial guess: 2.7

x(n) x(n+1)
_________________________
2.700000 2.733528
2.733528 2.734349
2.734349 2.734349
2.734349 2.734349
Required root (correct upto 6D) is 2.734349

You might also like