Secant method Algorithm

The Secant method is a root-finding algorithm that provides an iterative approach to approximating the roots of a real-valued function. It is an open method, meaning it does not require the function to be bracketed between two initial estimates. The Secant method is an improvement over the Bisection method and the Regula Falsi method, as it converges faster, and it is relatively simple to implement. The algorithm is based on linear interpolation, where the function is approximated by a secant line between two points, and the root is then estimated to be the point where the secant line intersects the x-axis. To initiate the Secant method, two initial approximations x0 and x1 are required, which do not necessarily need to bracket the root. The iterations proceed by the following formula: x_n+1 = x_n - f(x_n) * (x_n - x_n-1) / (f(x_n) - f(x_n-1)), where f(x) is the given function, and x_n and x_n-1 are the two most recent approximations. The process continues until the difference between successive approximations falls below a pre-specified tolerance level or the maximum number of iterations is reached. The Secant method converges at a rate that is faster than the Bisection method but slower than Newton's method. However, it requires only one function evaluation per iteration, as opposed to Newton's method, which requires both function and derivative evaluations.
#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, z, c, m, n;
	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++)
	{
		float h, d;
		m = eq(a);
		n = eq(b);

		c = ((a * n) - (b * m)) / (n - m);
		a = b;
		b = c;

		z = eq(c);
		if (z > 0 && z < 0.09) // stoping criteria
		{
			goto END;
		}
	}

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

LANGUAGE:

DARK MODE: