圆周率π的几种计算方法与分析

5 篇文章 0 订阅
2 篇文章 0 订阅

圆周率( π \Large π π)的计算方法

慈悲心待人,时时有平安;智慧心安己,处处有自在!
From 翔文数学 © kumath@outlook.com
\\[1em]

一、圆周率介绍


代数方面

代数上来讲,圆周率 π \pi π 就是一个无理数(irrational numbers, 即无限不循环小数),不能表示为两个整数之比;也是一个超越数(transcendental numbers, 即不能作为某个有理系数多项式方程的根的数),与三角函数有密切关系,并且与无穷级数的和有很大关系。

几何方面

几何上来说,就是一个比值,特殊图形圆的周长与其直径的比值(ratio of the circumference of a circle to its diameter),也就是单位圆的半周长,还可以是单位圆的面积或圆面积与其半径的平方之比;也有人提出:如果用圆的周长与其半径的比值作为圆周率的话,也不是不可以的。

用希腊字母 π \pi π 表示圆周率,它的几何定义就是 π = 圆周长 圆直径 = circumference diameter \pi =\dfrac{圆周长}{圆直径} =\dfrac{\text{circumference}}{\text{diameter}} π=圆直径圆周长=diametercircumference

由上可得:圆面积 A = π r 2 ; A=\pi r^2; A=πr2; 圆周长 C = 2 π r = π d ; C=2\pi r = \pi d; C=2πr=πd;

权威网站

π \Large \red \pi π 是一个数学常数,互联网上有很多关于圆周率的介绍,比较权威的参见 http://www.numberworld.org/y-cruncher/

现代计算机已经计算出 π \pi π 的小数位数

有很多计算圆周率的方法,比较常用的有:

  • 测量方法,量出圆周长和直径,用几何方法计算;
  • 用多边形逼近方法计算;
  • 用无限级数求和方法计算;
  • 用蒙特卡罗法计算;

常用的 π \pi π 的近似值有 3.14 , 3.1415926 , 22 7 , 355 113 , 3.14 < π < 22 7 3.14, 3.1415926, \dfrac{22}{7}, \dfrac{355}{113}, 3.14<\pi<\dfrac{22}{7} 3.14,3.1415926,722,113355,3.14<π<722

南北朝时期的祖冲之不满足刘徽的计算结果,成功计算出了圆周率小数点后7位数字,确定了圆周率介于 3.1415926 < π < 3.1415927 3.1415926<\pi<3.1415927 3.1415926<π<3.1415927 之间;得到了两个近似分数(有理数)约率 22 7 \dfrac{22}{7} 722 和祖率(也称为密率) 355 113 \dfrac{355}{113} 113355

2022年6月,Google 云团队 Emma Haruka Iwao 算出了 100 100 100 兆位为 0 \LARGE{\red{0}} 0 (100-trillion decimal place is 0),运行程序是 y-cruncher 程序,采用的算法是 Chudnovsky 算法。运行了 157 157 157 天。一共用了 515 515 515 TB 的存储空间。验证用了公式 Bailey-Borwein-Plouffe formula

2021年8月19日,由瑞士格力孙应用技术大学 (University of Applied Sciences of the Grisons—UAS Grisons) 打破记录,历时仅 114 天 (4月28日~8月19日),计算到了 62.8 62.8 62.8 万亿位(Trillion digits), 精确位数为 6 2 ′ 83 1 ′ 85 3 ′ 07 1 ′ 750 62'831'853'071'750 62831853071750 。 最后10位数字为 7817924264 7817924264 7817924264

打破了 2020年1月29日由美国阿拉巴马州的 Timothy Mullican 耗时 8 个月(303天)创造的 50 50 50 万亿位!

谷歌工程师 Emma Haruka Iwao 于2019年1月宣布已经计算到了 31.4 31.4 31.4 万亿 = 31.4 × 1 0 12 = 3.14 × 1 0 13 = 31.4 × 10^{12} = 3.14\times10^{13} =31.4×1012=3.14×1013 位,为了纪念 圆周率日-PAI Day(每年的3月14日)。

下面主要阐述 现代分析方法 对计算圆周率的贡献。

泰勒级数理论(Taylor’s theorem and Taylor series)

多项式逼近任意函数

多项式 (polynomial) 逼近 (approximate) 未知函数 f f f 在给定点附近的值,实际上就是找到多项式的系数 (coefficients): a 0 , a 1 , ⋯   , a n a_0, a_1, \cdots, a_n a0,a1,,an

P n ( x ) = a 0 + a 1 ( x − a ) + a 2 ( x − a ) 2 + ⋯ + a n ( x − a ) n + R ( ( x − a ) n + 1 ) \begin{equation} P_n(x)=a_0+a_1(x-a)+a_2(x-a)^2+\cdots+a_n(x-a)^n+R((x-a)^{n+1})\end{equation} Pn(x)=a0+a1(xa)+a2(xa)2++an(xa)n+R((xa)n+1)

靠近给定点 x = a x=a x=a 逼近函数 f ( x ) f(x) f(x), 此处假定 (assuming) 函数 f ( x ) f(x) f(x) x = a x = a x=a n t h nth nth 微分(differentiable). 从而有 f ( x ) ≈ P n ( x ) f(x)\approx P_n(x) f(x)Pn(x), 下面试着找出系数 a n , n = 0 , 1 , 2 , ⋯   , n a_n, n=0,1,2,\cdots,n an,n=0,1,2,,n.

>>> f=Function('f')
>>> series(f(x))
f(0) + x*Subs(Derivative(f(_x), _x), _x, 0) + x**2*Subs(Derivative(f(_x), (_x, 2)), _x, 0)/2 + x**3*Subs(Derivative(f(_x), (_x, 3)), _x, 0)/6 + x**4*Subs(Derivative(f(_x), (_x, 4)), _x, 0)/24 + x**5*Subs(Derivative(f(_x), (_x, 5)), _x, 0)/120 + O(x**6)

泰勒级数(Taylor series) 实函数或复函数(a real or complex-valued function) f ( x ) f(x) f(x) x = a x=a x=a 有无限阶微分,则可以表示为幂级数 (power series):

f ( x ) ≈ f ( a ) + f ′ ( a ) 1 ! ( x − a ) + f ′ ′ ( a ) 2 ! ( x − a ) 2 + f ′ ′ ′ ( a ) 3 ! ( x − a ) 3 + ⋯ \begin{equation} f(x) \approx f(a)+{\frac {f'(a)}{1!}}(x-a)+{\frac {f''(a)}{2!}}(x-a)^{2}+{\frac {f'''(a)}{3!}}(x-a)^{3}+\cdots \end{equation} f(x)f(a)+1!f(a)(xa)+2!f′′(a)(xa)2+3!f′′′(a)(xa)3+

此处 n ! n! n! 表示 n n n的阶乘 (denotes the factorial), f ( n ) ( a ) f^{(n)}(a) f(n)(a) 表示 f f f x = a x=a x=a 处的 n t h nth nth 微分 (derivative). 可以写成更加紧凑的求和式子:

