Getting Started with Computation.

Shivam Sharma
Caffeine2Code
Published in
2 min readJun 5, 2020

--

I see you are curious to learn a thing about computation. I must say that it was really difficult journey to be able to express what I learnt, till now, But as you are here Let’s Get started.

While I was learning the basics about the computation using Python, I came across two approaches to find the square root of a number(pre-defined for now).
It seem easy to hear, but wait.
One of the approach was Bisection search and the other was Newton-Raphson method. I was concerned with efficiency of both trends and the number of iteration each uses.

The N-R
  1. The first function is New_Raph (use any name you like)
    It’s parameters are k(value 25) that is the number whose square root is to be found and eps which is initially assigned value 0.01. The N-R guesses a number (starting with half the value of k) and uses an operation (line 12), to update the value such that the guess can produce the value closer to the required square root.
    NOTE: The final value of guess is an approximation to the required value.
    The variable ‘iter’ is used to count the number of iterations taken to reach the final guess.
The function bisection search.
The Bisection search.

2. The Bisection Search goes like this:
We assign low =0 , high = max(1.0,x)( including the negative integers).
The bisection search follows as- it first finds the middle of the range(line 20), then checks whether the middle guess/no. has square greater then or less then the given number i.e. x(line 21), and then updates the depending upon the result it gets.

**The main use of eps is to get the answer closest to the given number.**

Finally, The output of above code is:

Output
The value of K, initialised as 25.

The values 2.5973.. and 6.1035.. are the variance of the values/ ans found form the actual answer i.e. 5.00(float).

Thus as evident from the Output the Newton-Raphson gives better efficiency than Bisection search and also uses lesser number of iterations.

This does not imply that Bisection search is not good, the method of computation majorly depends upon your approach and requirements.

I HOPE YOU LIKED IT.

--

--