Trusted answers to developer questions

Zain Ali Babar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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:**

$f'(x)=\frac{f(x(i-1))-f(x_i)}{x(i-1)-x_i}$

The equation above is the derivative between two points, `xi`

and `x(i-1)`

.

$x(i+1)= {x_i}-\frac{f(x_i)}{f'(x_i)}$

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.**

$x(i+1)= {x_i}-\frac{f(x_i)(x(i-1)-x_i)}{f(x(i-1))-f(x_i)}$

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()

RELATED TAGS

communitycreator

numerical

analysis

CONTRIBUTOR

Zain Ali Babar

Grokking Modern System Design Interview for Engineers & Managers

Keep Exploring

Related Courses