泰勒级数 f ( x ) = ∑ n = 0 ∞ f ( n ) ( a ) n ! ( x − a ) n . f(x)={\displaystyle \sum _{n=0}^{\infty }{\frac {f^{(n)}(a)}{n!}}(x-a)^{n}.} f(x)=n=0n!f(n)(a)(xa)n.

f f f 的0阶导数就是它自己 f f f,且 ( x − a ) 0 = 0 ! = 1 (x − a)^0 = 0!=1 (xa)0=0!=1. 当 a = 0 a = 0 a=0, 此时的泰勒级数又叫做 麦克劳林级数 Maclaurin series.

举例 (For instance)

指数函数(exponential function)

  • 指数函数是重要的基本初等函数之一。一般地, y = a x y=a^x y=ax 函数 ( a (a (a 为常数且 a > 0 , a ≠ 1 ) a>0,a≠1) a>0a=1) 叫做 指数函数
  • 指数函数的定义域是 x ∈ R x \in \mathbb{R} xR;
  • 值域 (Domain)    ( 0 , + ∞ ) \text{(Domain)}\;(0,+\infty) (Domain)(0,+);
  • 单调递增: a > 1 a>1 a>1 时, 单调递减: 0 < a < 1 0 < a < 1 0<a<1 时。

注意,在指数函数的定义表达式中,在 a x a^x ax 前的系数必须是数1,

指数函数应用到值 a = e a=e a=e 上的这个函数写为 exp ⁡ ( x ) \exp(x) exp(x)。还可以等价的写为 e x e^x ex,这里的 e e e 是数学常数,就是自然对数的底数,近似等于 2.718281828 2.718281828 2.718281828,还称为欧拉数.它的严格定义是 e = lim ⁡ n → + ∞ ( 1 + 1 n ) n e=\displaystyle \lim_{n\to +\infty}\left(1+\dfrac{1}{n}\right)^n e=n+lim(1+n1)n

作为实数变量 x x x 的函数, e x e^x ex 的图像总是正的(在 x x x 轴之上)并递增(从左向右看)。它永不触及 x x x 轴,尽管它可以无限程度地靠近 x x x 轴(所以, x x x 轴是这个图像的水平渐近线。它的反函数是 自然对数 ln ⁡ ( x ) \ln(x) ln(x),其定义域是所有正数 x > 0 x>0 x>0

指数函数 e x e^x ex x 0 = 0 x_0 = 0 x0=0 处的泰勒级数展开式为:

∑ n = 0 ∞ x n n ! = x 0 0 ! + x 1 1 ! + x 2 2 ! + x 3 3 ! + x 4 4 ! + x 5 5 ! + ⋯ = 1 + x + x 2 2 + x 3 6 + x 4 24 + x 5 120 + ⋯ \displaystyle \begin{aligned}\sum _{n=0}^{\infty }{\frac {x^{n}}{n!}}& = {\frac {x^{0}}{0!}}+{\frac {x^{1}}{1!}}+{\frac {x^{2}}{2!}}+{\frac {x^{3}}{3!}}+{\frac {x^{4}}{4!}}+{\frac {x^{5}}{5!}}+\cdots \\& = 1+x+{\frac {x^{2}}{2}}+{\frac {x^{3}}{6}}+{\frac {x^{4}}{24}}+{\frac {x^{5}}{120}}+\cdots \end{aligned} n=0n!xn=0!x0+1!x1+2!x2+3!x3+4!x4+5!x5+=1+x+2x2+6x3+24x4+120x5+

  • e x e^x ex x x x 求导,始终不变,即 d k d x k e x = e x \dfrac{\mathbf{d}^{k} }{\mathbf{d}x^k} e^x = e^x dxkdkex=ex
  • e 0 = 1 e^0=1 e0=1.
  • 剩下的分子 (numerator) 为 ( x − 0 ) n (x − 0)^n (x0)n, 分母 (denominator) 为 n ! n! n!.

欧拉公式 (Euler formula): e i x = cos ⁡ ( x ) + i    sin ⁡ ( x ) e^{ix}=\cos(x)+\text{i}\; \sin(x) eix=cos(x)+isin(x)

>>> series(sin(x),n=10)
x - x**3/6 + x**5/120 - x**7/5040 + x**9/362880 + O(x**10)
>>> series(cos(x),n=10)
1 - x**2/2 + x**4/24 - x**6/720 + x**8/40320 + O(x**10)
>>> series(exp(x),n=10)
1 + x + x**2/2 + x**3/6 + x**4/24 + x**5/120 + x**6/720 + x**7/5040 + x**8/40320 + x**9/362880 + O(x**10)

指数函数 e z e^z ez z 0 = 0 z_0=0 z0=0 的泰勒级数展开式为

e z = ∑ n = 0 ∞ z n n ! = z 0 0 ! + z 1 1 ! + z 2 2 ! + z 3 3 ! + z 4 4 ! + z 5 5 ! + ⋯ = 1 + z + z 2 2 + z 3 6 + z 4 24 + z 5 120 + ⋯ \displaystyle \begin{aligned}{e^z}&=\sum _{n=0}^{\infty }{\frac {z^{n}}{n!}}\\&={\frac {z^{0}}{0!}}+{\frac {z^{1}}{1!}}+{\frac {z^{2}}{2!}}+{\frac {z^{3}}{3!}}+{\frac {z^{4}}{4!}}+{\frac {z^{5}}{5!}}+\cdots \\&=1+z+{\frac {z^{2}}{2}}+{\frac {z^{3}}{6}}+{\frac {z^{4}}{24}}+{\frac {z^{5}}{120}}+\cdots \end{aligned} ez=n=0n!zn=0!z0+1!z1+2!z2+3!z3+4!z4+5!z5+=1+z+2z2+6z3+24z4+120z5+

z = i x z=ix z=ix(纯虚数), 此处 i i i 虚数单位(imaginary unit), 满足 i 4 k = 1 , i 4 k + 1 = i , i 4 k + 2 = − 1 , i 4 k + 3 = − i , k ∈ N + i^{4k}=1, i^{4k+1}=i, i^{4k+2}=-1, i^{4k+3}=-i, k\in \mathbb{N^+} i4k=1,i4k+1=i,i4k+2=1,i4k+3=i,kN+.

右边为 1 + i x − x 2 2 ! − i x 3 3 ! + x 4 4 ! + i x 5 5 ! − x 6 6 ! − i x 7 7 ! + ⋯ {\displaystyle {\begin{aligned} 1+ix-\frac{x^2}{2!}-i \frac{x^3}{3!}+\frac{x^4}{4!}+i \frac{x^5}{5!}-\frac{x^6}{6!}-i \frac{x^7}{7!}+\cdots \end{aligned}}} 1+ix2!x2i3!x3+4!x4+i5!x56!x6i7!x7+

所以与正弦函数和余弦函数的泰勒展开式对比,得到

cos ⁡ ( x ) = 1 − x 2 2 ! + x 4 4 ! − x 6 6 ! + ⋯ (偶函数) sin ⁡ ( x ) = x − x 3 3 ! + x 5 5 ! − x 7 7 ! + ⋯ (奇函数) \displaystyle \red{\cos(x)} = 1-\frac{x^2}{2!}+\frac{x^4}{4!}-\frac{x^6}{6!}+\cdots (偶函数)\\[1em] \red{\sin(x)} = x-\frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!}+\cdots (奇函数) cos(x)=12!x2+4!x46!x6+(偶函数)sin(x)=x3!x3+5!x57!x7+(奇函数)

