PointBC: Single-Point Boundary Conditions

PointBC types specify derivative conditions at a single endpoint. They are the building blocks used by both quadratic and cubic splines.


Derivative Types

Deriv1: First Derivative (Slope)

Specify the slope S'(endpoint) = v directly:

Deriv1(0.0)   # Horizontal tangent
Deriv1(1.0)   # 45° upward slope
Deriv1(-2.0)  # Steep downward slope

Deriv2: Second Derivative (Curvature)

Specify the curvature S''(endpoint) = v:

Deriv2(0.0)   # Zero curvature (inflection point behavior)
Deriv2(1.0)   # Positive curvature (concave up)
Deriv2(-1.0)  # Negative curvature (concave down)

Deriv3: Third Derivative

For advanced use cases, specify S'''(endpoint) = v:

Deriv3(0.0)   # Constant second derivative near endpoint

PolyFit: Automatic Derivative Estimation

When you don't know the exact derivative values, PolyFit{D} estimates them by fitting a degree-D polynomial to the nearest D+1 data points.

How It Works

PolyFit{D} performs these steps:

  1. Extract D+1 points from the endpoint (left: first D+1 points, right: last D+1 points)
  2. Fit a Lagrange interpolating polynomial through these points
  3. Evaluate the polynomial's derivative at the endpoint

This gives a data-driven estimate of the endpoint derivative without requiring manual specification.

Available Degrees

TypePointsAccuracyFormula (uniform grid, left endpoint)
LinearFit() = PolyFit{1}2O(h)(y₂ - y₁) / h
QuadraticFit() = PolyFit{2}3O(h²)(-3y₁ + 4y₂ - y₃) / (2h)
CubicFit() = PolyFit{3}4O(h³)(-11y₁ + 18y₂ - 9y₃ + 2y₄) / (6h)
PolyFit{N}() (N > 3)N+1O(hᴺ)Barycentric interpolation
Choosing the Right Degree
  • LinearFit: Simplest, first-order accurate
  • QuadraticFit: Default for quadratic splines — exact for quadratic polynomials
  • CubicFit: Third-order accurate, exact for cubic polynomials
  • PolyFit{N}: Arbitrary degree N — uses barycentric interpolation for N > 3

Match the polynomial degree to your data's smoothness. See the visual comparison below for examples.

Higher Degree ≠ Always Better

Higher-degree fits are not always more accurate. For oscillatory or noisy data, high-degree polynomial fits can produce overshoot at boundaries. When in doubt, start with QuadraticFit or CubicFit and compare results visually.


Visual Comparison

Part 1: Smooth Polynomial Data

When the data near the boundary follows a smooth polynomial, higher-degree PolyFit estimates the true derivative more accurately.

The plots below show cubic_interp applied to f(x) = 2x³ - 13x with different boundary conditions. Notice how CubicFit() computes the exact boundary tangent, perfectly reproducing the original cubic.

Example block output

Key observations:

  • NaturalBC: Zero-curvature assumption causes deviation from the true cubic
  • LinearFit / QuadraticFit: Slightly underestimate the boundary slope
  • CubicFit: Computes the exact boundary derivative, enabling perfect reproduction ✓
Polynomial Reproduction Property

When the data near the boundary follows a smooth polynomial of degree ≤ D, PolyFit{D} computes the exact derivative.


Part 2: Oscillatory Data — When Higher Degree Fails

For oscillatory data (e.g., sin(x) with sparse sampling), higher-degree PolyFit can produce worse results due to overshoot at boundaries.

The plots below show the left boundary region of a longer interpolation interval. The gray dashed line is the true function. Open circles are all data points, while filled colored circles indicate the points used to estimate the left boundary derivative (2 for LinearFit, 3 for QuadraticFit, 4 for CubicFit, etc.).

Example block output

Observations by degree:

DegreeBehavior
D=1–2Stable with minimal undershoot
D=3Overshoot begins to appear at the left boundary
D=4–6Noticeable overshoot at the left boundary
D=7Follows the true function with slight undershoot
D=8, 9Severe overshoot with reversed slope
Runge-like Phenomenon

High-degree polynomial fits on oscillatory or non-polynomial data can produce wild oscillations near boundaries. The fitted polynomial tries to pass through many points but overshoots between them.

Practical Recommendation
  • Start with QuadraticFit or CubicFit
  • Only use higher degrees if your data is known to be a smooth polynomial
  • Always visually inspect boundary behavior when changing PolyFit degree

Usage Examples

Quadratic Splines

using FastInterpolations

x = range(0.0, 2π, 15)
y = sin.(x)

# Default: QuadraticFit at left
quadratic_interp(x, y, 1.0)

# CubicFit at right for higher accuracy
quadratic_interp(x, y, 1.0; bc=Right(CubicFit()))

# Known slope at left
quadratic_interp(x, y, 1.0; bc=Left(Deriv1(1.0)))

Cubic Splines

using FastInterpolations

x = range(0.0, 2π, 15)
y = sin.(x)

# Default: NaturalBC (zero curvature)
cubic_interp(x, y, 1.0)

# Use CubicFit for better polynomial reproduction
cubic_interp(x, y, 1.0; bc=CubicFit())

# Mix PolyFit with known derivative
cubic_interp(x, y, 1.0; bc=BCPair(CubicFit(), Deriv1(0.0)))

Mathematical Background

Lagrange Interpolation

PolyFit{D} uses barycentric Lagrange interpolation for numerical stability:

\[p(x) = \frac{\sum_{i=0}^{D} \frac{\beta_i y_i}{x - x_i}}{\sum_{j=0}^{D} \frac{\beta_j}{x - x_j}}\]

where $\beta_i = \frac{1}{\prod_{j \neq i} (x_i - x_j)}$ are the barycentric weights.

The derivative is computed analytically from this form, avoiding explicit polynomial coefficient computation.

Accuracy Order

For a smooth function $f$ sampled on a grid with spacing $h$:

  • PolyFit{D} estimates $f'(x_0)$ with error $O(h^D)$
  • Higher D → higher accuracy, but requires more points

API Reference

See the full API documentation in Types Reference:

  • PointBC — Abstract type for single-point BCs
  • Deriv1 — First derivative specification
  • Deriv2 — Second derivative specification
  • Deriv3 — Third derivative specification
  • PolyFit — Polynomial fitting BC
  • LinearFit — Alias for PolyFit{1}
  • QuadraticFit — Alias for PolyFit{2}
  • CubicFit — Alias for PolyFit{3}