[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Plot errors when using Heron's method to estimate square roots in Python

[A plot showing errors when estimating square roots]

The previous example Use Heron's method to estimate square roots in Python explained how to use Heron's method to estimate square roots. The results seem pretty good, but just how good isn't immediately obvious. This post plots the errors between Heron's method and true square roots to see how big the errors are.

The program plots two curves on one graph. The first plots the difference between Heron's method and true square roots. The second plots the error ratio.

Here's the example's code.

import matplotlib.pyplot as plt import math ... # Graph the errors. # Gather the data. xs = [] diffs = [] fracts = [] num_i = 100 for x in range(1, num_i + 1): estimate = estimate_root(x) root = math.sqrt(x) diff = estimate - root pct = diff / root xs.append(x) diffs.append(diff) fracts.append(pct) # Graph it. plt.plot(xs, diffs) plt.plot(xs, fracts) plt.xlabel('Number') plt.ylabel('Error') # Show the plot. plt.show()

The code creates an xs list to holds the points' X coordinates. It also creates the lists diffs and fracts to hold the differences and fractional differences.

Next the code enters a loop to generate some values. For each value of x, the code calculates the estimated square root and the actual square root as given by math.sqrt. It subtracts the two to get the difference and then divides that by the square root to get the fractional difference. (It's basically the percentage difference times 100. If you use the percentage itself, the results are too close to 0 to be interesting.)

The code then adds the X coordinate, difference, and fractional difference to the lists.

After the loop ends, the program calls the matplotlib library's pyplot.plot method to plot the two curves, X versus difference and X versus fractional difference. It finishes by calling plot to make the plot appear.

(If you're using Spyder, be sure to look in the Plots tab or you won't see the plot.)

The graph shows the absolute error in blue and the fractional error in orange. You can see from the graph that the absolute error is decreasing over time. As X grows larger, that means the fractional error grows much smaller. For lager values of X, the fractional difference is practically zero, as shown in the following plot.

[Fractional error approaches zero quickly as X grows larger]

Conclusion

The plots show that Heron's method does a remarkably good job. The absolute error is still decreasing for large values of X, albeit very slowly. For example, at the peak X = 930 the difference is about 0.004099. At the next peak X = 992, the difference has dropped to about 0.003969. That's only a drop of about 0.00013, but because X is getting bigger, the percentage difference drops even faster.

In any event, the differences are tiny so, if you're stuck on a dessert island and you really need to calculate square roots without a calculator (or computer, phone, etc.), then you really need Heron's method. (In addition to an ice skate, volleyball, medical supplies, etc.)

Download the example to experiment with it and to see additional details.

(In my next post, I'll go back to database programming in Python.)

© 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.