上面也是函数 cos ⁡ ( x ) \cos(x) cos(x) sin ⁡ ( x ) \sin(x) sin(x) 的泰勒级数展开式. (请参照 Colin Maclaurin series, ∀ x \forall x x)

右边可以简写为 cos ⁡ ( x ) + i    sin ⁡ ( x ) {\cos(x)+\text{i}\;\sin(x)} cos(x)+isin(x), 简写为 cis ( x ) \text{cis}(x) cis(x), 三个字母的缩写,产生一个新的函数。

故有 e i  x = cos ⁡ ( x ) + i    sin ⁡ ( x ) {\boxed{\large {e^{\text{i }x}=\cos(x)+\text{i}\;\sin(x)}}} ex=cos(x)+isin(x)

写成紧凑的求和式为:
cos ⁡ ( x ) = ∑ n = 0 ∞ ( − 1 ) n x 2 n ( 2 n ) ! = 1 − x 2 2 ! + x 4 4 ! − x 6 6 ! + ⋯   . \displaystyle \begin{aligned}\cos(x)&=\sum _{n=0}^{\infty }{\frac {(-1)^nx^{2n}}{(2n)!}}\\&={1}-{\frac {x^{2}}{2!}}+{\frac {x^{4}}{4!}}-{\frac {x^{6}}{6!}}+\cdots .\end{aligned} cos(x)=n=0(2n)!(1)nx2n=12!x2+4!x46!x6+.

sin ⁡ ( x ) = ∑ n = 0 ∞ ( − 1 ) n x 2 n + 1 ( 2 n + 1 ) ! = x − x 3 3 ! + x 5 5 ! − x 7 7 ! + ⋯   . \displaystyle \begin{aligned}\sin(x)&=\sum _{n=0}^{\infty }{\frac {(-1)^nx^{2n+1}}{(2n+1)!}}\\&={x}-{\frac {x^{3}}{3!}}+{\frac {x^{5}}{5!}}-{\frac {x^{7}}{7!}}+\cdots .\end{aligned} sin(x)=n=0(2n+1)!(1)nx2n+1=x3!x3+5!x57!x7+.

可见, cos ⁡ ( x ) \cos(x) cos(x) 是偶函数, sin ⁡ ( x ) \sin(x) sin(x) 是奇函数

显然,我们也可以通过泰勒展开得到上述式子。多阶导数有下列规律:

sin ⁡ ( 2 k ) ( x ) = d ( 2 k ) sin ⁡ ( x ) d x = ( − 1 ) k sin ⁡ ( x ) sin ⁡ ( 2 k + 1 ) ( x ) = ( − 1 ) k cos ⁡ ( x ) \sin^{(2k)}(x)= \frac{d^{(2k)}\sin(x)}{dx}=(-1)^k \sin(x)\\[1em] \sin^{(2k+1)}(x)=(-1)^k \cos(x) sin(2k)(x)=dxd(2k)sin(x)=(1)ksin(x)sin(2k+1)(x)=(1)kcos(x)

sin ⁡ ( x ) \sin(x) sin(x) x = 0 x=0 x=0 处的偶次导数为 0, sin ⁡ ( x ) \sin(x) sin(x) x = 0 x=0 x=0 奇次导数为 ( − 1 ) k (-1)^k (1)k

欧拉恒等式 (Euler identity) 的由来

只要在欧拉公式 cis ( x ) = e i  x = cos ⁡ ( x ) + i  sin ⁡ ( x ) \text{cis}(x) = \boxed{\red {e^{\text{i }x} = \cos(x)+\text{i } \sin(x)}} cis(x)=ex=cos(x)+sin(x) 中令 x = π x=\pi x=π, 即可得到 上帝恒等式——欧拉恒等式

e i  π = − 1 , e i  π + 1 = 0 \begin{equation}\boxed{\red {e^{\text{i }\pi} = -1, e^{\text{i }\pi} + 1=0}}\end{equation} eπ=1,eπ+1=0

神奇的是:这个欧拉恒等式包含了 e , i , π , 0 , 1 e, i, \pi, 0, 1 e,i,π,0,1 这五个神奇的数, 还有 + , = +,= +,= 这两个基本运算符,故称为上帝公式。

多项式逼近理论应用 (Polynomial approximation theorem)

下面应用多项式逼近理论,得到几个常用的三角函数( sin ⁡ ( x ) , cos ⁡ ( x ) , tan ⁡ ( x ) \sin(x),\cos(x),\tan(x) sin(x),cos(x),tan(x))的乘积展开式。主要还是代数学中的求根方法。

问题: 求方程 sin ⁡ ( x ) = 0 \sin(x)=0 sin(x)=0 的解。

  1. 首先, sin ⁡ ( x ) = 0 \sin(x)=0 sin(x)=0 有解 { k π , k = 0 , ± 1 , ± 2 , ⋯   . } \{kπ,k=0,\pm 1,\pm 2, \cdots .\} {k=0,±1,±2,.}

  2. 假设 sin ⁡ ( x ) \sin(x) sin(x) 是多项式函数,由多项式有根的代数基本理论 (the fundamental theorem of algebra) 即 多项式逼近理论 (polynomial approximation theorem)

    sin ⁡ ( x ) = c ∗ ∏ k = 1 ∞ ( k π − x ) ( k π + x ) x sin ⁡ ( x ) x = c ∗ ∏ k = 1 ∞ ( k π − x ) ( k π + x ) \displaystyle \sin(x) = c*\prod_{k=1}^{\infty} (kπ-x)(kπ+x)x \\[1em]\frac{\sin(x)}{x} = c*\prod_{k=1}^{\infty} (kπ-x)(kπ+x) sin(x)=ck=1(x)(+x)xxsin(x)=ck=1(x)(+x)

    x → 0 x \to 0 x0, 求极限得到
    c = ∏ k = 1 ∞ 1 k 2 π 2 \displaystyle c=\prod_{k=1}^{\infty} \frac {1}{k^2π^2} c=k=1k2π21

    我们得到:

    sin ⁡ ( x ) = x ∏ k = 1 ∞ ( 1 − x k π ) ( 1 + x k π ) = x ∏ k = 1 ∞ [ 1 − x 2 ( k π ) 2 ] \displaystyle \begin{aligned} \sin(x) &= x {\prod_{k=1} ^{\infty} ({1- \frac{x}{kπ})} (1+ \frac{x}{kπ})} \\&= x \prod_{k=1} ^{\infty} \left[1- \frac{x^2}{(kπ)^2}\right ] \end{aligned} sin(x)=xk=1(1x)(1+x)=xk=1[1()2x2]

x = π 2 x = \frac{π}{2} x=2π, 有

