- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Modelling the Secant Method in Python

Secant method is one of the powerful methods to know the x-intercept (zeros) of a polynomial or any transcendental function. In this method, first we select (basically guess) the interval in which we expect the root ($\mathrm{π₯_{1}}$,$\mathrm{π₯_{2}}$). Then we draw a secant line to join the points on the function (A,B) corresponding to the guessed values as shown in the figure below.

The secant line intersects the x-axis at the point $\mathrm{π₯_{3}}$, as $\mathrm{π₯_{3}}$ and $\mathrm{π₯_{2}}$ are not close (i.e., their absolute difference is finite) we find the point corresponding to π₯3 on the curve i.e., C. Then we join a secant line considering points B and C. We will extend the line till the X-axis is arrived, and mark that point as $\mathrm{π₯_{4}}$.

Now again we check whether $\mathrm{π₯_{3}}$ and $\mathrm{π₯_{4}}$ are close or not. As they are also not close, so we find the value of polynomial corresponding to $\mathrm{π₯_{4}}$ and mark it on the curve as D. The figure shown below represents the second secant and point D.

Then again, we draw a secant between C and D till the distance between the next and previous values of "x" converge to a very small value. Hence, we can say that the method finds the roots of a polynomial sequentially i.e. it can be said as a sequential search of root. The beauty of this method is that it doesn't matter that whether the x's are on one side of the root or surrounds the roots. The method gradually converges to the roots by the sequential search as explained above.

Now the task is to evaluate the next x based on the previous two x's. If we consider the first secant then the equation of line passing though them (to evaluate π₯3) will be as follows β

$$\mathrm{x_{3}=x_{1}-y_{1}\frac{x_{2}-x_{1}}{y_{2}-y_{1}}}$$

whereas if we consider the second secant then the equation of line to evaluate will be as follows β

$$\mathrm{x_{4}=x_{2}-y_{2}\frac{x_{3}-x_{2}}{y_{3}-y_{2}}}$$

Therefore, the generalised version of above equation will be as follows β

$$\mathrm{x_{i}=x_{i-2}-y_{i-2}\frac{x_{i-1}-x_{i-2}}{y_{i-1}-y_{i-2}}}$$

## Implementation of Secant Method in Python

The following algorithm can be used to model Secant method β

The Function f(x) whose roots are required has to be defined first

Select two arbitrary values of x's i.e. ($\mathrm{π₯_{1}}$,$\mathrm{π₯_{2}}$)

Evaluate the new value of x by the following formula $$\mathrm{x_{n}=x_{1}-f(x_{1})\frac{x_{2}-x_{1}}{f(x_{2})-f(x_{1})}}$$

If the new and previous value of x are close then the answer is obtained i.e. if $|x_{n_{}}-x2|<10^{-5}$then $π₯_{π}$ is the root. Not the we have taken the convergence criterion as $10^{-5}$ but you can take it based on your requirement.

If $|x_{n_{}}-x2|>10^{-5}$ then set: $π₯_{1}$=$π₯_{2}$ and $π₯_{2}$=$π₯_{π}$

Then again start with the step to evaluate new x.

Then again start with the step to evaluate new x.

Let's suppose we want to find the root of the equation: $π₯^{2}$+3π₯β10=0. Let us select the initial values as ($π₯_{1}$=β4,$π₯_{2}$= 3). Then the python program to perform the Secant method is as follows β

# Importing module from pylab import * # Defining Polynomial function def f(x): return x ** 2 + 3 * x - 10 # Defining function for new value of x def fn(a, b): return a - ((b - a) / (f(b) - f(a))) * f(a) # Creating array of x x = linspace(-15, 15, 150) # Plotting the function figure(1, figsize=(7.20, 3.50)) plot(x, f(x), linewidth=2) plot([-25, 25], [0, 0], "k--") ylim(-15, 20) xlim(-8, 6) # Initial guess Interval x1 = -4 x2 = 3 # Initial Error to enter into the loop error = 1 # Setting iteration counter count = 1 # Integration starts while error > 1.E-3: # Plotting Secant line plot([x1, x2], [f(x1), f(x2)]) # Evaluating new value of x based on old xn = fn(x1, x2) # Plotting x intercept of secant plot([xn], [0], 'o', label=f'{xn}') # Evaluating error error = abs(x2 - xn) # Setting x's for next iteration x1 = x2 x2 = xn # Incrementing loop counter count += 1 # Printing selected value of xn in the legend if count < 6: legend() # Showing root in the figure (just decoration) text(-7, -10, f'Root = {round(xn, 3)}', bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10}) print(f'Root = {round(xn, 3)}') show()

The above code is self-explanatory of the all the steps as mentioned in the beginning of this section. The output of the above program will be as shown in the Figure given below.

For other root you can take the initial "x" as: β6 andβ2. Then the result will be as follows:

## Conclusion

In this article, a detailed discussion of the Secant method has been done. The mathematical background has been given to easily model the method. The users can play with the code given above and can use it to find the roots of other functions.

- Related Articles
- Modelling the Newton Raphson Method in Python
- Modelling the Regula Falsi Method in Python
- Modelling the Carnot Cycle in Python
- Modelling the Rankine Cycle in Python
- Secant method to solve non-linear equation\n
- Modelling the Projectile Motion using Python
- Program to find root of an equations using secant method in C++
- What is the secant of a circle ?
- Explain about modelling in financial management.
- Modelling Of Consumer Behavior
- Cloud Computing in Natural Hazard Modelling Systems
- What are the tools of dimensional modelling project?
- Class method vs static method in Python
- eval method in Python?
- dir() Method in Python