Ticker

6/recent/ticker-posts

CPNM LAB PROGRAM -10

10. Implement Newton Raphson method to determine a root of polynomial equation.

                                                                       FLOW CHART
Algorithm: 
Step – 1 : Start 
Step – 2 : Declare x, epsilon, slope, xnew, rerror 
Step – 3 : Print enter initial approximations to root 
Step – 4 : Enter accuracy and limit on allowed slope 
Step – 5 : If(f’(x) < slope) if true go to Step–6 otherwise go to Step–11 
Step – 6 : Print slope is too small 
Step – 7 : Calculate xnew = x - (f(x) / f1(x)) 
Step – 8 : Calculate rerror = fabs((xnew - x) / xnew) 
Step – 9 : Calculate x = xnew 
Step – 10 : Check whether rerror >= epsilon 
Step – 11 : Print the root as xnew 
Step – 12 : Stop 

               Step – 1 : Call function f(float x) 
             Step – 2 : return(x * x * x - 2 * x - 5)
               Step – 3 : Call function f1(float x) 
               Step – 4 : return(3 * x * x - 2)             

Program:-

#include<stdio.h>


#include<conio.h>


#include<stdlib.h>


#include<math.h>


float f(float x)


{


return(x * x * x - 2 * x - 5);


}


float f1(float x)


{


return(3 * x * x - 2);


}


 main() 


{


float x, epsilon, slope, xnew, rerror; 


clrscr();


printf("\nEnter Initial approximation to root : \n"); 


scanf("%f",&x);


printf("\nEnter accuracy and limit on allowed slope : \n"); 


scanf("%f,%f",&epsilon, &slope); 


do {


if(f1(x) < slope)


{


printf("slope is too small \n"); 


break; }


xnew = x - (f(x) / f1(x)); 


rerror = fabs((xnew - x) / xnew); 


x = xnew; }


while(rerror >= epsilon); 


printf("Root = %f ", xnew); 


getch(); 


return 0;


}


 


Output:-

 

Run 1:

 

Enter Initial approximation to root : 3

Enter accuracy and limit on allowed slope :

0.001,0

Root = 2.094552

 

Run 2:

Enter Initial approximation to root :1
 
Enter accuracy and limit on allowed slope :

0.001,2 slope is too small

Root = 0.000000 

Post a Comment

0 Comments