Finding Roots of Complicated Functions

Shawn Michael
5 min readFeb 16, 2022

Roots in simple terms are numbers that evaluates an equation to zero or numbers that we need to input so that the output is zero. Why even bother finding roots? sometimes we deal with problems that needs those roots in order to satisfy certain conditions for example like the time it needs for a ball to reach a certain height or maybe calculating the amount of products a factory needs to make in order to achieve or gain a certain amount of revenue, and many more of it useful applications. Back when life was so simple, root finding was very easy. But it turns out the closer we try to apply it to real applications, things just seem not turn out the way we expected and it gets really messy.

Say you were told to solve for x in order for f(x) to be zero,

f(x) = x + 1

We can write the equation as,

0 = x + 1

What must the value of x in order f(x) to be zero? -1, right? Well that was easy in fact for linear functions that take the form,

f(x) = ax + b

we can simply set the function to zero and do some simple algebra to solve for x. How about other form of functions? like maybe quadratic. Solving quadratic equations is a bit trickier. Quadratic functions take the form,

f(x) = ax² + bx + c

Normally we would solve it using factorization. Say we want to find the roots of the quadratic equation,

f(x) = x²-7x+12

Now we try to find two numbers, say A and B, that adds up to the middle number in our case that is -7 and multiplies resulting the last number that is 12. We can do this simply by guess and check. Then we put the numbers in the form (x + A)*(x + B) = 0 and solve for x.

Let A = -3 and B = -4

If we add A + B = -3 + (-4) = -7 (correct)

If we multiply A*B = -3*-4 = 12 (correct)

(x-3)*(x-4) = 0

x1 = 3 and x2 =4

It is a bit more lengthy but its faster than using the quadratic formula. Usually we use it when the quadratic function involves square roots, cube roots, and etc. The quadratic formula is written,

x =[-b(+-)sqrt(b²-4ac)]/2a

(Note that the +- is a whole meaning plus and minus implying two conditions to be meet separately)

Linear functions and quadratic functions belong to a more general group of functions called polynomials. Polynomials are functions that take the form,

f(x) = axn^n + bxn-1^n-1 + …+ a0

where n represents the highest order and the maximum amount of possible roots. For linear functions the highest order is 1, quadratic functions is 2, cubic functions is 3, and etc.

Root finding becomes hard for polynomials with higher orders than 2 and functions involving square roots, cube roots also transendental functions like natural logarithm, general exponents, and the famous e and not to forget trigonometric functions and their inverses.

Say we want to find the roots of the function,

f(x) = e^x-4x

Though we could always just guess and check but It will take us time. Here is where numerical methods come to the rescue. Numerical methods are methods use to solve problems numerically when there is no progress or the problem is too hard to be done analytically. In the case for finding roots there are quite many of them but here I will talk about the fastest one that is the Newton-Raphson method.

Basically what we do is:

  • Guess a value of x and plugging it in into the function and check whether it is close to zero. If not you can do it again until you feel if it is necessary to stop and close enough. In here we will get a point (x, y) that lies on the function and use it in the proceeding step.
  • Then we find the derivative of the function and plug in the value of x we guessed in the first step. When done, we now have the slope of the function on the point (x, y).
  • Now with the point (x, y) and slope we got from previous step, we find equation of the line and solve for the new x by setting y = 0. We then plug the new x to f(x) and repeat the steps above again until there are no or little changes in the new x’s we get.

Lets try it to our function above!

f(x) = e^x-4x

  • Say I guess x = 0 then plugging it to f(x) gives f(0) = e⁰-4*0 = 1. So we get the point (0, 1).
  • Now I will differentiate f(x) with respect to x resulting in f’(x) = e^x-4. Now plugging the x value from the first step gives a slope of f’(0) = -3
  • We have the point (0, 1) and the slope at that point to be -3. Making the equation of line that is y-y0 = f’(x0)*(x-x0) we get y-1=-3x or y = -3x+1. Now setting y = 0, we get the value of x to be 1/3 or about 0.333.
  • We then plug x = 1/3 to f(x) and get f(1/3) = e^(1/3)-4/3. So we get our new point (1/3, e^(1/3)-4/3).
  • Now plugging the point to f’(x) to find the slope gives us f’(1/3) = e^(1/3)-4.
  • We now have the new point (1/3, e^(1/3)-4/3) and the new slope at that point that is e^(1/3)-4. Now plugging it to the equation of line we get y-(e^(1/3)-4/3) = (e^(1/3)-4)(x-1/3). Then setting y = 0 and solving for x, we get our new x to be 0.35725.
  • Observe that the value from 0.333 becomes 0.35725, a really slight of change that makes us confident that the root is about so. If we still find it not enough we can simply just do it again.

Now by comparing iterations, you should now have a greater sense of the Newton-Raphson method that the much more iterations we do the more accurate our root will be and that is basically it.

--

--