Finding good, fast, and stable numerical algorithms is usually hard, even for simple problems like the square root, subject we’vevisited a number of times. One method we haven’t explored yet, is using the Taylor series. The Taylor series for around is given by:

Can we exploit that to compute efficiently, and with some precision, square roots?

Well, yes. Kind of. I’ll come back later on the rather important caveat. But let’s try to find some regularity in the expansion. The coefficients seems cumbersome: until we remember that we’ve already saw them before. This allows us to write:

,

which is both regular and (relatively) simple. We can even factor the binomial coefficients and get an efficient iterative algorithm. Here’s a simple Mathematica implementation:

R[a_, n_, k_] := Module[{i, s = 0, c = Sqrt[a]},
For[i = 0, i <= k, i++,
s += c;
c *= 1/( i + 1) (1/2 - i) ((n - a)/a );
];
Return[s]
]

* * *

Let’s see how this thing behaves. Let’s start with numbers on the interval , with . With three terms, we get:

with the green line being the true square root and the red dashed line being the approximation using the Taylor series. Not bad, but we see that the approximation gets somewhat rapidly worse as we get away from one half. That’s not unexpected: we used the expansion around , and we use only three terms. When we are clearly far from , the approximation is just plain wrong, and we must use a lot more terms and another .

When we move away, we must chose different for different . The following graph shows different giving different approximations (with different number of terms):

The graph gives a false sense of precision. The solutions are good to only a (very) few decimals.

* * *

Choosing is a problem. The convergence radius is small, and a single can’t be used for a wide range of . Worst, the Taylor expansion seems to ask us to compute , which is kind of what we’re trying to do in the first place. One way out of this problem is to use a that is a known perfect square, of about the same magnitude than .

* * *

Maybe we could use the first few terms to get a rough estimate for and use that estimate to kick-start some other algorithm, say, Newton’s, to get a refined solution.

This entry was posted on Tuesday, October 4th, 2016 at 17:39 pm and is filed under algorithms, Mathematics. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.