跳到正文

学习·文化课

泰勒展开求三角函数

CXQianyiXu约 17 分钟
原文
文章信息与版本

文章信息

发布
2024年12月22日
收录
2025年12月12日
更新
2026年8月20日
篇幅
约 17 分钟
获赞
2

版本

  1. v1当前

观察下图

由上图可得

sin⁡x=x−x33!+x55!−x77!+x99!− ⁣⋯=∑i=0∞x4i+1(4i+1)!−x4i+3(4i+3)!\begin {align*} \sin x &= x - \frac {x ^ 3} {3!} + \frac {x ^ 5} {5!} - \frac {x ^ 7} {7!} + \frac {x ^ 9} {9!} - \dotsi \\ &= \sum _ {i = 0} ^ {\infty} \frac {x ^ {4i + 1}} {(4i + 1)!} - \frac {x ^ {4i + 3}} {(4i + 3)!} \end {align*}

正文

从多项式开始

把 sin⁡x\sin x 展开为多项式,先取前五项。

sin⁡x=c0+c1x+c2x2+c3x3+c4x4 \sin x = c _ 0 + c _ 1 x + c _ 2 x ^ 2 + c _ 3 x ^ 3 + c _ 4 x ^ 4

我们从 x=0x = 0 开始逼近 sin⁡x\sin x,那么因为 sin⁡0=0\sin 0 = 0,所以我们要让多项式的值在 x=0x = 0 时为 00,即 c0=0c _ 0 = 0。


sin⁡x=c1x+c2x2+c3x3+c4x4 \sin x = c _ 1 x + c _ 2 x ^ 2 + c _ 3 x ^ 3 + c _ 4 x ^ 4

同时,我们也需要让多项式的斜率(倾斜程度,具体见下一节)在 x=0x = 0 时与 sin⁡x\sin x 相等,于是我们对 sin⁡x\sin x 进行求导,即 (sin⁡x)′=cos⁡x(\sin x)' = \cos x。当 x=0x = 0 时,cos⁡x=1\cos x = 1。 我们又对多项式进行求导,得 (c1x+c2x2+c3x3+c4x4)′=c1+2c2x+3c3x2+4c4x3(c _ 1 x + c _ 2 x ^ 2 + c _ 3 x ^ 3 + c _ 4 x ^ 4)' = c _ 1 + 2c _ 2 x + 3c _ 3 x ^ 2 + 4c _ 4 x ^ 3。当 x=0x = 0 时,c1+2c2x+3c3x2+4c4x3=c1c _ 1 + 2c _ 2 x + 3c _ 3 x ^ 2 + 4c _ 4 x ^ 3 = c _ 1。 所以 c1=1c _ 1 = 1。


sin⁡x=x+c2x2+c3x3+c4x4 \sin x = x + c _ 2 x ^ 2 + c _ 3 x ^ 3 + c _ 4 x ^ 4

同时,为了在 x=0x = 0 处附近的误差不太大,要让多项式斜率的变化程度与 sin⁡x\sin x 在 x=0x = 0 处也相等,于是对其进行二阶求导。 当 x=0x = 0 时,

(sin⁡x)′′=−sin⁡x=0(\sin x)'' = -\sin x = 0 (x+c2x2+c3x3+c4x4)′′=2c2+6c3x+12c4x2=2c2(x + c _ 2 x ^ 2 + c _ 3 x ^ 3 + c _ 4 x ^ 4)'' = 2c _ 2 + 6c _ 3 x + 12c _ 4 x ^ 2 = 2c _ 2

所以 2c2=02c _ 2 = 0,即 c2=0c _ 2 = 0。


sin⁡x=x+c3x3+c4x4 \sin x = x + c _ 3 x ^ 3 + c _ 4 x ^ 4

继续如法炮制。 当 x=0x = 0 时,

(sin⁡x)′′′=−cos⁡x=−1(\sin x)''' = -\cos x = -1 (x+c3x3+c4x4)′′′=6c3+24c4x=6c3(x + c _ 3 x ^ 3 + c _ 4 x ^ 4)''' = 6c _ 3 + 24c _ 4 x = 6c _ 3

所以 6c3=−16c _ 3 = -1,即 c3=−16c _ 3 = -\displaystyle \frac 1 6。


sin⁡x=x−x36+c4x4 \sin x = x - \frac {x ^ 3} 6 + c _ 4 x ^ 4

当 x=0x = 0 时,

