Chuyển đến nội dung

Bài 46 · Projects and Practice

One-page cheat sheet

A one-page recap with a poster of the key schemes, stability rules and checks, plus a tested mini-library of reference implementations in Python.

This page is the recap you keep open while coding: six panels with the formulas that matter, then a small library of reference implementations that you can copy into your own programs. Every function in the library is tested against a known answer in the same editor, so you can trust it as a starting point.

A poster with six panels: heat, wave, Laplace, molecular dynamics, quantum and data analysis
Chapters 5 to 8 on one sheet: schemes, safety rules and checks.

What you will learn

  • The one formula or rule to remember for each method.
  • Which known case tests each method.
  • How to organize the methods as small, reusable functions.

Key terms

Scheme: a rule that turns an equation into a computable update.

Stability limit: the largest step for which errors do not grow.

Known case: a situation with an exact answer used to test the code.

Reference implementation: the simplest correct version of a method, kept as a baseline.

Regression test: a check that a function still returns the right answer after you change the code.

The six rules

  1. Heat (FTCS): u[j] += r (u[j+1] − 2u[j] + u[j−1]), r = αΔt/Δx² ≤ 1/2. Test: sin(πx) decays as exp(−π²αt). Implicit schemes remove the limit.
  2. Wave (leapfrog): u⁺ = 2u − u⁻ + r² Δ²u, r = cΔt/Δx ≤ 1. Test: a pulse splits into two half-height pulses; r = 1 is exact.
  3. Laplace: each interior value is the average of its four neighbors; SOR uses ω ≈ 2/(1 + sin(π/(N−1))). Test: center of a square with one edge at 1 is 0.25.
  4. Molecular dynamics: U = 4ε[(σ/r)¹² − (σ/r)⁶], velocity Verlet, Δt ≈ 0.005, r_c ≤ L/2. Test: energy fluctuation scales like Δt² and does not drift.
  5. Quantum: H = −½ d²/dx² + V. Eigenvalues from a tridiagonal matrix; time evolution by split-step Fourier; ⟨H⟩ ≥ E₀. Tests: box E = n²π²/2, oscillator E = n + ½, free-packet width σ√(1 + (t/2σ²)²).
  6. Data: χ² = Σ((y − f)/σ)², χ²/dof ≈ 1; σ_mean = σ/√N; Δf = 1/T, f_max = f_s/2.

A final habit: halve and compare

For any scheme with a step h, run with h and h/2 and compute the observed order p = log₂(e(h)/e(h/2)). If p does not match the theory (2 for the central differences used here), something is wrong, even when the pictures look fine.

Common misconceptions

  • "A cheat sheet replaces understanding." The formulas are a memory aid. If you cannot explain why r ≤ 1/2, go back to the lesson.
  • "The stability limit is a rule of thumb." It is a mathematical condition; crossing it makes the error grow without bound.
  • "Reference implementations are production code." They are baselines for clarity. Real codes add neighbor lists, parallelism and sparse solvers.

Try it

A mini-library of reference implementations with one test per function. If everything is right, the program ends with "all checks passed".

Python Mini-library with tests

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which stability rule applies to the explicit FTCS heat scheme?

    FTCS for the heat equation needs r = αΔt/Δx² at most 1/2.
  2. 2. What is the cheapest test of a Laplace solver on a square with one edge at 1?

    By symmetry the four rotated solutions add to a constant 1, so each contributes 0.25 at the center.
  3. 3. You halve the step and the error falls by 8. The observed order is...

    p = log₂(8) = 3.
  4. 4. What should the norm of ψ stay at during split-step evolution?

    The method is unitary, so the norm is conserved.

Summary

  • One rule per method: r ≤ 1/2 (heat), r ≤ 1 (wave), average of neighbors (Laplace), Verlet with Δt ≈ 0.005 (MD), unitary split-step (quantum), χ²/dof ≈ 1 (data).
  • Test every function on a known case and compare h with h/2.
  • Keep reference implementations small; grow them only when a test demands it.

Next: Where to go next

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.