π 2 = ∏ k = 1 ∞ 2 k 2 k − 1 ⋅ 2 k 2 k + 1 = ∏ k = 1 ∞ 1 1 − 1 4 k 2 = ∏ k = 1 ∞ [ 1 + 1 4 k 2 − 1 ] = 2 1 ⋅ 2 3 ⋅ 4 3 ⋅ 4 5 ⋅ 6 5 ⋅ 6 7 ⋯ = 4 3 ⋅ 16 15 ⋅ 36 35 ⋅ 64 63 ⋯ \displaystyle \begin{aligned} \frac{π}{2} &= \prod_{k=1} ^{\infty} \frac{2k}{2k-1} \centerdot \frac{2k}{2k+1} \\&= \prod_{k=1} ^{\infty} \frac{1}{1- \frac{1}{4k^2}} \\&= \prod_{k=1} ^{\infty} \left[1+\frac{1}{4k^2-1}\right] \\&=\frac{2}{1} \centerdot \frac{2}{3} \centerdot \frac{4}{3} \centerdot \frac{4}{5} \centerdot \frac{6}{5} \centerdot \frac{6}{7} \cdots \\&=\frac{4}{3} \centerdot \frac{16}{15} \centerdot \frac{36}{35} \centerdot \frac{64}{63} \cdots \end{aligned} 2π=k=12k12k2k+12k=k=114k211=k=1[1+4k211]=123234545676=34151635366364

参见 John Wallis’ product for π π π

因为 sin ⁡ ( π / 2 − x ) = cos ⁡ ( x ) \sin(\pi/2-x)=\cos(x) sin(π/2x)=cos(x), 所以有偶函数 cos ⁡ ( x ) \cos(x) cos(x) 的无穷级数乘积公式:
f c 1 ( x ) = cos ⁡ ( x ) ≈ ( π 2 − x ) ∏ k = 1 ∞ ( 1 − π / 2 − x k π ) ( 1 + π / 2 − x k π ) = ( π 2 − x ) ∏ k = 1 ∞ ( 1 − 1 2 k + x k π ) ( 1 + 1 2 k − x k π ) \displaystyle \begin{aligned} \red{fc_1(x)}=\cos(x)&\approx(\frac{\pi}{2}-x) \prod_{k=1} ^{\infty} {\left(1 - \frac{\pi/2-x}{kπ}\right)} {\left(1 + \frac{\pi/2-x}{kπ}\right)}\\&=(\frac{\pi}{2}-x) \prod_{k=1} ^{\infty} {\left(1-\frac{1}{2k}+\frac{x}{k \pi}\right)}{\left(1+\frac{1}{2k}-\frac{x}{k \pi}\right)} \end{aligned} fc1(x)=cos(x)(2πx)k=1(1π/2x)(1+π/2x)=(2πx)k=1(12k1+x)(1+2k1x)

同理可得

f c 2 ( x ) = cos ⁡ ( x ) ≈ ( 1 − 2 x π ) ∏ k = 1 ∞ ( 1 + 2 x ( 2 k − 1 ) π ) ( 1 − 2 x ( 2 k + 1 ) π ) = ( 1 − 2 x π ) ∏ k = 1 ∞ ( 1 + x / π k − 1 / 2 ) ( 1 − x / π k + 1 / 2 ) \displaystyle \begin{aligned} \red{fc_2(x)}=\cos(x)&\approx\left(1-\frac{2x}{\pi}\right) \prod_{k=1} ^{\infty} {\left(1 + \frac{2x}{(2k-1)π}\right)} {\left(1 - \frac{2x}{(2k+1)π}\right)}\\&=\left(1-\frac{2x}{\pi}\right) \prod_{k=1} ^{\infty} {\left(1+\frac{x/\pi}{k-1/2}\right)}{\left(1-\frac{x/\pi}{k+1/2}\right)} \end{aligned} fc2(x)=cos(x)(1π2x)k=1(1+(2k1)π2x)(1(2k+1)π2x)=(1π2x)k=1(1+k1/2x/π)(1k+1/2x/π)

所有正整数平方的倒数和

通过泰勒级数展开理论和多项式逼近理论这两种不同方法所得到的结果进行比较。

>>> series(sin(x),n=10)
x - x**3/6 + x**5/120 - x**7/5040 + x**9/362880 + O(x**10)
  1. 泰勒展开理论得到
    sin ⁡ ( x ) = x − x 3 3 ! + x 5 5 ! + ⋯ \begin{align} \sin(x)&=x-\frac{x^3}{3!}+\frac{x^5}{5!}+\cdots \end{align} sin(x)=x3!x3+5!x5+

  2. 多项式逼近理论得到
    sin ⁡ ( x ) = x ∏ k = 1 ∞ ( 1 − x 2 ( k π ) 2 ) \begin{align} \sin(x)&= x \prod_{k=1} ^{\infty} (1- \frac{x^2}{(kπ)^2}) \end{align} sin(x)=xk=1(1()2x2)

  3. 比较 x 3 x^3 x3 的系数可得
    1 3 ! = ∑ k = 1 ∞ 1 ( k π ) 2 \begin{align} \frac{1}{3!}&= \sum_{k=1} ^{\infty} \frac{1}{(kπ)^2} \end{align} 3!1=k=1()21

    所以
    π 2 6 = ∑ k = 1 ∞ 1 k 2 = 1 + 1 4 + 1 9 + ⋯ + 1 n 2 + ⋯ \displaystyle \begin{aligned} \frac{π^2}{6}&=\sum_{k=1} ^{\infty} \frac{1}{k^2}\\&=1+\frac{1}{4}+\frac{1}{9}+\cdots + \frac{1}{n^2}+\cdots \end{aligned} 6π2=k=1k21=1+41+91++n21+
    收敛速度还可以接受。

    from sympy import summation, Symbol, oo
    k = Symbol('k',integer=True)
    summation(1/k**2,(k,1,oo))
    

    计算
    π = 6 ∑ k = 1 ∞ 1 k 2 \pi=\sqrt{6 \displaystyle \sum_{k=1}^{\infty}\dfrac{1}{k^2}} π=6k=1k21

格雷戈里-莱布尼茨级数(Gregory-Leibniz series)

格雷戈里-莱布尼茨级数于 1671年2月15日提出。利用反正切函数的泰勒级数展开得到 π 4 \dfrac{\pi}{4} 4π 的级数算法。

>>> from sympy import *
>>> x,y,z=symbols('x y z')
>>> series(atan(x),n=10)
x - x**3/3 + x**5/5 - x**7/7 + x**9/9 + O(x**10)

反正切函数的级数 (series for the inverse tangent function), 也称为 Gregory’s series:
arctan ⁡ ( x ) = x − x 3 3 + x 5 5 − x 7 7 + ⋯ = ∑ k = 0 ∞ ( − 1 ) k x 2 k + 1 2 k + 1 \displaystyle \begin{aligned} \arctan(x)&=x-\dfrac{x^3}{3}+\dfrac{x^5}{5}-\dfrac{x^7}{7}+\cdots\\&=\sum_{k=0}^\infty(-1)^k\dfrac{x^{2k+1}}{2k+1} \end{aligned} arctan(x)=x3x3+5x57x7+=k=0(1)k2k+1x2k+1

具体推导过程如下:
∵ d ( arctan ⁡ x ) d x = 1 1 + x 2 \because \dfrac{\mathbf{d}(\arctan x)}{\mathbf{d}x}=\dfrac{1}{1+x^2} dxd(arctanx)=1+x21
∵ 1 − x n = ( 1 − x ) ( 1 + x + x 2 + ⋯ + x n − 1 ) ∴ 1 1 − x = ∑ n = 0 ∞ x n ,    ∣ x ∣ < 1 \because 1-x^n=(1-x)(1+x+x^2+\cdots+x^{n-1})\\\therefore \dfrac{1}{1-x}=\displaystyle\sum_{n=0}^{\infty} x^n,\;|x|<1 1xn=(1x)(1+x+x2++xn1)1x1=n=0xn,x<1
1 1 + x 2 = 1 1 − ( − x 2 ) = ∑ n = 0 ∞ ( − x 2 ) n = ∑ n = 0 ∞ ( − 1 ) n x 2 n \dfrac{1}{1+x^2}=\dfrac{1}{1-(-x^2)}=\displaystyle\sum_{n=0}^{\infty}(-x^2)^n=\sum_{n=0}^{\infty}(-1)^nx^{2n} 1+x21=1(x2)1=n=0(x2)n=n=0(1)nx2n

∴ arctan ⁡ ( x ) = ∫ 1 1 + x 2 d x = ∫ ∑ n = 0 ∞ ( − 1 ) n x 2 n d x = ∑ n = 0 ∞ ( − 1 ) n x 2 n + 1 2 n + 1 ,    ∣ x ∣ < 1 \begin{aligned} \therefore \arctan(x)&=\int\dfrac{1}{1+x^2}\mathrm{d}x\\ &=\displaystyle\int\sum_{n=0}^{\infty}(-1)^nx^{2n}\mathrm{d}x\\ &=\sum_{n=0}^{\infty}(-1)^n\dfrac{x^{2n+1}}{2n+1}, \;|x|<1 \end{aligned} arctan(x)=1+x21dx=n=0(1)nx2ndx=n=0(1)n2n+1x2n+1,x<1

莱布尼茨公式Leibniz formula

π 4 \dfrac{π}{4} 4π 可以通过 x = 1 x = 1 x=1 ( T O D O ) \red{(TODO)} (TODO) 代入上述反正切函数的级数中得到.这里有一个疑问,既然 要求 ∣ x ∣ < 1 |x|<1 x<1, 又怎么能取 x = 1 x=1 x=1 呢?这里存在一个 0 × ∞ = ? 0 0\times\infty \overset{?} = 0 0×=?0 的问题。

π 4 = ∑ n = 0 ∞ ( − 1 ) n 2 n + 1 = 1 1 − 1 3 + 1 5 − 1 7 + ⋯   . \displaystyle \frac{\pi}{4} = \sum_{n=0}^{\infty}\frac{(-1)^n}{2n+1} = \frac{1}{1}- \frac{1}{3}+ \frac{1}{5}- \frac{1}{7}+ \cdots. 4π=n=02n+1(1)n=1131+5171+.

正如你看到,这个级数的收敛极慢!不可取。但是,通过变形,得到很多计算 π / 4 \pi/4 π/4 的公式。

反正切函数的特殊用途

马青——John Machin (1680-1751) 英国伦敦人, 于 1706 年发现公式:
π = 4 [ 4 arctan ⁡ ( 1 / 5 ) − arctan ⁡ ( 1 / 239 ) ] \pi = 4[4\arctan(1/5)-\arctan(1/239)] π=4[4arctan(1/5)arctan(1/239)]

上式得益于 Gregory 有关反正切函数的幂级数展开式:
arctan ⁡ ( x ) = ∑ n = 0 ∞ ( − 1 ) n x 2 n + 1 2 n + 1 \displaystyle \arctan(x) = \sum_{n=0}^{\infty}(-1)^n\dfrac{x^{2n+1}}{2n+1} arctan(x)=n=0(1)n2n+1x2n+1
x = 1 x=1 x=1,则有:
π 4 = ∑ n = 0 ∞ ( − 1 ) n ( 2 n + 1 ) ( 4 ( 1 / 5 ) 2 n + 1 − ( 1 / 239 ) 2 n + 1 ) \displaystyle \large\frac{\pi}{4}=\normalsize \sum_{n=0}^{\infty}\frac{(-1)^n}{(2n+1)} \left(4(1/5)^{2n+1}-(1/239)^{2n+1} \right) 4π=n=0(2n+1)(1)n(4(1/5)2n+1(1/239)2n+1)

鉴于 Gregory-Leibniz 级数收敛速度极慢,人们想出很多办法来改进此算法。

(1) tan ⁡ − 1 1 3 = π 6 = 1 3 ( 1 − 1 3 ⋅ 3 + 1 5 ⋅ 3 2 − 1 7 ⋅ 3 3 + . . . ) \tan^{-1}\frac{1}{\sqrt{3}} = \frac{\pi}{6} = \frac{1}{\sqrt{3}}{(1 - \frac{1}{3\cdot 3} + \frac {1}{5\cdot 3^2} - \frac {1}{7\cdot 3^3} + ...)} tan13 1=6π=3 1(1331+53217331+...)
上式是夏普在1699年发现的,计算π到72位小数位;

(2) tan ⁡ − 1 1 = tan ⁡ − 1 1 2 + tan ⁡ − 1 1 3 = 1 2 ( 1 − 1 3 ⋅ 2 2 + 1 5 ⋅ 2 4 − . . . ) + 1 3 ( 1 − 1 3 ⋅ 3 2 + 1 5 ⋅ 3 4 − . . . ) \tan^{-1}1 = \tan^{-1}\frac{1}{2} + \tan^{-1}\frac{1}{3} = \frac{1}{2}{(1 - \frac{1}{3\cdot 2^2} + \frac{1}{5\cdot 2^4} - ...)} + \frac{1}{3}{(1 - \frac{1}{3\cdot 3^2} + \frac{1}{5\cdot 3^4} - ...)} tan11=tan121+tan131=21(13221+5241...)+31(13321+5341...)

(3) tan ⁡ − 1 ( 1 ) = 4 tan ⁡ − 1 1 5 − tan ⁡ − 1 1 239 \tan^{-1}(1) = 4 \tan^{-1}\frac{1}{5} - \tan^{-1}\frac{1}{239} tan1(1)=4tan151tan12391
上式是英国伦敦人约翰马青 John Machin 于1706年发现的,计算π到101位小数位。经程序验证,收敛极快,精度提高也快。小数点位数与迭代次数之比大约为1.4,即1000次迭代可以得到小数点后1400位。

(4) tan ⁡ − 1 ( 1 ) = 5 tan ⁡ − 1 1 7 + 2 tan ⁡ − 1 3 79 \tan^{-1}(1) = 5 \tan^{-1}\frac{1}{7} + 2 \tan^{-1}\frac{3}{79} tan1(1)=5tan171+2tan1793

(5) tan ⁡ − 1 ( 1 ) = 2 tan ⁡ − 1 1 3 + tan ⁡ − 1 1 7 \tan^{-1}(1) = 2 \tan^{-1}\frac{1}{3} + \tan^{-1}\frac{1}{7} tan1(1)=2tan131+tan171
欧拉于1755年发现,计算π到126位小数位。

(6) tan ⁡ − 1 ( 1 ) = 4 tan ⁡ − 1 1 5 − 2 tan ⁡ − 1 1 408 + tan ⁡ − 1 1 1393 \tan^{-1}(1) = 4 \tan^{-1}\frac{1}{5} - 2 \tan^{-1}\frac{1}{408} + \tan^{-1}\frac{1}{1393} tan1(1)=4tan1512tan14081+tan113931
计算π到136位小数位。

(7) tan ⁡ − 1 ( 1 ) = 4 tan ⁡ − 1 1 5 − tan ⁡ − 1 1 70 + tan ⁡ − 1 1 99 \tan^{-1}(1) = 4 \tan^{-1}\frac{1}{5} - \tan^{-1}\frac{1}{70} + \tan^{-1}\frac{1}{99} tan1(1)=4tan151tan1701+tan1991
计算π到152位小数位。

(8) tan ⁡ − 1 ( 1 ) = tan ⁡ − 1 1 2 + tan ⁡ − 1 1 5 + tan ⁡ − 1 1 8 \tan^{-1}(1) = \tan^{-1}\frac{1}{2} + \tan^{-1}\frac{1}{5} + \tan^{-1}\frac{1}{8} tan1(1)=tan121+tan151+tan181
计算π到200位小数位。

(9) tan ⁡ − 1 ( 1 ) = tan ⁡ − 1 1 2 + tan ⁡ − 1 1 3 \tan^{-1}(1) = \tan^{-1}\frac{1}{2} + \tan^{-1}\frac{1}{3} tan1(1)=tan121+tan131

(10) tan ⁡ − 1 ( 1 ) = 2 tan ⁡ − 1 1 2 − tan ⁡ − 1 1 7 \tan^{-1}(1) = 2\tan^{-1}\frac{1}{2} - \tan^{-1}\frac{1}{7} tan1(1)=2tan121tan171

(11) tan ⁡ − 1 ( 1 ) = 12 tan ⁡ − 1 1 18 + 8 tan ⁡ − 1 1 57 − 5 tan ⁡ − 1 1 239 \tan^{-1}(1) = 12\tan^{-1}\frac{1}{18} + 8\tan^{-1}\frac{1}{57} - 5\tan^{-1}\frac{1}{239} tan1(1)=12tan1181+8tan15715tan12391
Gauss公式,收敛速度最快,小数点位数与迭代次数之比大约为2.5,即1000次迭代可以得到小数点后2500位。

Nilakantha Series

π = 3 + 4 2 ⋅ 3 ⋅ 4 − 4 4 ⋅ 5 ⋅ 6 + 4 6 ⋅ 7 ⋅ 8 − 4 8 ⋅ 9 ⋅ 10 + ⋯ = 3 + 1 1 ⋅ 3 ⋅ 2 − 1 2 ⋅ 5 ⋅ 3 + 1 3 ⋅ 7 ⋅ 4 − 1 4 ⋅ 9 ⋅ 5 + ⋯ = 3 + ∑ n = 2 ∞ ( − 1 ) n ( n − 1 ) n ( 2 n − 1 ) \displaystyle \begin{aligned} {\pi} &= 3+ \frac{4}{2 \centerdot 3 \centerdot 4}- \frac{4}{4 \centerdot 5 \centerdot 6}+ \frac{4}{6 \centerdot 7 \centerdot 8}- \frac{4}{8 \centerdot 9 \centerdot 10}+ \cdots \\&= 3+ \frac{1}{1 \centerdot 3 \centerdot 2}- \frac{1}{2 \centerdot 5 \centerdot 3}+ \frac{1}{3 \centerdot 7 \centerdot 4}- \frac{1}{4 \centerdot 9 \centerdot 5}+ \cdots \\&= 3+ \sum_{n=2}^{\infty} \frac{(-1)^n}{(n-1)n(2n-1)} \end{aligned} π=3+23444564+678489104+=3+13212531+37414951+=3+n=2(n1)n(2n1)(1)n

This is the faster convergent method for pi.
这是计算圆周率的更快一点的收敛方法。

Nilakantha series with Rational iterations for PAI

耗时(s), 迭代次数, 圆周率 π
1.9259201999999997, 4500, 3.1415926535870515825837405507560137150803433030977
3.6562770999999996, 6500, 3.1415926535888833262423877236599992905011247088849
6.4071963, 8500, 3.1415926535893862988637989428921227071088158696038
9.226604299999998, 10000, 3.1415926535895433134507693207779502215449257421397
59.0730983, 20000, 3.1415926535897619931497723041779282156055633567492
166.8244976, 30000, 3.1415926535897839801292611829194198667214211744860

Viete’s Formula

有关数学常数 π π π 的无穷级数乘积形式的韦达公式:
2 π = 2 2 ⋅ 2 + 2 2 ⋅ 2 + 2 + 2 2 ⋯ \displaystyle {\frac {2}{\pi }}={\frac {\sqrt {2}}{2}}\cdot {\frac {\sqrt {2+{\sqrt {2}}}}{2}}\cdot {\frac {\sqrt {2+{\sqrt {2+{\sqrt {2}}}}}}{2}}\cdots π2=22 22+2 22+2+2
发表于1593年,以 François Viète (1540–1603)的名字命名。

韦达公式可以用极限表示如下:
lim ⁡ n → ∞ ∏ i = 1 n a i 2 = 2 π \displaystyle \lim _{n\rightarrow \infty }\prod _{i=1}^{n}{\frac {a_{i}}{2}}={\frac {2}{\pi }} nlimi=1n2ai=π2
此处 a n = 2 + a n − 1 a_n = \sqrt {2 + a_{n − 1}} an=2+an1 , 初始值 a 1 = 2 a_1 = \sqrt{2} a1=2 .

欧拉 (Leonhard Euler) 发现正弦函数的二倍角公式:

因为 sin ⁡ x = 2 sin ⁡ x 2 cos ⁡ x 2 = ⋯ = 2 n sin ⁡ x 2 n ∏ k = 1 n cos ⁡ x 2 k {\displaystyle \sin x=2\sin {\frac {x}{2}} \cos {\frac {x}{2}}=\cdots=2^n \sin {\frac {x}{2^n}} \prod_{k=1}^{n} \cos {\frac {x}{2^k}}} sinx=2sin2xcos2x==2nsin2nxk=1ncos2kx
lim ⁡ n ← ∞ 2 n sin ⁡ x 2 n = x {\displaystyle \lim_{n\leftarrow \infty} 2^n \sin {\frac {x}{2^n}} =x} nlim2nsin2nx=x

所以 sin ⁡ x x = cos ⁡ x 2 ⋅ cos ⁡ x 4 ⋅ cos ⁡ x 8 ⋯ {\displaystyle {\frac {\sin x}{x}}=\cos {\frac {x}{2}}\cdot \cos {\frac {x}{4}}\cdot \cos {\frac {x}{8}}\cdots } xsinx=cos2xcos4xcos8x

x = π 2 {\displaystyle x={\frac {\pi }{2}}} x=2π 替换:

2 π = cos ⁡ π 4 ⋅ cos ⁡ π 8 ⋅ cos ⁡ π 16 ⋯ {\displaystyle {\frac {2}{\pi }}=\cos {\frac {\pi }{4}}\cdot \cos {\frac {\pi }{8}}\cdot \cos {\frac {\pi }{16}}\cdots} π2=cos4πcos8πcos16π

然后, 每一项用半角公式:
cos ⁡ x 2 = 1 + cos ⁡ x 2 {\displaystyle \cos {\frac {x}{2}}={\sqrt {\frac {1+\cos x}{2}}}} cos2x=21+cosx
代入后也能得到韦达公式。

It is also possible to derive from Viète’s formula a related formula for π π π that still involves nested square roots of two, but uses only one multiplication: ref Viete formula

π = lim ⁡ k → ∞ 2 k 2 − 2 + 2 + 2 + 2 + ⋯ + 2 ⏟ k   s q u a r e    r o o t s \displaystyle \pi =\lim _{k\to \infty }2^{k}\underbrace {\sqrt {2-{\sqrt {2+{\sqrt {2+{\sqrt {2+{\sqrt {2+\cdots +{\sqrt {2}}}}}}}}}}}} _{k\ \mathrm {square \;roots}} π=klim2kk squareroots 22+2+2+2++2

该算法效率最高,计算时间最短,精度也高。170次迭代报错,超出了迭代深度(RecursionError: maximum recursion depth exceeded,缺省是5000)。

反三角函数 arcsin ⁡ ( x ) \arcsin(x) arcsin(x)

1676年,牛顿给出的方法。令下列 x = 1 2 x=\dfrac{1}{2} x=21
d d x ( sin ⁡ − 1 x ) = 1 1 − x 2    ⟹    \dfrac{d}{dx}(\sin^{-1}x)=\dfrac{1}{\sqrt{1-x^2}}\implies dxd(sin1x)=1x2 1

sin ⁡ − 1 x = ∫ ( 1 − x 2 ) − 1 / 2 d x + c ( c o n s t a n t ) = ∫ ( 1 + x 2 2 + 3 x 4 8 + 5 x 6 16 + ⋯ + c ) ,    ∣ x ∣ < 1 = x + 1 2 x 3 3 + 1 ⋅ 3 2 ⋅ 4 x 5 5 + 1 ⋅ 3 ⋅ 5 2 ⋅ 4 ⋅ 6 x 7 7 + ⋯ + c \begin{aligned} \displaystyle \sin^{-1}x &=\int(1-x^2)^{-1/2}\mathrm{d}x+c(constant)\\ &=\int(1+\dfrac{x^2}{2}+\dfrac{3x^4}{8}+\dfrac{5x^6}{16}+\cdots+c),\;|x|<1\\ &=x+\dfrac{1}{2}\dfrac{x^3}{3}+\dfrac{1\cdot3}{2\cdot4}\dfrac{x^5}{5}+\dfrac{1\cdot3\cdot5}{2\cdot4\cdot6}\dfrac{x^7}{7}+\cdots+c \end{aligned} sin1x=(1x2)1/2dx+c(constant)=(1+2x2+83x4+165x6++c),x<1=x+213x3+24135x5+2461357x7++c

∵ sin ⁡ − 1 x = 0    at    x = 0    ⟹    c = 0 \because \sin^{-1}x=0\; \text{at}\; x=0\implies c=0 sin1x=0atx=0c=0

∴ sin ⁡ − 1 ( x ) = x + 1 2 x 3 3 + 1 ⋅ 3 2 ⋅ 4 x 5 5 + 1 ⋅ 3 ⋅ 5 2 ⋅ 4 ⋅ 6 x 7 7 + ⋯   ,    ∣ x ∣ < 1 = ∑ n = 0 ∞ ( 2 n ) ! 2 2 n ( n ! ) 2 ( 2 n + 1 ) x 2 n + 1 \therefore \sin^{-1}(x)=x+\dfrac{1}{2}\dfrac{x^3}{3}+\dfrac{1\cdot3}{2\cdot4}\dfrac{x^5}{5}+\dfrac{1\cdot3\cdot5}{2\cdot4\cdot6}\dfrac{x^7}{7}+\cdots,\;|x|<1\\ =\displaystyle \sum_{n=0}^{\infty}\dfrac{(2n)!}{2^{2n}(n!)^2(2n+1)}x^{2n+1} sin1(x)=x+213x3+24135x5+2461357x7+,x<1=n=022n(n!)2(2n+1)(2n)!x2n+1

同理可以得到
∴ cos ⁡ − 1 ( x ) = π 2 − ( x + 1 2 x 3 3 + 1 ⋅ 3 2 ⋅ 4 x 5 5 + 1 ⋅ 3 ⋅ 5 2 ⋅ 4 ⋅ 6 x 7 7 + ⋯   ) ,    ∣ x ∣ < 1 = π 2 − ∑ n = 0 ∞ ( 2 n ) ! 2 2 n ( n ! ) 2 ( 2 n + 1 ) x 2 n + 1 \therefore \cos^{-1}(x)=\dfrac{\pi}{2}-\left(x+\dfrac{1}{2}\dfrac{x^3}{3}+\dfrac{1\cdot3}{2\cdot4}\dfrac{x^5}{5}+\dfrac{1\cdot3\cdot5}{2\cdot4\cdot6}\dfrac{x^7}{7}+\cdots\right),\;|x|<1\\ =\displaystyle \dfrac{\pi}{2}-\sum_{n=0}^{\infty}\dfrac{(2n)!}{2^{2n}(n!)^2(2n+1)}x^{2n+1} cos1(x)=2π(x+213x3+24135x5+2461357x7+),x<1=2πn=022n(n!)2(2n+1)(2n)!x2n+1

Stirling’s Approximation for n!

ln ⁡ n ! = ln ⁡ 1 + ln ⁡ 2 + . . . + ln ⁡ n = ∑ k = 1 n ln ⁡ k = ∫ 1 n ln ⁡ x d x = ( x ln ⁡ x − x ) ∣ 1 n = n ln ⁡ n − n + 1 ≈ n ln ⁡ n − n \begin{aligned} \ln n! &=\ln1+\ln2+...+\ln n\\ &= \sum_{k=1}^{n} \ln k\\ &= \int_1^n \ln x \mathbf{d}x \\ &= (x \ln x-x)|_1^n\\ &= n \ln n-n+1\\ &\approx n \ln n-n \end{aligned} lnn!=ln1+ln2+...+lnn=k=1nlnk=1nlnxdx=(xlnxx)1n=nlnnn+1nlnnn

所以 对于很大的 n n n,Stirling斯特林逼近公式为
ln ⁡ n ! ≈ n ln ⁡ n − n \boxed{\ln n! \approx n\ln n-n} lnn!nlnnn n ! ≈ n n e − n 2 π n \boxed{n!\approx n^n e^{-n} \sqrt{2\pi n}} n!nnen2πn