(sin⁡x)(4)=sin⁡x=0(\sin x) ^ {(4)} = \sin x = 0 (x−x36+c4x4)(4)=24c4(x - \frac {x ^ 3} 6 + c _ 4 x ^ 4) ^ {(4)} = 24c _ 4

所以 24c4=024c _ 4 = 0,即 c4=0c _ 4 = 0。


还可以增加 c5c _ 5,c6c _ 6…… 而对 sin⁡\sin 不断求导会陷入循环。

导函数阶数 n mod 4n \bmod 4sin⁡(n)\sin ^ {(n)}
0sin⁡\sin
1cos⁡\cos
2−sin⁡-\sin
3−cos⁡-\cos

所以每一项是正负交替的,且项与项之间的次数差 22。 那又为何是阶乘呢?


求导的方法

导函数 是函数值的变化情况。定义如下:

如果函数 f⁡(x)\operatorname f(x) 在 (a,b)(a, b) 中每一点处都可导,则称 f⁡(x)\operatorname f(x) 在 (a,b)(a,b) 上可导,则可建立 f⁡(x)\operatorname f(x) 的导函数,简称导数,记为 f⁡′(x)\operatorname f'(x)。[1]

如果 f⁡(x)\operatorname f(x) 在 (a,b)(a,b) 内可导,且在区间端点 aa 处的右导数和端点 bb 处的左导数都存在,则称 f⁡(x)\operatorname f(x) 在闭区间 [a,b][a,b] 上可导,f⁡′(x)\operatorname f'(x) 为区间 [a,b][a,b] 上的导函数,简称导数。[2]

乍一眼看上去它十分复杂 (其实我也没读懂),所以我在这里给出一个更加简单的 XXS 都能明白的解释。

在六年级我们学过正比例函数。正比例函数可以用 f⁡(a)−f⁡(b)a−b\displaystyle \frac {\operatorname f(a) - \operatorname f(b)} {a - b} 来求斜率,斜率越大,这个函数的图像表示就越斜。

例如 f⁡(x)=32x\operatorname f(x) = \displaystyle \frac 3 2 x 这个函数。假设 a=1,b=0a = 1, b = 0,该函数的斜率为 f⁡(1)−f⁡(0)1−0=1.51=32\displaystyle \frac {\operatorname f(1) - \operatorname f(0)} {1 - 0} = \displaystyle \frac {1.5} 1 = \displaystyle \frac 3 2,即 f⁡′(x)=32\operatorname f'(x) = \displaystyle \frac 3 2。

因为正比例函数是线性的,所以每处的斜率都相等,那遇到更加复杂的函数怎么办呢?

例如 y=12x2−3x+1y = \displaystyle \frac 1 2 x ^ 2 - 3x + 1,它的图像如下: 很容易得出在 x=3x = 3 处(切线的)斜率为 00。 那在 x=0x = 0 处呢?其他地方呢? 如何求图中虚线(x=0x = 0 处的切线)的斜率?

取一点 (x,f⁡(x))=(0,1)(x, \operatorname f(x)) = (0, 1),再取一点坐标为 (x′,f⁡(x′))(x', \operatorname f(x'))。我们可以发现,x′x' 越接近 xx,所得越来越接近真实值。(如下图) 于是我们记 x′−xx' - x 为 Δx\Delta x,则 Δx→0\Delta x \to 0 时可取得真实值。

由此可得求导的一般方法:

f⁡′(x)=lim⁡Δx→0f⁡(x+Δx)−f⁡(x)Δx\operatorname f'(x) = \lim \limits _ {\Delta x \to 0} \frac {\operatorname f(x + \Delta x) - \operatorname f(x)} {\Delta x}

把这个公式应用到 y=12x2−3x+1y = \displaystyle \frac 1 2 x ^ 2 - 3x + 1 中,则

y′=lim⁡Δx→012(x+Δx)2−3(x+Δx)+1−(12x2−3x+1)Δx=lim⁡Δx→012x2+xΔx+12Δx2−3x−3Δx+1−12x2+3x−1)Δx=lim⁡Δx→0xΔx+12Δx2−3ΔxΔx=lim⁡Δx→0x+12Δx−3=x−3\begin {align*} y' &= \lim \limits _ {\Delta x \to 0} \frac {\displaystyle \frac 1 2 (x + \Delta x) ^ 2 - 3 (x + \Delta x) + 1 - (\frac 1 2 x ^ 2 - 3x + 1)} {\Delta x} \\ &= \lim \limits _ {\Delta x \to 0} \frac {\displaystyle \frac 1 2 x ^ 2 + x \Delta x + \frac 1 2 \Delta x ^ 2 - 3x - 3 \Delta x + 1 - \frac 1 2 x ^ 2 + 3x - 1)} {\Delta x} \\ &= \lim \limits _ {\Delta x \to 0} \frac {x \Delta x + \frac 1 2 \Delta x ^ 2 - 3 \Delta x} {\Delta x} \\ &= \lim \limits _ {\Delta x \to 0} x + \frac 1 2 \Delta x - 3 = x - 3 \end {align*}

所以当 x=0x = 0 时斜率为 −3-3。


代码示例

Python
from typing import Union
from math import pi, sqrt


# angle:以弧度为单位,decimal_digits:精确的小数位数,返回值为(cos, sin)
def taylor(angle: Union[int, float], decimal_digits: int = 6):
    angle %= 2 * pi
    res = 0
    mul = 1
    num = 1
    while True:
        nxt = res + angle ** num / mul
        mul *= (num + 1) * (num + 2)
        num += 2
        nxt -= angle ** num / mul
        mul *= (num + 1) * (num + 2)
        num += 2
        if (round(nxt, decimal_digits + 1) == round(res, decimal_digits + 1)):
            res = nxt
            break
        res = nxt
    sin_ans = round(res, decimal_digits)
    cos_ans = (1 - res ** 2) ** 0.5
    tan_ans = round(res / (1 - res ** 2) ** 0.5, decimal_digits) if cos_ans != 0 else float('inf')
    cos_ans = round(cos_ans, decimal_digits)
    if cos_ans == -0.0:
        cos_ans = 0.0
    if sin_ans == -0.0:
        sin_ans = 0.0
    return cos_ans, sin_ans, tan_ans

n = float(input())
n = n / 180 * pi
x, y, tangent = taylor(n)
print(x, y, tangent)

遗留问题

现解决两个遗留问题:多项式和三角函数的导函数怎么求?

设 f1⁡(x)=axb\operatorname {f _ 1}(x) = ax ^ b。求 f1⁡′(x)\operatorname {f _ 1}'(x)。 代入公式,则

f1⁡′(x)=lim⁡Δx→0a(x+Δx)b−axbΔx=lim⁡Δx→0∑i=0baCbixb−iΔxi−axbΔx=lim⁡Δx→0∑i=1baCbixb−iΔxi−1=aCb1xb−1=abxb−1\begin {align*} \operatorname {f _ 1}'(x) &= \lim \limits _ {\Delta x \to 0} \frac {a (x + \Delta x) ^ b - ax ^ b} {\Delta x} \\ &= \lim \limits _ {\Delta x \to 0} \frac {\displaystyle \sum _ {i = 0} ^ b aC _ b ^ i x ^ {b - i} \Delta x ^ i - ax ^ b} {\Delta x} \\ &= \lim \limits _ {\Delta x \to 0} \sum _ {i = 1} ^ b aC _ b ^ i x ^ {b - i} \Delta x ^ {i - 1} \\ &= aC _ b ^ 1 x ^ {b - 1} = abx ^ {b - 1} \end {align*}

即单项式的求导方法是先乘指数再降幂。

又设 f2⁡(x,y)=axb+myn\operatorname {f _ 2}(x, y) = ax ^ b + my ^ n。求 f2⁡′(x,y)\operatorname {f _ 2}'(x, y)。 代入公式,则

f2⁡′(x,y)=lim⁡Δx→0a(x+Δx)b+m(y+Δx)n−axb−mynΔx=lim⁡Δx→0a(x+Δx)b−axbΔx+lim⁡Δx→0m(y+Δx)n−mynΔx=abxb−1+mnyn−1\begin {align*} \operatorname {f _ 2}'(x, y) &= \lim \limits _ {\Delta x \to 0} \frac {a (x + \Delta x) ^ b + m (y + \Delta x) ^ n - ax ^ b - my ^ n} {\Delta x} \\ &= \lim \limits _ {\Delta x \to 0} \frac {a (x + \Delta x) ^ b - ax ^ b} {\Delta x} + \lim \limits _ {\Delta x \to 0} \frac {m (y + \Delta x) ^ n - my ^ n} {\Delta x} \\ &= abx ^ {b - 1} + mny ^ {n - 1} \end {align*}

即多项式的求导方法是先逐个击破,再相加。

再设 f3⁡(x)=sin⁡x\operatorname {f _ 3}(x) = \sin x,则

f3⁡′(x)=lim⁡Δx→0sin⁡(x+Δx)−sin⁡xΔx=lim⁡Δx→0sin⁡xcos⁡Δx+cos⁡xsin⁡Δx−sin⁡xΔx=sin⁡xlim⁡Δx→0cos⁡Δx−1Δx+cos⁡xlim⁡Δx→0sin⁡ΔxΔx=cos⁡x\begin {align*} \operatorname {f _ 3}'(x) &= \lim \limits _ {\Delta x \to 0} \frac {\sin (x + \Delta x) - \sin x} {\Delta x} \\ &= \lim \limits _ {\Delta x \to 0} \frac {\sin x \cos \Delta x + \cos x \sin \Delta x - \sin x} {\Delta x} \\ &= \sin x \lim \limits _ {\Delta x \to 0} \frac {\cos \Delta x - 1} {\Delta x} + \cos x \lim \limits _ {\Delta x \to 0} \frac {\sin \Delta x} {\Delta x} \\ &= \cos x \end {align*}

同理可得 (cos⁡x)′=−sin⁡x(\cos x)' = -\sin x,(−sinx)′=−cos⁡x(-sin x)' = -\cos x,(−cos⁡x)′=sin⁡x(-\cos x)' = \sin x。


两个重要极限的证明

为什么 lim⁡x→0sin⁡xx=1\lim \limits _ {x \to 0} \displaystyle \frac {\sin x} x = 1,lim⁡x→01−cos⁡xx=0\lim \limits _ {x \to 0} \displaystyle \frac {1 - \cos x} x = 0?

证明 lim⁡x→0sin⁡xx=1\lim \limits _ {x \to 0} \displaystyle \frac {\sin x} x = 1 过程

由图 x=∠BADx = \angle BAD,sin⁡x=EB\sin x = EB,cos⁡x=AE\cos x = AE,tan⁡x=DF\tan x = DF。 于是

S△ABD<SABD⏠<S△AFD(x∈(0,π2))sin⁡x2<12π⋅x2π<tan⁡x2sin⁡x<x<tan⁡x1<xsin⁡x<1cos⁡x\begin {align*} S _ {\triangle ABD} &< S _ {\overgroup {ABD}} < S _ {\triangle AFD} \qquad (x \in (0, \frac \pi 2)) \\ \frac {\sin x} 2 &< 1 ^ 2 \pi \cdot \frac x {2 \pi} < \frac {\tan x} 2 \\ \sin x &< x < \tan x \\ 1 &< \frac x {\sin x} < \frac 1 {\cos x} \end {align*}

因为 lim⁡x→01cos⁡x=1\lim \limits _ {x \to 0} \displaystyle \frac 1 {\cos x} = 1,所以

lim⁡x→0xsin⁡x=1\lim \limits _ {x \to 0} \frac x {\sin x} = 1

证明 lim⁡x→01−cos⁡xx=0\lim \limits _ {x \to 0} \displaystyle \frac {1 - \cos x} x = 0 过程

由和角公式可得出

sin⁡(2α)=2sin⁡αcos⁡α\sin (2 \alpha) = 2 \sin \alpha \cos \alpha cos⁡(2α)=cos⁡2α−sin⁡2α=1−2sin⁡2α\cos (2 \alpha) = \cos ^ 2 \alpha - \sin ^ 2 \alpha = 1 - 2 \sin ^ 2 \alpha

所以

1−cos⁡x=1−(1−2sin⁡2x2)=2sin⁡2x2lim⁡x→01−cos⁡xx=lim⁡x→02sin⁡2x2x=lim⁡x→02(x2)2x=lim⁡x→0x2=0\begin {align*} 1 - \cos x &= 1 - (1 - 2 \sin ^ 2 \frac x 2) \\ &= 2 \sin ^ 2 \frac x 2 \\ \lim \limits _ {x \to 0} \frac {1 - \cos x} x &= \lim \limits _ {x \to 0} \frac {2 \sin ^ 2 \displaystyle \frac x 2} x \\ &= \lim \limits _ {x \to 0} \frac {2 (\displaystyle \frac x 2) ^ 2} x \\ &= \lim \limits _ {x \to 0} \frac x 2 = 0 \end {align*}

完结撒花!!!

原文