Function Settings
Difference Schemes
Backward: (f(x)-f(x-h))/h
Central: (f(x+h)-f(x-h))/(2h)
4-point: (-f(x+2h)+8f(x+h)-8f(x-h)+f(x-2h))/(12h)
Visualize truncation error and numerical order of accuracy for finite difference schemes. Understand how grid spacing affects discretization error and compare first- vs. second-order methods.
The core idea is approximating a derivative, which is fundamentally a limit, using a finite difference. The forward difference method is a common first-order approach.
$$f'(x) \approx \frac{f(x+h) - f(x)}{h}+ \mathcal{O}(h)$$Here, $f'(x)$ is the true derivative, $h$ is the finite step size, and $\mathcal{O}(h)$ is the truncation error term, meaning the error is proportional to $h$ (first-order accurate).
A more accurate central difference method uses points on both sides. This "cancels out" more of the error, leading to second-order accuracy.
$$f'(x) \approx \frac{f(x+h) - f(x-h)}{2h}+ \mathcal{O}(h^2)$$The error term $\mathcal{O}(h^2)$ shows second-order accuracy. If you decrease $h$ by a factor of 10, the error decreases by a factor of 100. This is the power of higher-order methods.
Computational Fluid Dynamics (CFD): Simulating airflow over a wing or weather patterns requires solving partial differential equations (PDEs) for fluid motion. The choice of discretization scheme (e.g., 1st vs. 2nd order) directly impacts the accuracy of lift/drag predictions and the computational time. A lower-order method might be used for initial, rapid prototyping.
Finite Element Analysis (FEA): When calculating stress in a bridge or a car chassis, FEA software approximates derivatives of displacement fields. The order of the element shape functions determines the truncation error. Higher-order elements can capture complex stress gradients with fewer, larger elements, saving memory.
Financial Engineering: Pricing complex financial derivatives often involves solving the Black-Scholes PDE numerically. The order of accuracy in the numerical scheme affects the precision of the calculated option price, which can translate to significant monetary value in high-frequency trading.
Computer Graphics & Animation: Simulating realistic cloth, hair, or water surfaces uses numerical integration of physical laws. A game might use a fast, low-order method for real-time animation, while a movie studio uses a high-order method for offline rendering to achieve photorealistic, error-free fluid simulations.
There are several important points to keep in mind when mastering this tool, especially with practical applications in view. First is the point that "the optimal h changes depending on the function and the point of differentiation." While the simulator's default function often shows an optimal h around $10^{-8}$, if the function's value is extremely large (e.g., $f(x)=10^{10}x$), the rounding error term becomes larger, so the optimal h becomes larger. Conversely, if the function's change is very gradual (close to $f(x)=constant$), the truncation error becomes smaller, allowing for a smaller h without issue. In short, blindly applying an optimal h found for one function to other cases is risky.
Next is the point that "higher-order difference schemes are not always the best." Four-point or six-point differences do indeed have a higher order of truncation error and are theoretically superior. However, these schemes require function values at more points, increasing computational cost. Furthermore, as the number of function evaluation points increases, the accompanying accumulation of rounding error cannot be ignored. Especially when the function itself contains noise (e.g., experimental data) or near discontinuities, oscillations in higher-order schemes can sometimes increase error. In practice, it's crucial to choose a scheme based on a comprehensive judgment of required accuracy, computational cost, and the nature of the function.
Finally, there is the fundamental question: "What do you do in real-world problems where the theoretical value is unknown?" In fields like CAE sensitivity analysis, the function you want to differentiate is often a black box (inside a solver). In such cases, the concept of the "error V-curve" learned with this tool becomes useful. You calculate the numerical derivative for several different h values (e.g., $10^{-4}, 10^{-6}, 10^{-8}$) and observe how the result changes as h becomes smaller. If the result doesn't change monotonically and starts oscillating at a certain point, it's a sign that rounding error influence has become dominant. Adopting the h value just before that as the "practical optimum" is an empirical approach that is vital in real-world settings.