Advertisement
Guest User

False Position Method

a guest
Jun 30th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. // FALSE POSITION METHOD
  2.  
  3. # include <stdio.h>
  4. # include <math.h>
  5.  
  6. double a,b,c;
  7.  
  8. double initial_upper_value() {
  9.     return sqrt( (b/a)*(b/a) - 2*(c/a) );
  10. }
  11.  
  12. double evaluate( double x ) {
  13.     return a*x*x + b*x + c;
  14. }
  15.  
  16. int main() {
  17.     printf( "Ax2 + Bx+ C = 0\n" );
  18.  
  19.     // input
  20.     printf( "A = " );
  21.     scanf( "%lf", &a );
  22.  
  23.     printf( "B = " );
  24.     scanf( "%lf", &b );
  25.  
  26.     printf( "C = " );
  27.     scanf( "%lf", &c );
  28.  
  29.     double x1, x2;
  30.     // collecting initial limits
  31.     x2 = initial_upper_value();
  32.     x1 = -x2;
  33.  
  34.     int counter=0;
  35.     double x;
  36.     while( counter<1000 ) {
  37.         x = x1 - ( ( evaluate(x1)*(x2-x1) ) / ( evaluate(x2)-evaluate(x1) ) );
  38.  
  39.         if( evaluate(x)==0 ) { counter=-10; break; }
  40.         else if( evaluate(x1)*evaluate(x) < 0 ) x2=x;
  41.         else x1=x;
  42.  
  43.         counter++;
  44.     }
  45.  
  46.     if( counter<0 ) printf( "Root is = %lf\n", x );
  47.     else printf( "Approximate root is = %lf\n", x );
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement