Let’s understand the secant method in numerical analysis and learn how to implement secant method in C programming with an explanation, output, advantages, disadvantages and much more.

#### What is Secant Method?

The secant method is a root-finding method that uses a succession of the roots of secant lines to find a better approximation of root.

The secant method algorithm is a root bracketing method and is the most efficient method of finding the root of a function. It requires two initial guesses which are the start and end interval points. This method is very similar to the Regula Falsi method.

The secant method is a **Quasi-Newton** method and it is seen to be faster in execution in some scenarios as compared to Newton-Raphson method and the False Position Method well.

The secant algorithm does not ensure convergence. The rate of convergence of secant method algorithm is **1.618**, which is really fast comparatively. The order of convergence of secant method is superlinear.

The equation used in the following secant method c programs are as follows:

- x
^{3}– 5x + 3 - x
^{3}– 3x – 8

#### Secant Method Formula

#### Secant Method Algorithm

Step 1: Assume an equation f(x) = 0(which must be defined) Step 2: Input Start and Final values of the interval Step 3: Do xi+1 = xi - (f(xi) - (xi - xi-1)/(f(xi) - f(xi-1)) where i = 1, 2, 3, 4, ..... (while no convergence criteria is achieved) Step 4: Evaluated Result Step 5: Exit

#### Convergence criteria for Secant Method

- Fixing apriori the total number of iterations (limit).
- Testing the condition (x
_{i}+1−x_{i}), is less than some tolerance limit (epsilon).

#### Advantages

- It evaluates one function at every iteration as compared with Newton’s method which evaluates two.
- The convergence rate is very fast.
- The secant method rule does not use the derivatives of a function.

#### Disadvantages

- The convergence may not always happen.
- Newton’s method generalizes much efficiently to new methods for solving simultaneous systems of nonlinear equations as compared to the Secant method.

**Note**: This secant method in C programming is compiled with GNU GCC compiler using CodeLite IDE on Microsoft Windows 10 operating system.

#### Method 1: C Program For Secant Method using Do While Loop

#include<stdio.h> double equation(float temp); int main() { float start_interval, end_interval, midpoint, allowed_error; int count, limit; printf("\nEnter Value for Start Interval Point:\t"); scanf("%f", &start_interval); printf("\nEnter Value for End Interval Point:\t"); scanf("%f", &end_interval); printf("\nEnter Value for Allowed Error:\t"); scanf("%f", &allowed_error); printf("\nEnter Limit for Iterations:\t"); scanf("%d", &limit); do { if(equation(start_interval) == equation(end_interval)) { printf("Please enter different start and end intervals\n"); return 0; } midpoint = (start_interval * equation(end_interval) - end_interval * equation(start_interval)) / (equation(end_interval) - equation(start_interval)); start_interval = end_interval; end_interval = midpoint; printf("\nIteration: %d\tRoot: %f\n", count, midpoint); count++; if(count == limit) { break; } }while(fabs(equation(midpoint)) > allowed_error); printf("\nRoot of the Equation:\t%f", midpoint); return 0; } double equation(float temp) { return(temp * temp * temp - 5 * temp + 3); }

#### Output

#### Method 2: Implement Secant Method in C Programming using While Loop

#include<stdio.h> #include<math.h> #define EQUATION(x) (x)*(x)*(x) - 3*(x) - 8 int main() { float interval_start, interval_end, midpoint; float a, b, temp, allowed_error; printf("\nEnter Value for Start Interval Point:\t"); scanf("%f", &interval_start); printf("\nEnter Value for End Interval Point:\t"); scanf("%f", &interval_end); printf("\nEnter value for Allowed Error:\t"); scanf("%f", &allowed_error); printf("\n"); printf("\nStart\t\tEnd\t\tMidPoint\tfunction(Start)\tfunction(End)"); printf("\n"); while(temp > allowed_error) { a = EQUATION(interval_start); b = EQUATION(interval_end); midpoint = interval_end - ((b * (interval_end - interval_start)) / (b - a)); printf("\n%f\t%f\t%f\t%f\t%f", interval_start, interval_end, midpoint, a, b); interval_start = interval_end; interval_end = midpoint; if(b < 0) { temp = fabs(b); } else { temp = b; } } printf("\n\nRoot of the Equation: %f\n", midpoint); }

#### Output

If you have any doubts about the implementation of Secant method in C programming, let us know about it in the comment section. Find more about it on **WikiPedia**.