Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // FALSE POSITION METHOD
- # include <stdio.h>
- # include <math.h>
- double a,b,c;
- double initial_upper_value() {
- return sqrt( (b/a)*(b/a) - 2*(c/a) );
- }
- double evaluate( double x ) {
- return a*x*x + b*x + c;
- }
- int main() {
- printf( "Ax2 + Bx+ C = 0\n" );
- // input
- printf( "A = " );
- scanf( "%lf", &a );
- printf( "B = " );
- scanf( "%lf", &b );
- printf( "C = " );
- scanf( "%lf", &c );
- double x1, x2;
- // collecting initial limits
- x2 = initial_upper_value();
- x1 = -x2;
- int counter=0;
- double x;
- while( counter<1000 ) {
- x = x1 - ( ( evaluate(x1)*(x2-x1) ) / ( evaluate(x2)-evaluate(x1) ) );
- if( evaluate(x)==0 ) { counter=-10; break; }
- else if( evaluate(x1)*evaluate(x) < 0 ) x2=x;
- else x1=x;
- counter++;
- }
- if( counter<0 ) printf( "Root is = %lf\n", x );
- else printf( "Approximate root is = %lf\n", x );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement