bisection method

Hi guys I was trying to write a program to find roots by using bisection method and got stuck up with a problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
double bisection (int z)
{
		double r;
	r= 2 * (z)^3 - 11*z - 50;
	
	return (r);
}


int main (int argc, char * const argv[]) {
	double fa=0, x=0;
	double fb=0;
	double m=0, product=0;
    double a=0, b=0;
	cout << "Please enter your a value " << endl; 
	cin >> a;
	cout << "Please enter your b value " << endl;
	cin >> b;
loop:
	
	fa= bisection (a);
	fb = bisection (b);
	product = fa * fb; 
	m = (a+b)/2;
	if (product < 0) {
		b=m;
	}
	else {
		a= m;
	}
	x=double abs((a-b));
	if (x > 0.000001) {
		goto loop; 
	}
	else {
		goto loopa;
	}
	
loopa:
	cout << "The root value is " << m << endl;
    return 0;
}


The error i get is

/Users/bluegenep/Desktop/c++/functions/bisection/trial 2/main.cpp:36:0 /Users/bluegenep/Desktop/c++/functions/bisection/trial 2/main.cpp:36: error: expected primary-expression before 'double'

Thanks!!
Last edited on
I think line 33 should be:

x=(double) abs((a-b));
Tried that it didnt work either!!
erroe:

/Users/bluegenep/Desktop/c++/functions/bisection/trial 2/main.cpp:36:0 /Users/bluegenep/Desktop/c++/functions/bisection/trial 2/main.cpp:36: error: call of overloaded 'abs(double)' is ambiguous


Thanks though!
And you were not able to think of a solution? Consider the function 'abs', which apparently has definitions based on http://www.cplusplus.com/reference/clibrary/cmath/abs/ (a quick google for 'abs iostream' reveals this)

1
2
3
4
5

double abs (      double x );
float abs (       float x );
long double abs ( long double x );


In your code, a-b is an int subtracted by an int which also returns an int. Is there a function 'int abs(int x)' above? If not, is there a way to force the compiler to choose on of the available functions?
hey rollie thanks! I was able to compile the program by your suggestion but still i was not able to get the correct root that was 3.54337. my program looked like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <cmath>

using namespace std;
double bisection (int z)
{
	
	
	double r;
	r= 2 * (z)^3 - 11*z - 50;
	
	return (r);
}


int main (int argc, char * const argv[]) {
	double fa=0;
	int x=0;
	double fb=0;
	double m=0, product=0;
    double a, b;
	cout << "Please enter your a value " << endl; 
	cin >> a;
	cout << "Please enter your b value " << endl;
	cin >> b;
loop:
	
	fa= bisection (a);
	fb = bisection (b);
	product = fa * fb; 
	m = (a+b)/2;
	if (product < 0) {
		b=m;
	}
	else {
		a= m;
	}
	x= abs (a-b);
	
	if (x > 0.000000001) {
		goto loop; 
	}
	else {
		goto loopa;
	}
	
loopa:
	cout << "The root value is " << m << endl;
    return 0;
}
however I tried to do the program to do by do while statement to make it more logical but still i was not able to get the correct root value. The program looked like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cmath>

using namespace std;
int n;
double bisection (int z)
{
	double r;
	r= 2 * (z)^3 - 11*z - 50;
	return (r);
}
int main (int argc, char * const argv[]) {
	double fa;
	int x;
	double fb;
	double m, product;
    double a, b;
	cout << "Please enter your a value " << endl; 
	cin >> a;
	cout << "Please enter your b value " << endl;
	cin >> b;	
	
	cout << "How many iterations do you want to do ";
	
	cin >> x;
	while (n<x) {
		
		fa= bisection (a);
		fb = bisection (b);
		product = fa * fb; 
		
		if (product < 0) {
			b=m;
		}
		else {
			a=m;
		}
		m = (a+b)/2;
		n++;
	
	}
		cout << "The root value is " << m << endl;
    return 0;
}
I haven't reviewed everything in the above but, I doubt the line r= 2 * (z)^3 - 11*z - 50; does what you think it does...z^3 is not z*z*z; ^ is the bitwise XOR operator.
Topic archived. No new replies allowed.