泰勒展开求三角函数

· · 学习·文化课

观察下图

由上图可得

\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 = c _ 0 + c _ 1 x + c _ 2 x ^ 2 + c _ 3 x ^ 3 + c _ 4 x ^ 4

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

\sin x = c _ 1 x + c _ 2 x ^ 2 + c _ 3 x ^ 3 + c _ 4 x ^ 4

同时,我们也需要让多项式的斜率(倾斜程度,具体见下一节)在 x = 0 时与 \sin x 相等,于是我们对 \sin x 进行求导,即 (\sin x)' = \cos x。当 x = 0 时,\cos x = 1
我们又对多项式进行求导,得 (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 = 0 时,c _ 1 + 2c _ 2 x + 3c _ 3 x ^ 2 + 4c _ 4 x ^ 3 = c _ 1
所以 c _ 1 = 1

\sin x = x + c _ 2 x ^ 2 + c _ 3 x ^ 3 + c _ 4 x ^ 4

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

(\sin x)'' = -\sin x = 0 (x + c _ 2 x ^ 2 + c _ 3 x ^ 3 + c _ 4 x ^ 4)'' = 2c _ 2 + 6c _ 3 x + 12c _ 4 x ^ 2 = 2c _ 2

所以 2c _ 2 = 0,即 c _ 2 = 0

\sin x = x + c _ 3 x ^ 3 + c _ 4 x ^ 4

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

(\sin x)''' = -\cos x = -1 (x + c _ 3 x ^ 3 + c _ 4 x ^ 4)''' = 6c _ 3 + 24c _ 4 x = 6c _ 3

所以 6c _ 3 = -1,即 c _ 3 = -\displaystyle \frac 1 6

\sin x = x - \frac {x ^ 3} 6 + c _ 4 x ^ 4

x = 0 时,

(\sin x) ^ {(4)} = \sin x = 0 (x - \frac {x ^ 3} 6 + c _ 4 x ^ 4) ^ {(4)} = 24c _ 4

所以 24c _ 4 = 0,即 c _ 4 = 0

还可以增加 c _ 5c _ 6…… 而对 \sin 不断求导会陷入循环。 导函数阶数 n \bmod 4 \sin ^ {(n)}
0 \sin
1 \cos
2 -\sin
3 -\cos

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

求导的方法

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

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

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

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

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

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

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

例如 y = \displaystyle \frac 1 2 x ^ 2 - 3x + 1,它的图像如下:

很容易得出在 x = 3 处(切线的)斜率为 0。 那在 x = 0 处呢?其他地方呢? 如何求图中虚线(x = 0 处的切线)的斜率?

取一点 (x, \operatorname f(x)) = (0, 1),再取一点坐标为 (x', \operatorname f(x'))。我们可以发现,x' 越接近 x,所得越来越接近真实值。(如下图)

于是我们记 x' - x\Delta x,则 \Delta x \to 0 时可取得真实值。

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

\operatorname f'(x) = \lim \limits _ {\Delta x \to 0} \frac {\operatorname f(x + \Delta x) - \operatorname f(x)} {\Delta x}

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

\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 = 0 时斜率为 -3

代码示例

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)

遗留问题

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

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

\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*}

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

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

\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*}

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

再设 \operatorname {f _ 3}(x) = \sin 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(-sin x)' = -\cos x(-\cos x)' = \sin x

两个重要极限的证明

为什么 \lim \limits _ {x \to 0} \displaystyle \frac {\sin x} x = 1\lim \limits _ {x \to 0} \displaystyle \frac {1 - \cos x} x = 0

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

由图 x = \angle BAD\sin x = EB\cos x = AE\tan x = DF

于是

\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 \limits _ {x \to 0} \displaystyle \frac 1 {\cos x} = 1,所以

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

证明 \lim \limits _ {x \to 0} \displaystyle \frac {1 - \cos x} x = 0 过程

由和角公式可得出

\sin (2 \alpha) = 2 \sin \alpha \cos \alpha \cos (2 \alpha) = \cos ^ 2 \alpha - \sin ^ 2 \alpha = 1 - 2 \sin ^ 2 \alpha

所以

\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*}

完结撒花!!!