tampurus

Unit 1.6 Newton Raphson Method

Apr 17th, 2022 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define f(x) ( (x*x*x*x) -x - 10)
  4. #define f1(x) ( (4*x*x*x) - 1)
  5. int main()
  6. {
  7.     float x0,x1,E;
  8.     int i,n;
  9.     printf("Enter the value of x0,E,n\n");
  10.     scanf("%f %f %d",&x0,&E,&n);
  11.     for(i=1 ; i<=n ; i++){
  12.         if(fabs(f1(x1)) <= E ){
  13.             printf("Slope is too small \n");
  14.             return 0;
  15.         }
  16.         x1 = x0 - (f(x0)/f1(x0));
  17.         if(x0==x1){
  18.             printf("Convergent solution\n");
  19.             printf("Hence the root is %f\n",x1);
  20.             printf("No. of interations %d\n",i);
  21.             return 0;
  22.         }
  23.         x0 = x1;
  24.     }
  25.     printf("Eoes not converge in %d interations",i-1);
  26.     return 0;
  27. }
  28.  
  29. /*
  30. Output
  31. Enter the value of x0,d,n
  32. 2 0.001 5
  33. Convergent solution
  34. Hence the root is 1.855585
  35. No. of interations 4
  36. */
Add Comment
Please, Sign In to add comment