Write a program to demonstrate Bisection Method in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (n)Space Complexity: O (1)Open Source: MIT
1
/* Program to execute Bisection method */
2
#include<stdio.h>
3
#include<conio.h>
4
#include<math.h>
5
double f(float x)
6
{
7
	double val=0.0;
8
	val=x*x*x-x*x-x-3;
9
	return val;
10
}
11
void main()
12
{
13
	int maxitr,count=0;
14
	float x0,x1,x2,aerr;
15
	double err,y0,y1,y2;
16
	printf("\nEnter initial guesses, no. of iterations and maximum allowed error\n");
17
	scanf("%f %f %d %f",&x0,&x1,&maxitr,&aerr);
18
	while(count<maxitr)
19
	{
20
		y0=f(x0);
21
		y1=f(x1);
22
		if(y0==0)
23
		{
24
			printf("root is %f",x0);
25
			return;
26
		}
27
		else if(y1 ==0)
28
		{
29
			printf("Root is %f",x1);
30
			return;
31
		}
32
		else if(y0*y1>0)
33
		{
34
			printf("Unsuitable starting values");
35
			return;
36
		}
37
		else
38
		{
39
			x2=(x0+x1)/2;
40
			y2=f(x2);
41
			if(y0*y2<0)
42
				x1=x2;
43
			else
44
				x0=x2;
45
			err=fabs(x1-x0);
46
			if(err<aerr)
47
			{
48
				count ++;
49
				printf("After %d itearations root = %f\n",count,x2);
50
				return;
51
			}
52
			count++;
53
			printf("Iteration no. %d: x= %f\n",count,x2);
54
		}
55
	}
56
	printf("\nSolution does not converge, iterations not sufficient");
57
	return;
58
}

Output

1
Enter initial guesses, no. of iterations and maximum allowed error
2
2
3
3
4
14
5
0.001
6
Iteration no. 1: x= 2.500000
7
Iteration no. 2: x= 2.250000
8
Iteration no. 3: x= 2.125000
9
Iteration no. 4: x= 2.187500
10
Iteration no. 5: x= 2.156250
11
Iteration no. 6: x= 2.140625
12
Iteration no. 7: x= 2.132812
13
Iteration no. 8: x= 2.128906
14
Iteration no. 9: x= 2.130859
15
After 10 itearations root = 2.129883

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run Bisection Method program in C-Language?

To run Bisection Method program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More About Bisection Method

Discovered by: Bernard Bolzano

Discovered Year: 1817

The bisection method, also known as Bolzano’s method, is a simple numerical method used to find the root (solution) of nonlinear equations.

How does the bisection method work?

The bisection method works on the Intermediate Value Theorem. The principle means that if the function changes sign (at one point it is + and at another it is -), then there is at least one root between those points.

And this method divides the interval of the functions into half-half to check whether root exist or not until find the accurate root. Due to dividing the interval, it is called the bisection method.

Why use the bisection method

Complex equations cannot be solved with simple algebraic formulas, but the bisection method can solve complex equations so that we get an approximate yet reliable solution.

Real-life applications of the bisection method

  1. Physics and scientific modeling: To simulate physical forces as well as wave equations. To detect the gas or liquid pressure in a pipeline the bisection method is used because the bisection method will never fail in these types of situations and gives accurate answers in these complex situations.
  2. Electrical Engineering: used to solve the complex equations for the frequencies, impedance, or resonance in a circuit.
  3. Control Systems and Robotics: To find out the root causes of stability and behavior of a system.

Limitations of the Bisection Method

  1. Very slow: due to linear convergence, the bisection method is very slow. That is why it is not suitable for big problems
  2. Sign change dependency: To use this method, the alternate signs should exist at the function intervals.

Alternative to the Bisection method:

Some methods solve the above-mentioned problems

  • Newton-Raphson Method: With the help of the function derivative, the Newton-Raphson method solves the equation very fast instead of the Bisection method.
  • Secant and Regula Falsi methods are also used to improve the speed.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.