Series for sin(x)
The function sin(x) has a well-known polynomial approximation.
sin(x) ≈ x - x3/6 + x5/120 - ...
This polynomial is produced by matching the function value, and then successively higher derivatives, at x=0. It is the result of the well-known Taylor expansion.
There are other approaches to finding polynomials to approximate sin(x). For instance, consider the roots of sin(x). They occur at x=nπ, where n is an integer. What would a polynomial which reproduces the roots at zero and ±π look like? It would be written most naturally as
P(x)=Ax(x+π)(x-π)
where A is an arbitrary scaling constant. But we can also choose to write this, for a particular value of A, as
P(x)=x(1+x/π)(1-x/π)
The next two roots of sin(x) are at x=±2π, so the polynomial which reproduces these five roots would be
P(x)=x(1+x/π)(1-x/π)(1+x/(2π))(1-x/(2π))
Remembering that (a+b)(a-b)=a2-b2, this can be written more compactly as
P(x)=x(1-x2/π2)(1-x2/(4π2))
This is generating a polynomial approximation to sin(x) not as a sum of terms, but as a product of terms, with the nth term of the product being
(1-x2/(n2π2))
How can we quickly see how good these approximations are? Here we demonstrate the use of python with matplotlib, and also of the free, dedicated graphing program Gnuplot.
Gnuplot
Gnuplot, if installed, is started by simply
typing gnuplot
. It is not installed by default with
Raspbery Pi OS, although it is in the standard package repository,
so one may need to type
$ sudo apt-get install gnuplot
to install it. Here we compare the series above with just two terms each.
$ gnuplot G N U P L O T gnuplot> plot [-4:4][-2:2] x-x**3/6,x*(1-x*x/(pi*pi)),sin(x)
The functions to plot are separated by commas, the
operator **
is the power operator, and the x and y
ranges are explicitly given in square brackets before the functions.
As one might have predicted, the product approximation starts to diverge visibly from sin(x) first, but it does get the roots at ±π precisely correct, so for values around ±π it is a better approximation than the Taylor sum.
If one then moves to including the first three terms in each series, the result is:
gnuplot> plot [-4:4][-2:2] x-x**3/6+x**5/120,x*(1-x*x/(pi*pi))*(1-x*x/(4*pi*pi)),sin(x)
Now the surprise is that the Taylor approximation has just a single
root at the origin, but does not cross the y axis anywhere else,
whereas the product approximation now crosses it five times. Try
setting the range to [-8:8][-3:3]
to see this.
Which approximation is better? It depends which properties of sin(x) it is important to retain in any given situation.
matplotlib
People familiar with matplotlib might prefer to use it, rather than Gnuplot.
$ ipython3 In [1]: import matplotlib.pyplot as plt In [2]: import numpy as np In [3]: x=np.linspace(-4,4,200) In [4]: plt.plot(x,x-x**3/6,'r') Out[4]: [<matplotlib.lines.Line2D at 0x7f5589172ef0>] In [5]: plt.plot(x,x*(1-x*x/(np.pi*np.pi)),'b') Out[5]: [<matplotlib.lines.Line2D at 0x7f5589103e48>] In [6]: plt.plot(x,np.sin(x),'k') Out[6]: [<matplotlib.lines.Line2D at 0x7f5589115a90>] In [9]: plt.show()
One then has to close the plot window in order to use the python window again. And, to repeat something like the second Gnuplot example,
x=np.linspace(-8,8,200) plt.plot(x,x-x**3/6+x**5/120,'r') plt.plot(x,x*(1-x*x/(np.pi*np.pi))*(1-x*x/(4*np.pi*np.pi)),'b') plt.plot(x,np.sin(x),'k') plt.ylim(-3,3) plt.grid(True) plt.show()
Alternatives
The website GeoGebra offers very good online graphing.
Geogebra is also available as a stand-alone application. Like Gnuplot, it is in the default Raspberry Pi OS repository, it is just not installed by default, so one has to install it explicitly.
sudo apt-get install geogebra
Here typing "E=Extremum(f)" has highlighted the four maxima and minima of f, and printed their co-ordinates. It can quickly be found that the position of Taylor series' first turning point at (1.59, 1.00) is much closer to the correct value for the sign wave of (1.57, 1.00) than is that of the product series at (1.71,1.11). The version of GeoGebra currently in the default Raspberry Pi OS repository is somewhat older than the online version, but it is still very useable, especially as some would argue that version 6, which moves from Java to Javascript, is significantly slower on modest hardware.
π surprises
The coefficient of x in both series is one. The coefficient of x3 in the Taylor series is 1/6, whereas for the product it depends on how many terms are included. It initially appears as 1/π2, but changes as more terms are added. Calling this coefficient c3, one can write
c3=1/π2+1/(4π2)+1/(9π2)+...
or
π2c3=1+1/4+1/9+1/16+...+1/n2+...
Given that we know that c3 should ultimately be 1/6, this suggests that the sum of the reciprocals of the squares of the positive integers is π2/6. There may be plenty of caveats about the use of infinite series in this "proof", but the answer is correct.