Advertisement
Arnab_Manna

Regula Falsi. C

Oct 5th, 2020 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | None | 0 0
  1.  //for implementation of regula falsi Method for
  2. // solving equations
  3. #include<stdio.h>
  4. #include<math.h>
  5. #define MAX_ITER 1000000  
  6. #define err 0.00001
  7.  
  8. double f(double x)
  9. {
  10. //    return x*x - 4*x - 10;
  11. return x*x*x - 4*x - 9;
  12. }
  13. // Prints root of f(x) in interval [a, b]
  14.  
  15. void regulaFalsi(double a, double b)
  16. {
  17. int count=0;
  18. printf("finding the appreox root of the eqn. by REGULA FALSI    method");
  19.     if (f(a) * f(b) >= 0)
  20.     {
  21.         printf("You have not assumed right a and b\n");
  22.         return;
  23.     }
  24.     double c = a;  // Initialize result
  25.     printf("\na\t\tb\t\tf(a)\t\tf(b)\t\tc\t\tf(c)\n");
  26.    
  27.     for (int i=0; i < MAX_ITER; i++)
  28.     {
  29.         printf("%.4f \t\t%.4f \t\t%.4f\t\t%.4f",a,b,f(a),f(b));
  30.         // Find the point that touches x axis
  31.         c = (a*f(b) - b*f(a))/ (f(b) - f(a));
  32.         printf("\t\t%.4f\t\t%.4f\n",c,f(c));
  33.         // Check if the above found point is root
  34.        if (f(c)<=0.00001 && f(c)>=-0.00001)
  35.        {    // if(fabs(a-c)>err)
  36.         break;
  37.         }
  38.         // Decide the side to repeat the steps
  39.      else   if (f(c)*f(a) < 0)
  40.             b = c;
  41.         else if(f(c)*f(a) > 0)
  42.             a = c;
  43.            
  44.             count++;
  45.     }
  46.     printf( "The value of root is :%.4f ", c);
  47.     printf(" the no of iterations required is %d",count);
  48. }
  49.  
  50. void GetRootRange(double *L, double *U)
  51.  {
  52.  double y;
  53.  for(y=-999999.0l;y<999999.0l;y=y+0.1)
  54.  {
  55.  if(f(y)*f(y+0.1)<0.0)
  56.  {fflush(stdin);
  57.  *L=y;*U=y+0.1;
  58.  return;
  59.  }
  60.  }
  61.  }
  62. // Driver program to test above ftion :""
  63.  
  64. int main()
  65. {
  66.     // Initial values assumed
  67.     double a , b  ;
  68.     GetRootRange(&a,&b);
  69.     regulaFalsi(a, b);
  70.     return 0;
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement