Program 2: Finding Roots using Secant Method

Consider the figure to the left. The point at which the curve crosses the x axis is called the root of the function. In many cases it is not possible to get an exact expression for the root of a function and you must obtain a numerical approximation to the root. One method for finding the approximation is called the Secant Method. The Secant method begins by selecting two values of x near the root and calculating the corresponding function values. A secant line is then constructed using the two points (x1,f(x1)) and (x2,f(x2)). The x intercept for the secant line is calculated and is used as the new approximation to the root. A new secant line is constructed using the previous point and the current x intercept, and the process is continued until the root is determined within a given tolerance. This process may be summarized by the following formula:
           
The Secant Method requires just one new function evaluation at each step.

Your assignment is to write a function subroutine which implements the secant method. The prototype for the function subroutine is:
            float secant(float x1, float x2, float tol, float (*f)(float x))

The routine should return the approximation to the root. The Secant routine must be independent of the specific function for which you are locating the root. Use this subroutine to find all three real roots of
            f(x) = x3 – 3x + 1
to a tolerance of 5x10-5.