Square Root Calculator

Often we take the buttons on our calculator for granted. But what really goes into the calculations behind them?

I wanted to build a program that approximates the square root of a function using basic calculus skills. While the calculations are somewhat simple, it is important to understand the inner workings of the calculations we use so often.

Try out the program I made in Sage below: 

Exploring The Tangent Line

Linear Approximation on Steroids

An Interactive Applet powered by Sage and MathJax.

(By Gretchen Boxdorfer)



Last modified on April 6th, 2018.

The Algorithm to approximate the square root of n:

  1. Find the perfect squares directly above and below n
  2. Build tangent lines at these two points
  3. Build a secant line between these two points
  4. Approximate the square root of n to be halfway between the secant line at n and the lesser of the two tangent lines at n

For more accurate approximation, repeat the steps above with rational squares with denominators of increasing perfect squares (4, 9, 16, etc.) until desired accuracy is achieved.
The Thought Behind the AlgorithmThe function of the square root is a concave down function. Therefore, tangent lines will be an overestimate of the true value and secant lines will be an underestimate of the true value. Thus, the true value lies somewhere between these estimates. The algorithm performs its steps until an estimate is found, and then measures how far away the estimate is from the true value. This estimate can be made more accurate by entering smaller values for the "desired error", repeating the steps of the algorithm until the value between the estimate and the true square root is less than the desired error value. This error can continue shrinking closer and closer to zero until the limits of the program are reached. 

Activities for the Square Root Calculator

Estimating Square Roots

Beginning to understand how the square root calculator operates 

You can get a general estimate of the square root of a number by finding the perfect squares on either side and creating a range of values. 

For example, can you find the square root of 28 in your head? 

Probably not, but you CAN find the square root of 25 and the square root of 36, which are the perfect squares above and below 28. Therefore, you know that the value of the square root of 28 is somewhere between 5 and 6.

The Square Root Calculator uses this same logic. When you input n, the program searches for the nearest perfect squares to begin estimation.

Try it yourself! Can you find a range of values for the square root of 45? 183? How about 215? 764?

Estimating Square Roots – On Steroids!

Using denominators other than 1

Not only can you find perfect squares with a denominator of 1, but you can find perfect squares by creating a fraction of a numerator with a perfect square and a denominator of another perfect square! This can greatly improve the accuracy of the estimation. My professor and I call these the "steroids" of the program.

Take the example of the square root of 28. We decided this value falls between 5 and 6, because 25 < 28 < 36. BUT we can get even more accurate when we use denominators other than 1. 25 is essentially 25/1. 

Let's try denominators of 4. This isn't quite as easy to calculate in your head, but if you really tried you would find that 28 falls between 25/1 and 121/4. This gives us a smaller range, making the estimation more accurate! 

Why does the estimation still use 25 as the lower bound? While using steroids is great, sometimes it's not needed! 25 was still the closest perfect square less than 28. 

The square root calculator uses these "steroids" to meet the desired accuracy level input. It starts with a denominator of 1, but if the estimation isn't close enough it begins to bump that number up. Next it tries 3, then 9, then 16, and so on. This helps make the square root estimation more and more accurate. 

Try it yourself! Start with a small number, like 7. Can you find the closest perfect squares above and below it using "steroids"? Start with a denominator of 4, and then keep going to see how close you can estimate! 

Further Estimating Square Roots

Using the secant line to improve accuracy

Getting a range of values for estimation is better than nothing, but we can do even better than that! 

So we know by now the square root of 28 falls between 25 and 36. (In fact, we know that it falls between 25/1 and 121/4, but we'll get to that later!) We can build a secant line between 25 and 36, then plug in 28 to get an estimate! 

Picture this like a graph where the input values are the original numbers, and the output values are the square roots of the given numbers. So our secant line would go through points (25, 5) and (36, 6). Now this becomes simple linear approximation. We calculate the slope of (6-5) / (36-25), which is 1/11. Then we can use point-slope form to plug in one of our ordered pairs. Let's use (25, 5). Now our equation becomes y - 5 = (1/11)(x - 25). This simplifies to y = (1/11)x + 30/11. We can now plug in 28 for the x value, which gives us y = 57/11 or 5.18. 

The exact square root of 28 is 5.29, so our estimate is pretty close! But we can get even closer. 

Let's use the points (25, 5) and (121/4, 11/2), and let's use (121/4, 11/2) in the point-slope formula. Our equation is now y = (1/11)x + 121/44. If we plug in 28, we get 5.30! This is almost exactly the square root of 28. And that was with only a simple use of steroids. You can keep going until you get as accurate of an estimate as you would like! 

Try using the secant line approximation technique (with or without steroids) to find the square root of some numbers. Try: 13, 67, 249, and 584. 


Sage for Beginners

A guided exercise for dabbling in Sage, using the methods from my program

Try your hand at some programming in Sage with the following steps:

Can you find the square root of 5? 

Can you build tangent and secant lines from the points x = 1 and x = 7 on the square root function?

Can you write a code that uses a "while" loop or a "for" loop?


© 2019 Gretchen Boxdorfer. All rights reserved.
Powered by Webnode
Create your website for free! This website was made with Webnode. Create your own for free today! Get started