Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //for implementation of regula falsi Method for
- // solving equations
- #include<stdio.h>
- #include<math.h>
- #define MAX_ITER 1000000
- #define err 0.00001
- double f(double x)
- {
- // return x*x - 4*x - 10;
- return x*x*x - 4*x - 9;
- }
- // Prints root of f(x) in interval [a, b]
- void regulaFalsi(double a, double b)
- {
- int count=0;
- printf("finding the appreox root of the eqn. by REGULA FALSI method");
- if (f(a) * f(b) >= 0)
- {
- printf("You have not assumed right a and b\n");
- return;
- }
- double c = a; // Initialize result
- printf("\na\t\tb\t\tf(a)\t\tf(b)\t\tc\t\tf(c)\n");
- for (int i=0; i < MAX_ITER; i++)
- {
- printf("%.4f \t\t%.4f \t\t%.4f\t\t%.4f",a,b,f(a),f(b));
- // Find the point that touches x axis
- c = (a*f(b) - b*f(a))/ (f(b) - f(a));
- printf("\t\t%.4f\t\t%.4f\n",c,f(c));
- // Check if the above found point is root
- if (f(c)<=0.00001 && f(c)>=-0.00001)
- { // if(fabs(a-c)>err)
- break;
- }
- // Decide the side to repeat the steps
- else if (f(c)*f(a) < 0)
- b = c;
- else if(f(c)*f(a) > 0)
- a = c;
- count++;
- }
- printf( "The value of root is :%.4f ", c);
- printf(" the no of iterations required is %d",count);
- }
- void GetRootRange(double *L, double *U)
- {
- double y;
- for(y=-999999.0l;y<999999.0l;y=y+0.1)
- {
- if(f(y)*f(y+0.1)<0.0)
- {fflush(stdin);
- *L=y;*U=y+0.1;
- return;
- }
- }
- }
- // Driver program to test above ftion :""
- int main()
- {
- // Initial values assumed
- double a , b ;
- GetRootRange(&a,&b);
- regulaFalsi(a, b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement