| 
 | 1 | +# Step Size Selection (Epsilons)  | 
 | 2 | + | 
 | 3 | +Functions and theory for computing optimal step sizes in finite difference approximations.  | 
 | 4 | + | 
 | 5 | +## Theory  | 
 | 6 | + | 
 | 7 | +The choice of step size (epsilon) in finite difference methods is critical for accuracy. Too large a step leads to truncation error, while too small a step leads to round-off error. The optimal step size balances these two sources of error.  | 
 | 8 | + | 
 | 9 | +### Error Analysis  | 
 | 10 | + | 
 | 11 | +For a function `f` with bounded derivatives, the total error in finite difference approximations consists of:  | 
 | 12 | + | 
 | 13 | +1. **Truncation Error**: Comes from the finite difference approximation itself  | 
 | 14 | +   - Forward differences: `O(h)` where `h` is the step size  | 
 | 15 | +   - Central differences: `O(h²)`  | 
 | 16 | +   - Hessian central differences: `O(h²)` for second derivatives  | 
 | 17 | + | 
 | 18 | +2. **Round-off Error**: Comes from floating-point arithmetic  | 
 | 19 | +   - Forward differences: `O(eps/h)` where `eps` is machine epsilon  | 
 | 20 | +   - Central differences: `O(eps/h)`  | 
 | 21 | + | 
 | 22 | +### Optimal Step Sizes  | 
 | 23 | + | 
 | 24 | +Minimizing the total error `truncation + round-off` gives optimal step sizes:  | 
 | 25 | + | 
 | 26 | +- **Forward differences**: `h* = sqrt(eps)` - balances `O(h)` truncation with `O(eps/h)` round-off  | 
 | 27 | +- **Central differences**: `h* = eps^(1/3)` - balances `O(h²)` truncation with `O(eps/h)` round-off  | 
 | 28 | +- **Hessian central**: `h* = eps^(1/4)` - balances `O(h²)` truncation for mixed derivatives  | 
 | 29 | +- **Complex step**: `h* = eps` - no subtractive cancellation, only limited by machine precision  | 
 | 30 | + | 
 | 31 | +## Adaptive Step Sizing  | 
 | 32 | + | 
 | 33 | +The step size computation uses both relative and absolute components:  | 
 | 34 | + | 
 | 35 | +```julia  | 
 | 36 | +epsilon = max(relstep * abs(x), absstep) * dir  | 
 | 37 | +```  | 
 | 38 | + | 
 | 39 | +This ensures:  | 
 | 40 | +- **Large values**: Use relative step `relstep * |x|` for scale-invariant accuracy  | 
 | 41 | +- **Small values**: Use absolute step `absstep` to avoid underflow  | 
 | 42 | +- **Direction**: Multiply by `dir` (±1) for forward differences  | 
 | 43 | + | 
 | 44 | +## Implementation  | 
 | 45 | + | 
 | 46 | +The step size computation is handled by internal functions:  | 
 | 47 | + | 
 | 48 | +- **`compute_epsilon(fdtype, x, relstep, absstep, dir)`**: Computes the actual step size for a given finite difference method and input value  | 
 | 49 | +- **`default_relstep(fdtype, T)`**: Returns the optimal relative step size for a given method and numeric type  | 
 | 50 | + | 
 | 51 | +These functions are called automatically by all finite difference routines, but understanding their behavior can help with custom implementations or debugging numerical issues.  | 
 | 52 | + | 
 | 53 | +## Special Cases  | 
 | 54 | + | 
 | 55 | +### Complex Step Differentiation  | 
 | 56 | + | 
 | 57 | +For complex step differentiation, the step size is simply machine epsilon since this method avoids subtractive cancellation entirely:  | 
 | 58 | + | 
 | 59 | +⚠️ **Important**: The function `f` must be complex analytic when the input is complex!  | 
 | 60 | + | 
 | 61 | +### Sparse Jacobians  | 
 | 62 | + | 
 | 63 | +When computing sparse Jacobians with graph coloring, the step size is computed based on the norm of the perturbation vector to ensure balanced accuracy across all columns in the same color group.  | 
 | 64 | + | 
 | 65 | +## Practical Considerations  | 
 | 66 | + | 
 | 67 | +- **Default step sizes** are optimal for most smooth functions  | 
 | 68 | +- **Custom step sizes** may be needed for functions with unusual scaling or near-discontinuities  | 
 | 69 | +- **Relative steps** should scale with the magnitude of the input  | 
 | 70 | +- **Absolute steps** provide a fallback for inputs near zero  | 
 | 71 | +- **Direction parameter** allows for one-sided differences when needed (e.g., at domain boundaries)  | 
0 commit comments