18 / 90

第 18 天:有限差分、步長與浮點效應

透過定義、分步推導、檢索練習與誠實的數值證據,讓函數與變化變得可計算。

數學與微積分 480 分鐘主動學習(8 小時專注學習)
01 120 分鐘

有限差分是近似

Agent 輔助工程檢查

前提條件

完成前一項活動;如遇困難,使用列出的恢復來源。

學習目標

由導數定義實現前向、後向和中央有限差分,保持 h 明確可見,並與解析答案比較,而不是把近似當作精確值。

操作說明

1. 代入數字之前,先用符號 h 寫出三種格式:前向 [f(a+h) - f(a)]/h,後向 [f(a) - f(a-h)]/h,中央 [f(a+h) - f(a-h)]/(2h)。保持 h 的符號可見。
2. 利用 Day 17 的結論(x^2 在 3 處的前向差商恰為 6 + h),預測哪種格式最接近 f'(3) = 6。
3. 只計算一次 f(3.1)、f(3) 和 f(2.9),然後在三種格式中重複使用這三個值。
4. 列表寫出每個估計值及其帶符號的誤差(估計值 - 6)。
5. 用泰勒展開解釋規律:對 x^2,前向誤差恰為 +h,後向誤差恰為 -h,二者在中央格式中相互抵消;下一項含 f''',而二次函數的 f''' 為零。
6. 說明哪些量是精確的(f'(3)、函數值),哪些是近似(一般情況下格式的輸出),並解釋為什麼這裡中央差分精確只是特例,而不是規律。

時間安排(120 分鐘):閱讀 30 + 練習 55 + 檢查 35。 閱讀:OpenStax 第 3.1 節(作為差商極限的導數)和 SciPy derivative 頁面概述。練習:三種格式、誤差表和泰勒解釋。檢查:獨立完成 x=2、h=.01 的精熟題。

練習與預測紀錄

對 x=3 處的 f(x)=x^2,取 h=.1,計算前向 [f(3+h)-f(3)]/h、後向 [f(3)-f(3-h)]/h、中央 [f(3+h)-f(3-h)]/(2h)。預測哪個最接近 f'(3)=6。

獨立掌握標準

獨立計算 f(x)=x^2 在 x=2、h=.01 處的三個估計值,與 4 比較,並說明哪些量是精確的、哪些是近似的。

權威來源資源與出處

linking CC BY-NC-SA 4.0 attribution noncommercial share-alike

OpenStax Calculus Volume 1

作者 / 機構: OpenStax, Rice University (Gilbert Strang, Edwin Herman, et al.) | 原始出處連結 ↗

對應章節 / 時間戳: Sections 2.1-2.3 (Functions, Limits, and the Derivative); Sections 3.1-3.2 (Derivative Rules)

驗證版本與日期: 3rd edition, verified 2026-10-30

入選理由:

Open, systematic treatment of the limit definition and derivative rules with stable section locations.

來源前置條件:

Algebra, coordinate geometry, and function notation

安全回退方案(離線彈性):

The authored derivations, tables, and exercises in these three Study Days are complete offline recovery material; use the linked section only for optional comparison.

對比候選資源與選型分析 (3)
MIT OpenCourseWare 18.01SC Single Variable Calculus ↗ compared_selected_for_visual_practice [CC BY-NC-SA 4.0]

Strong exercises and transcripts, but the noncommercial share-alike terms and course navigation make OpenStax the primary stable text.

Paul's Online Math Notes: Calculus I ↗ compared_rejected_for_reuse [All Rights Reserved]

Useful reference, but rights and redistribution terms are less suitable for embedded recovery material.

SciPy Numerical Differentiation Documentation ↗ compared_selected_for_numerics [BSD-3-Clause]

A primary API reference for numerical derivative step-size behavior; it complements rather than replaces the hand derivation.

linking BSD-3-Clause

SciPy numerical derivative reference

作者 / 機構: SciPy Developers | 原始出處連結 ↗

對應章節 / 時間戳: derivative API: finite differences, step-size iteration, tolerances

驗證版本與日期: SciPy documentation, verified 2026-10-30

入選理由:

Documents numerical differentiation as an approximation with finite step sizes and stopping criteria.

來源前置條件:

Python arithmetic and the definition of a derivative

安全回退方案(離線彈性):

The local Day 18 tables and pure-Python snippets reproduce the relevant finite-difference behavior without SciPy.

對比候選資源與選型分析 (2)
NumPy: numpy.gradient ↗ compared_alternative [BSD 3-Clause License]

Array-oriented gradient estimation on sampled grids; the SciPy derivative reference matches the pointwise finite-difference helper the exercises implement.

SymPy Calculus tutorial ↗ compared_alternative [BSD License]

Symbolic rather than numerical differentiation; useful for exact cross-checks but not the finite-difference practice target.

02 120 分鐘

步長:截斷誤差與捨入誤差

獨立掌握檢查(無 AI 輔助)

前提條件

完成前一項活動;如遇困難,使用列出的恢復來源。

學習目標

解釋為什麼最小的步長並不總是最好:截斷誤差通常隨 h 減小而減小,而當鄰近函數值變得無法區分時,浮點相消與捨入卻會增大。

操作說明

1. 計算前先預測:中央差分的截斷誤差大約按 h^2 減小,捨入誤差大約按 eps/h 增大(雙精度下 eps ≈ 2.2e-16)。寫下你預計二者交叉的位置。
2. 對 h = .1, 1e-3, 1e-5, 1e-8, 1e-12 計算中央估計 (sin(1+h) - sin(1-h))/(2h),至少顯示 12 位有效數字。
3. 每個絕對誤差都對照工作精度的 math.cos(1) 計算,而不是對照 10 位顯示值 0.5403023059。
4. 標出誤差不再減小、開始增大的那一行(轉折點)。
5. 用兩句話解釋轉折點:一句講截斷誤差,一句講相消與捨入。
6. 明確說明:這張表只展示了一個函數、一個點、一種精度,並不給出普遍適用的最佳 h。

時間安排(120 分鐘):閱讀 25 + 練習 60 + 檢查 35。 閱讀:SciPy derivative 頁面(步長、容差)和活動 1 的筆記。練習:含誤差和轉折點的五行表。檢查:獨立重現三行並解釋為什麼 1e-12 比 1e-5 差的精熟題。

練習與預測紀錄

對 sin(1) 使用中央差分(其解析導數為 cos(1)≈0.5403023059(10 位十進位顯示;誤差應對照工作精度的 cos(1) 計算))。比較 h=.1, 1e-3, 1e-5, 1e-8 和 1e-12。解釋最佳區間,而不是習慣性地選最小的 h。

獨立掌握標準

無協助地用計算器或 Python 重現至少三列表值,計算相對 cos(1) 的絕對誤差,並解釋此處為什麼 h=1e-12 比 h=1e-5 更差。

權威來源資源與出處

linking CC BY-NC-SA 4.0 attribution noncommercial share-alike

OpenStax Calculus Volume 1

作者 / 機構: OpenStax, Rice University (Gilbert Strang, Edwin Herman, et al.) | 原始出處連結 ↗

對應章節 / 時間戳: Sections 2.1-2.3 (Functions, Limits, and the Derivative); Sections 3.1-3.2 (Derivative Rules)

驗證版本與日期: 3rd edition, verified 2026-10-30

入選理由:

Open, systematic treatment of the limit definition and derivative rules with stable section locations.

來源前置條件:

Algebra, coordinate geometry, and function notation

安全回退方案(離線彈性):

The authored derivations, tables, and exercises in these three Study Days are complete offline recovery material; use the linked section only for optional comparison.

對比候選資源與選型分析 (3)
MIT OpenCourseWare 18.01SC Single Variable Calculus ↗ compared_selected_for_visual_practice [CC BY-NC-SA 4.0]

Strong exercises and transcripts, but the noncommercial share-alike terms and course navigation make OpenStax the primary stable text.

Paul's Online Math Notes: Calculus I ↗ compared_rejected_for_reuse [All Rights Reserved]

Useful reference, but rights and redistribution terms are less suitable for embedded recovery material.

SciPy Numerical Differentiation Documentation ↗ compared_selected_for_numerics [BSD-3-Clause]

A primary API reference for numerical derivative step-size behavior; it complements rather than replaces the hand derivation.

linking BSD-3-Clause

SciPy numerical derivative reference

作者 / 機構: SciPy Developers | 原始出處連結 ↗

對應章節 / 時間戳: derivative API: finite differences, step-size iteration, tolerances

驗證版本與日期: SciPy documentation, verified 2026-10-30

入選理由:

Documents numerical differentiation as an approximation with finite step sizes and stopping criteria.

來源前置條件:

Python arithmetic and the definition of a derivative

安全回退方案(離線彈性):

The local Day 18 tables and pure-Python snippets reproduce the relevant finite-difference behavior without SciPy.

對比候選資源與選型分析 (2)
NumPy: numpy.gradient ↗ compared_alternative [BSD 3-Clause License]

Array-oriented gradient estimation on sampled grids; the SciPy derivative reference matches the pointwise finite-difference helper the exercises implement.

SymPy Calculus tutorial ↗ compared_alternative [BSD License]

Symbolic rather than numerical differentiation; useful for exact cross-checks but not the finite-difference practice target.

03 120 分鐘

數值實驗與失敗分析

Agent 輔助工程檢查

前提條件

完成前一項活動;如遇困難,使用列出的恢復來源。

學習目標

執行一個可重現的純 Python 有限差分實驗,比較前向與中央誤差,並在 h 變得極小時用 sqrt(1+h)-sqrt(1) 診斷相消。

操作說明

1. 腳本開頭列印 sys.version,使執行可以重現。
2. 參照值單獨保存:只計算一次 math.cos(1),絕不從有限差分推出它。
3. 走訪 h = .1, 1e-3, 1e-5, 1e-8, 1e-12,以科學記號列印 sin(1) 的 h、前向誤差和中央誤差。
4. 加入相消的例子:h = 1e-8, 1e-12, 1e-16 時的前向估計 (sqrt(1+h) - sqrt(1))/h。執行前先預測在雙精度下 1 + 1e-16 的結果,以及這對分子意味著什麼。
5. 保留所有行,包括不穩定的行;隱藏壞行的實驗不能作為證據。
6. 為每個區間寫一行診斷:截斷誤差主導、相消/捨入主導,或有效數字完全喪失。

時間安排(120 分鐘):閱讀 20 + 練習 70 + 檢查 30。 閱讀:提示和活動 2 的表。練習:撰寫並執行腳本,記錄完整輸出。檢查:將表和診斷與參考答案對照;若某行不一致,換一種格式重新執行。

練習與預測紀錄

寫一個短腳本,列印 sin(1) 的 h、前向誤差和中央誤差。為平方根例子加入 h=1e-16,並預測分子在浮點中是否仍可區分。

獨立掌握標準

獨立作業必須包含可執行程式碼、至少四個 h 值的表格記錄,以及區分截斷誤差與相消/捨入的書面診斷。

權威來源資源與出處

linking CC BY-NC-SA 4.0 attribution noncommercial share-alike

OpenStax Calculus Volume 1

作者 / 機構: OpenStax, Rice University (Gilbert Strang, Edwin Herman, et al.) | 原始出處連結 ↗

對應章節 / 時間戳: Sections 2.1-2.3 (Functions, Limits, and the Derivative); Sections 3.1-3.2 (Derivative Rules)

驗證版本與日期: 3rd edition, verified 2026-10-30

入選理由:

Open, systematic treatment of the limit definition and derivative rules with stable section locations.

來源前置條件:

Algebra, coordinate geometry, and function notation

安全回退方案(離線彈性):

The authored derivations, tables, and exercises in these three Study Days are complete offline recovery material; use the linked section only for optional comparison.

對比候選資源與選型分析 (3)
MIT OpenCourseWare 18.01SC Single Variable Calculus ↗ compared_selected_for_visual_practice [CC BY-NC-SA 4.0]

Strong exercises and transcripts, but the noncommercial share-alike terms and course navigation make OpenStax the primary stable text.

Paul's Online Math Notes: Calculus I ↗ compared_rejected_for_reuse [All Rights Reserved]

Useful reference, but rights and redistribution terms are less suitable for embedded recovery material.

SciPy Numerical Differentiation Documentation ↗ compared_selected_for_numerics [BSD-3-Clause]

A primary API reference for numerical derivative step-size behavior; it complements rather than replaces the hand derivation.

linking BSD-3-Clause

SciPy numerical derivative reference

作者 / 機構: SciPy Developers | 原始出處連結 ↗

對應章節 / 時間戳: derivative API: finite differences, step-size iteration, tolerances

驗證版本與日期: SciPy documentation, verified 2026-10-30

入選理由:

Documents numerical differentiation as an approximation with finite step sizes and stopping criteria.

來源前置條件:

Python arithmetic and the definition of a derivative

安全回退方案(離線彈性):

The local Day 18 tables and pure-Python snippets reproduce the relevant finite-difference behavior without SciPy.

對比候選資源與選型分析 (2)
NumPy: numpy.gradient ↗ compared_alternative [BSD 3-Clause License]

Array-oriented gradient estimation on sampled grids; the SciPy derivative reference matches the pointwise finite-difference helper the exercises implement.

SymPy Calculus tutorial ↗ compared_alternative [BSD License]

Symbolic rather than numerical differentiation; useful for exact cross-checks but not the finite-difference practice target.

04 120 分鐘

檢索、複習與獨立掌握

獨立掌握檢查(無 AI 輔助)

前提條件

完成前一項活動;如遇困難,使用列出的恢復來源。

學習目標

把解析導數、不可導性、有限差分與數值誤差整合成一套可辯護的驗證流程,用於模型訓練數學。

操作說明

1. 闔上書和筆記。憑記憶依次回答題目的 (1)-(6)。
2. 在 (1) 和 (2) 中給出論證而不只是數值:寫明 x^3 所用的法則,並寫出 |x| 在 0 處的兩個單側斜率。
3. 在 (3) 中定義一套流程:一種格式、一組跨越幾個數量級的 h 值,以及一條接受規則(例如在捨入誤差主導之前估計值逐漸逼近的相對誤差容差)。
4. 在 (4) 中列出要記錄的證據欄位:函數、點、dtype、h 值、估計值、參照值、誤差和失敗情況。
5. 在 (5) 中自己選定並寫明函數、點和 h,把中央差分計算一直做到與解析導數比較的誤差。
6. 在 (6) 中用自己的話寫出局限性說明。
7. 對照參考答案批改。每個遺漏都回看 Day 16-18 中對應的活動,並獨立重做一道等價題。

時間安排(120 分鐘):閱讀 15 + 練習 60 + 檢查 45。 閱讀:無(閉卷回憶)。練習:六部分作答和計算示例。檢查:批改、回看,並獨立重做每道遺漏的題。

練習與預測紀錄

閉卷:(1) 給出 x^3 在 2 處的導數;(2) 說明 |x| 在 0 處是否可導及原因;(3) 為檢驗黑箱光滑函數選擇一種有限差分格式和 h 策略;(4) 列出你會記錄的證據;(5) 附上由你自己選定並說明的函數、點和 h 的中央差分完整計算;(6) 明確說明有限差分檢驗是驗證證據,而非可導性或精確性的證明。

獨立掌握標準

獨立掌握檢查:提交四部分回答、一個完整的中央差分計算,並明確聲明有限差分是驗證證據,而非可導性或精確性的證明。

權威來源資源與出處

linking CC BY-NC-SA 4.0 attribution noncommercial share-alike

OpenStax Calculus Volume 1

作者 / 機構: OpenStax, Rice University (Gilbert Strang, Edwin Herman, et al.) | 原始出處連結 ↗

對應章節 / 時間戳: Sections 2.1-2.3 (Functions, Limits, and the Derivative); Sections 3.1-3.2 (Derivative Rules)

驗證版本與日期: 3rd edition, verified 2026-10-30

入選理由:

Open, systematic treatment of the limit definition and derivative rules with stable section locations.

來源前置條件:

Algebra, coordinate geometry, and function notation

安全回退方案(離線彈性):

The authored derivations, tables, and exercises in these three Study Days are complete offline recovery material; use the linked section only for optional comparison.

對比候選資源與選型分析 (3)
MIT OpenCourseWare 18.01SC Single Variable Calculus ↗ compared_selected_for_visual_practice [CC BY-NC-SA 4.0]

Strong exercises and transcripts, but the noncommercial share-alike terms and course navigation make OpenStax the primary stable text.

Paul's Online Math Notes: Calculus I ↗ compared_rejected_for_reuse [All Rights Reserved]

Useful reference, but rights and redistribution terms are less suitable for embedded recovery material.

SciPy Numerical Differentiation Documentation ↗ compared_selected_for_numerics [BSD-3-Clause]

A primary API reference for numerical derivative step-size behavior; it complements rather than replaces the hand derivation.

linking BSD-3-Clause

SciPy numerical derivative reference

作者 / 機構: SciPy Developers | 原始出處連結 ↗

對應章節 / 時間戳: derivative API: finite differences, step-size iteration, tolerances

驗證版本與日期: SciPy documentation, verified 2026-10-30

入選理由:

Documents numerical differentiation as an approximation with finite step sizes and stopping criteria.

來源前置條件:

Python arithmetic and the definition of a derivative

安全回退方案(離線彈性):

The local Day 18 tables and pure-Python snippets reproduce the relevant finite-difference behavior without SciPy.

對比候選資源與選型分析 (2)
NumPy: numpy.gradient ↗ compared_alternative [BSD 3-Clause License]

Array-oriented gradient estimation on sampled grids; the SciPy derivative reference matches the pointwise finite-difference helper the exercises implement.

SymPy Calculus tutorial ↗ compared_alternative [BSD License]

Symbolic rather than numerical differentiation; useful for exact cross-checks but not the finite-difference practice target.