0% found this document useful (0 votes)
22 views5 pages

Bisection Method

Uploaded by

SJV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Bisection Method

Uploaded by

SJV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

The Bisection Method is a numerical method for finding roots of a function.

Here's a simple program


in Turbo C++ to find a root of a function using the Bisection Method. This example uses the function \
( f(x) = x^3 - x - 2 \).

```cpp

#include <iostream>

#include <cmath>

using namespace std;

// Function to find the root of

double f(double x) {

return x * x * x - x - 2;

// Function to perform the Bisection Method

void bisection(double a, double b, double tol) {

if (f(a) * f(b) >= 0) {

cout << "The function has the same signs at the endpoints a and b. The Bisection Method cannot
be applied." << endl;

return;

double c;

while ((b - a) / 2.0 > tol) {

c = (a + b) / 2.0;

if (f(c) == 0) {

break; // We've found the root

} else if (f(c) * f(a) < 0) {

b = c;

} else {

a = c;
}

cout << "The root is: " << c << endl;

int main() {

double a, b, tol;

// Input the interval [a, b] and the tolerance

cout << "Enter the lower limit a: ";

cin >> a;

cout << "Enter the upper limit b: ";

cin >> b;

cout << "Enter the tolerance: ";

cin >> tol;

// Perform the Bisection Method

bisection(a, b, tol);

return 0;

```

### Explanation:

1. **Function Definition (`f`)**:

- Defines the function \( f(x) = x^3 - x - 2 \) whose root is to be found.

2. **Bisection Method Function (`bisection`)**:

- Takes three parameters: `a` (lower limit), `b` (upper limit), and `tol` (tolerance).
- Checks if the function has opposite signs at `a` and `b`. If not, the Bisection Method cannot be
applied.

- Uses a loop to iteratively halve the interval until the interval width is less than the tolerance.

- Calculates the midpoint `c` and checks if it is the root. If not, updates the interval to continue the
search.

- Outputs the root when the desired tolerance is achieved.

3. **Main Function**:

- Prompts the user to input the interval `[a, b]` and the tolerance.

- Calls the `bisection` function to find and print the root.

This program finds the root of the given function within a specified interval and tolerance using the
Bisection Method. The user must ensure that the function has opposite signs at the interval
endpoints for the method to be applicable.
#include <iostream.h>

#include <conio.h>

#include <math.h>

// Function to find the root of

double f(double x) {

return x * x * x - x - 2;

// Function to perform the Bisection Method

void bisection(double a, double b, double tol) {

if (f(a) * f(b) >= 0) {

cout << "The function has the same signs at the endpoints a and b. The Bisection Method cannot
be applied." << endl;

return;

double c;

while ((b - a) / 2.0 > tol) {

c = (a + b) / 2.0;

if (f(c) == 0) {

break; // We've found the root

} else if (f(c) * f(a) < 0) {

b = c;

} else {

a = c;

cout << "The root is: " << c << endl;


}

int main() {

double a, b, tol;

// Input the interval [a, b] and the tolerance

cout << "Enter the lower limit a: ";

cin >> a;

cout << "Enter the upper limit b: ";

cin >> b;

cout << "Enter the tolerance: ";

cin >> tol;

// Perform the Bisection Method

bisection(a, b, tol);

getch(); // To keep the console window open

return 0;

You might also like