Bisection Method

So i am trying to write a bisection method code for finding the roots of a given equation (x^3 - 6x^2 +11x -6), but my code keeps getting errors and I am unsure what to change.
The purpose I'd like to achieve is to have the user type in the interval and number of loop iterations, and have the code spit out the root.

#include<iostream.h>
#include<conio.h>
#include<math.h>

float f(float x)
{
float fa;
fa = pow(x, 3) - 6*pow(x, 2) + (11 * x) - 6;
return (fa);
}

void main()
{
float a, b, m;
int count = 0;
int iter;

cout << "Enter a = "; //Low bound (1.5 for this problem)
cin >> a;
cout << "Enter b = "; //High bound (4 for this problem)
cin >> b;
cout << "Enter number of iterations = ";
//Number of iterations loop will run before breaking
cin >> iter;

//Here is what the f(a) and f(b) are:
// int f(a) = fa = (a^3) - 6*(a^2) + (11*a) -6;
// int f(b)= fb = (b^3) - 6*(b^2) + (11*b) - 6;

do
{
if (count == iter)
{
break;
}

m = (a + b) / 2;

cout << "a= " << a << " | b= " << b << " | m= " << m << " | " << " f(a)= " << f(a) << " | f(b)= " << f(b) << " | f(m)= " << f(m) <<endl;

//float temp1 = f(a);
//float temp2 = f(m);
if (f(a) * f(m) < 0)
{
b = m;
}
else
{
a = m;
}
count++;
} while (abs(a - b) < 0.0000001 || f(m) == 0);

}
try this:
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
52
53
54
55
56
57
#include<iostream>
using namespace std;

#include "conio.h"
#include<cmath>

float f(float x)
{
float fa;
fa = pow(x, 3) - 6*pow(x, 2) + (11 * x) - 6;
return (fa);
}

int main()
{
float a, b, m;
int count = 0;
int iter;

cout << "Enter a = "; //Low bound (1.5 for this problem)
cin >> a;
cout << "Enter b = "; //High bound (4 for this problem)
cin >> b;
cout << "Enter number of iterations = ";
//Number of iterations loop will run before breaking
cin >> iter;

//Here is what the f(a) and f(b) are:
// int f(a) = fa = (a^3) - 6*(a^2) + (11*a) -6;
// int f(b)= fb = (b^3) - 6*(b^2) + (11*b) - 6;

do
{
if (count == iter)
{
break;
}

m = (a + b) / 2;

cout << "a= " << a << " | b= " << b << " | m= " << m << " | " << " f(a)= " << f(a) << " | f(b)= " << f(b) << " | f(m)= " << f(m) <<endl;

//float temp1 = f(a);
//float temp2 = f(m);
if (f(a) * f(m) < 0)
{
b = m;
}
else
{
a = m;
}
count++;
} while (abs(a - b) < 0.0000001 || f(m) == 0);

}


Corrections:

add the "using namespace std;"
#include <conio.h> -> #include "conio.h"
void main() -> int main()
#include<iostream.h> -> #include<iostream>
#include<math> -> #include<cmath>
Thanks for the help! I ran the revised code with your suggestions, and got the following errors however:

warning C4627: '#include <iostream>': skipped when looking for precompiled header use

warning C4627: '#include "conio.h"': skipped when looking for precompiled header use

error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source?



The problem is the the while condition at line 54. The condition you've given is when you want to exit the loop, not when you want to repeat it. Also, you need to use fabs() instead of abs. abs() is for integers. So the correct statement is while (fabs(a - b) >= 0.000001 && f(n) != 0);
SO i have drafted another version of the code, but am getting some errors with it as well.

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
#include <iostream>
#include <cmath>
#include "conio.h"
using namespace std;

float f(float x)
{
	return (x*x*x) - (6 * x*x) + (11 * x) - 6;

}
int main() 
{
	float a;
	float b;

	//Prompt user input for initial bounds
	cout << "Enter lower bound: ";
	cin >> a;
	cout << "Enter upper bound: ";
	cin >> b;
	float c = (a + b) / 2;

	//Begin While Loop
	while (((b - a) > .0000001) && (abs(f(c)) > .0000001))
	{
		c = (a + b) / 2;
		if (f(a)*f(c) < 0) {
			b = c;
		}
		if (f(b)*f(c) < 0) {
			a = c;
		}
	}
	//Output root to user
	cout << "Root = " << c;

	_getch();
	
	return 0;
}


Change abs at line 24 to fabs.

If neither line 27 nor line 30 is true then the code goes into an infinite loop.
Which is your compiler? In my compiler there is not error! I'm talking about the first code.
Try this code
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
#include <iostream>
#include <cmath>
#include "conio.h"
using namespace std;

float f(float x)
{
	return (x*x*x) - (6 * x*x) + (11 * x) - 6;

}
int main()
{
	float a;
	float b;

	//Prompt user input for initial bounds
	cout << "Enter lower bound: ";
	cin >> a;
	cout << "Enter upper bound: ";
	cin >> b;
	float c = (a + b) / 2;

	//Begin While Loop
	while (((b - a) > .0000001) && (abs(f(c)) > .0000001))
	{
		c = (a + b) / 2;
		if (f(a)*f(c) > 0) {
			b = c;
		}
		if (f(b)*f(c) > 0) {
			a = c;
		}
	}
	//Output root to user
	cout << "Root = " << c;

	_getch();

	return 0;
}


line 27 (f(a)*f(c) < 0 -> (f(a)*f(c) > 0
line 30 (f(b)*f(c) < 0 -> (f(b)*f(c) > 0
Last edited on
but am getting some errors with it as well.

What errors are you getting? If they are compiler errors post the complete error listing, exactly as they appear in your development environment.

Also if you're going to use the outdated conio.h header file you should be using angle brackets <> not quotation marks " ". Quotation marks are used primarily for include files that are located relative to the project. Angle brackets are usually for include files that are in system/compiler specified directories. This header file is an implementation defined header file located in one of the system/compiler specified directories.

Topic archived. No new replies allowed.