Bisection method Algorithm

The Bisection method algorithm is a widely-used numerical technique for finding the roots of a continuous function within a given interval. The algorithm is based on the Intermediate Value Theorem, which states that if a continuous function has values of opposite signs at two points, then there must be at least one root between these two points. The Bisection method is particularly useful in cases where other methods, such as the Newton-Raphson method or the Secant method, may fail to converge, providing a guaranteed way of finding the roots of a function, albeit at a slower pace. The Bisection method algorithm works by iteratively dividing the given interval into two equal sub-intervals and determining which sub-interval contains the root using the sign of the function value at the midpoint. The algorithm starts by taking the endpoints of the given interval, and it calculates the midpoint of the interval. Then, it evaluates the function at the midpoint and the endpoints. If the function value at the midpoint is zero or sufficiently close to zero, the algorithm converges, and the root is found. Otherwise, the algorithm checks the sign of the function value at the midpoint and compares it with the sign of the function value at the endpoints. If the sign is different from one of the endpoints, it indicates that the root lies within the sub-interval formed by the midpoint and that endpoint. The algorithm then repeats the process using the new sub-interval until the root is found or a predefined tolerance level is reached.
#include <iostream.h>
#include <conio.h>
#include <math.h>

float eq(float i)
{
	return (pow(i, 3) - (4 * i) - 9); // original equation
}

void main()
{
	float a, b, x, z;
	clrscr();
	for (int i = 0; i < 100; i++)
	{
		z = eq(i);
		if (z >= 0)
		{
			b = i;
			a = --i;
			goto START;
		}
	}

START:

	cout << "\nFirst initial: " << a;
	cout << "\nSecond initial: " << b;
	for (i = 0; i < 100; i++)
	{
		x = (a + b) / 2;
		z = eq(x);
		cout << "\n\nz: " << z << "\t[" << a << " , " << b << " | Bisect: " << x << "]";

		if (z < 0)
		{
			a = x;
		}
		else
		{
			b = x;
		}

		if (z > 0 && z < 0.0009) // stoping criteria
		{
			goto END;
		}
	}

END:
	cout << "\n\nRoot: " << x;
	getch();
}

LANGUAGE:

DARK MODE: