Consider the following Taylor series for sin(θ/7)
and the following two functions based on the series, one takes only the first non-zero term
def short_series(x): return 0.14285714*x
and a second that three non-zero terms.
def long_series(x): return 0.1425714*x - 4.85908649e-04*x**3 + 4.9582515e-07*x**5
Which is more accurate? Let’s make a couple plots plot to see.
First here are the results on the linear scale.
Note that the short series is far more accurate than the long series! The differences are more dramatic on the log scale. There you can see that you get more correct significant figures from the short series as the angle approaches zero.
What’s going on? Shouldn’t you get more accuracy from a longer Taylor series approximation?
Yes, but there’s an error in our code. The leading coefficient in
long_series is wrong in the 4th decimal place. That small error in the most important term outweighs the benefit of adding more terms to the series. The simpler code, implemented correctly, is better than the more complicated code with a small error.
The moral of the story is to focus on the leading term. Until it’s right, the rest of the terms don’t matter.
Update: Based on some discussion after publishing this post, I think my comment about
short_series being simpler misdirected attention from my main point. Here’s a variation on the same argument based on two equally complex functions.
def typo1(x): return 0.1425714*x - 4.85908649e-04*x**3 + 4.9582515e-07*x**5 def typo2(x): return 0.14285714*x - 5.85908649e-04*x**3 + 4.9582515e-07*x**5
Both functions have one typo,
typo1 in the first non-zero coefficient and
typo2 in the second non-zero coefficient. These two functions behave almost exactly like
short_series respectively. A typo in the leading coefficient has a much bigger impact on accuracy than a typo in one of the other coefficients.
Related post: Life lessons from differential equations