The calculation of the roots of some equations can result in a very lengthy calculation that may not result in a correct root. Due to this, we use Graphical Methods to approximate these roots.
Secant Method is one such graphical methods. It involves using the tangent to the curve to converge to the actual root. We use two random values xi
and x(i-1)
, and draw a tangent between the two. The point at which this tangent cuts the x-axis is now regarded as the second point xi
. The previous xi
is considered as the new first point x(i-1)
and the previous x(i-1)
is no longer considered. Several iterations are performed before we converge to the actual root.
Derivation:
The equation above is the derivative between two points, xi
and x(i-1)
.
The equation above comes from rearranging the equation of a derivative between a point and the x-axis. This equation is also known as Newton Raphson Formula.
By substituting the first equation into the Newton Raphson Formula, we will get the equation above. This equation is the Secant Method.
Take a look at the code below for futher understanding of the equation.
import mathdef func(x):func = 0.95*(pow(x,3))-5.9*(pow(x,2))+10.9*x-6 #Example equationreturn funcdef secant():ea = 100 #Absolute Errorx0 = 2.5x1 = 3.5i=1while ea > 0.1: #This loop will run until the absolute error becomes less than 0.1xr = x1 - ((func(x1) * (x0 - x1)) / (func(x0) - func(x1))) #Secant Formulaea = abs((xr - x1) / xr) * 100 #Updating the errorx0 = x1 #Changing the values for the two points for the tangentx1 = xr # The newly found point becomes the new second point for the tangent.print("Iteration",i)print("Absolute Error", ea)print("Root",xr)i=i+1def main():secant()main()