更精确的有:
2 π n ( n e ) n ≤ n ! ≤ 2 π n ( n e ) n e 1 12 n \sqrt{2\pi n}\left( \dfrac{n}{e}\right)^n \le n! \le \sqrt{2\pi n}\left(\dfrac{n}{e}\right)^n e^{\frac{1}{12n}} 2πn (en)nn!2πn (en)ne12n1

Taylor级数和Maclaurin Series的实现

在GeoGebra中可以轻松实现多项式之和逼近各种函数。

  1. 先定义次数Order的滑条 n = s l i d e r ( 1 , 20 , 1 ) n=slider(1,20,1) n=slider(1,20,1)
  2. 再定义要逼近的函数,如 f ( x ) = arctan ⁡ ( x ) , g ( x ) = sin ⁡ ( x ) , h ( x ) = cos ⁡ ( x ) , ⋯ f(x)=\arctan(x),g(x)=\sin(x),h(x)=\cos(x),\cdots f(x)=arctan(x),g(x)=sin(x),h(x)=cos(x),
  3. 调用函数 g = T a y l o r P o l i n o m i a l ( f , x ( A ) , n ) ,    A = ( 0 , 0 ) g=TaylorPolinomial(f,x(A),n),\;A=(0,0) g=TaylorPolinomial(f,x(A),n),A=(0,0)
  4. 调用函数 F o r m u l a T e x t ( g , t r u e , t r u e ) FormulaText(g, true, true) FormulaText(g,true,true) 可以显示级数

TaylorSeries的多项式逼近演示GGB

计算 π \pi π 收敛速度极快算法

代数和几何结合的方法。

采用刘徽割圆术,用正 n n n 边形逼近圆的方法,实现计算圆周率 π \pi π 的目的。

记 正 n n n 边形的边长为 L n L_n Ln, 由正 n n n 边形产生的正 2 n 2n 2n 边形的边长为 L 2 n L_{2n} L2n, 则如图可有
刘徽割圆术
L n = A A ′ , L 2 n = A B L 2 n 2 = ( L n 2 ) 2 + ( 1 − O C ) 2 = 1 4 L n 2 + ( 1 − 1 − 1 4 L n 2 ) 2 = 2 − 2 1 − 1 4 L n 2 L 2 n = 2 − 4 − L n 2 L_n=AA',L_{2n}=AB\\ L_{2n}^2=(\dfrac{L_n}{2})^2+(1-OC)^2\\ =\dfrac{1}{4}L_n^2+\left(1-\sqrt{1-\dfrac{1}{4}L_n^2}\right)^2\\ =2-2\sqrt{1-\dfrac{1}{4}L_n^2}\\ L_{2n}=\sqrt{2-\sqrt{4-L_n^2}} Ln=AA,L2n=ABL2n2=(2Ln)2+(1OC)2=41Ln2+(1141Ln2 )2=22141Ln2 L2n=24Ln2

单位圆的半径 r = 1 r=1 r=1, 则有正 6 6 6 边形的边长 L 6 = 1 L_6=1 L6=1, 利用迭代关系式可以快速求出 周长
C O = 2 π = lim ⁡ n → ∞ ( n × L n ) π = 1 2 × lim ⁡ n → ∞ ( n × L n ) \displaystyle C_{O}=2\pi=\lim_{n\to \infin} (n\times L_{n})\\ \pi=\dfrac{1}{2}\times \lim_{n\to \infin} (n \times L_{n}) CO=2π=nlim(n×Ln)π=21×nlim(n×Ln)

即圆周率就是半周长,上述算法收敛性很快!

在GeoGebra中实现

  1. 先建立迭代次数滑动条 n = s l i d e r ( 1 , 10 , 1 ) n=slider(1,10,1) n=slider(1,10,1)
  2. 再建立迭代函数 f ( x ) = s q r t ( 2 − s q r t ( 4 − x    x ) ) f(x)=sqrt(2 - sqrt(4 - x\; x)) f(x)=sqrt(2sqrt(4xx))
  3. 然后用GGB的迭代命令 v a l u e = I t e r a t i o n ( f , 1 , n ) → value = Iteration(f, 1, n)\to value=Iteration(f,1,n) L 2 n = 2 − 4 − L n × L n , L 6 = 1 L_{2n}=\sqrt{2-\sqrt{4-L_n\times L_n}},L_6=1 L2n=24Ln×Ln ,L6=1,初始值取正6边形时的值1.
  4. 2 n 2n 2n 边形的半周长 π = v a l u e × 6 × 2 n − 1 \pi=value\times6\times2^{n-1} π=value×6×2n1

这一算法的收敛也极快,正多边形的边数以指数级增长。边数 = 6 × 2 n − 1 =6\times2^{n-1} =6×2n1

Python程序得到的结果

Python 源码 参见迭代计算圆周率π的Python源码

采用符号运算得到前10个结论:

12sqrt(2 - sqrt(3))
24
sqrt(2 - sqrt(sqrt(3) + 2))
48sqrt(2 - sqrt(sqrt(sqrt(3) + 2) + 2))
96
sqrt(2 - sqrt(sqrt(sqrt(sqrt(3) + 2) + 2) + 2))
192sqrt(2 - sqrt(sqrt(sqrt(sqrt(sqrt(3) + 2) + 2) + 2) + 2))
384
sqrt(2 - sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(3) + 2) + 2) + 2) + 2) + 2))
768sqrt(2 - sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(3) + 2) + 2) + 2) + 2) + 2) + 2))
1536
sqrt(2 - sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(3) + 2) + 2) + 2) + 2) + 2) + 2) + 2))
3072sqrt(2 - sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(3) + 2) + 2) + 2) + 2) + 2) + 2) + 2) + 2))
6144
sqrt(2 - sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(sqrt(3) + 2) + 2) + 2) + 2) + 2) + 2) + 2) + 2) + 2))

第151次 到 第159次的迭代结果:

3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825324499856
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825337712765
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825341015992
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825341841799
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342048251
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342099864
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342112767
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342115993
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342116799

第 160 次迭代结果,准确到小数点后 第96位:

3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117001

圆周率小数点后100位

14 15 92 65 35 89 79 32 38 46 26 43 38 32 79 50 28 84 19 71 69 39 93 75 10 58 20 97 49 44 59 23 07 81 64 06 28 62 08 99 86 28 03 48 25 34 21 17 06 79 。

圆周率 π \pi π 生成器

在网络上搜索到一个圆周率生成器,只要输入一个数:生成位数,就可以算出该圆周率。没有验证正确否。供大家参考。

圆周率 π \pi π生成器

在Python环境下,只要调用 sympy.pi.evalf(n+1) 就可以得到任意 n n n 位小数的 圆周率 π \pi π.

from sympy import pi

pi.evalf(1001)  # 得到1000位小数位的π

我输入了 1000 位数,计算结果如下:

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679 821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201988

有关迭代次数超限问题

在Python中,迭代次数超过 1 0 4 10^4 104 时,会报错 RecursionError: maximum recursion depth exceeded
改进办法:

import sys
 
# the setrecursionlimit function is
# used to modify the default recursion
# limit set by python. Using this,
# we can increase the recursion limit
# to satisfy our needs
 
sys.setrecursionlimit(10**6)
  • 8
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值