|
fitpack
Modern Fortran library for curve and surface fitting with splines
|
This tutorial covers parametric curve fitting in \( \mathbb{R}^d \) with FITPACK: open parametric curves, closed (periodic) curves, and curves with prescribed endpoint derivatives.
A standard 1D spline \( y = s(x) \) cannot represent multi-valued or looping paths. A parametric curve expresses each coordinate as a separate spline over a common parameter \( u \):
\[ \mathbf{s}(u) = \bigl( s_1(u),\; s_2(u),\; \ldots,\; s_d(u) \bigr), \quad u \in [u_1, u_m] \]
All \( d \) component splines share a single knot vector and degree \( k \). Use parametric curves for multi-dimensional paths, closed loops, or constrained endpoints with prescribed positions and derivatives.
| Type | Description | Core routine |
|---|---|---|
fitpack_parametric_curve | Open parametric spline | parcur |
fitpack_closed_curve | Periodic parametric spline | clocur |
fitpack_constrained_curve | Endpoint derivative constraints | concur |
When no parameter values are supplied, FITPACK computes them from cumulative chord lengths, normalized to \( [0, 1] \):
\[ u_1 = 0, \quad u_i = u_{i-1} + \| \mathbf{x}_i - \mathbf{x}_{i-1} \|, \quad u_m = 1 \]
You may also supply your own strictly increasing parameter values (e.g. time stamps) via the optional u argument.
Construct and fit with new_fit:
Arguments: x(idim, m) data points; optional u(m) parameter values; optional w(m) weights; optional smoothing ( \( s \), use zero for interpolation); optional order (spline degree \( k \), default 3).
Evaluation returns arrays whose first dimension is idim:
curve\eval_one(u, ierr) — returns array(idim) at a single \( u \).curve\eval_many(u, ierr) — returns array(idim, m) at multiple \( u \).curve\eval dispatches to either form.Derivatives with respect to \( u \):
curve\dfdx(u, order, ierr) — returns array(idim), the order-th derivative.curve\dfdx_all(u, ierr) — returns array(idim, 0:k), all derivatives at once.A Lissajous curve \( x = \sin(3t),\; y = \sin(2t) \) is multi-valued in both coordinates, making it a natural test case.
fitpack_closed_curve extends fitpack_parametric_curve with periodic boundary conditions, guaranteeing:
\[ s_j^{(\ell)}(u_1) = s_j^{(\ell)}(u_m), \quad \ell = 0, 1, \ldots, k-1, \quad j = 1, \ldots, d \]
Do not duplicate the first point as the last; periodicity is handled internally by clocur.
The fields curve\ubegin and curve\uend give the parameter range. For a closed curve, evaluating at either boundary returns the same point.
fitpack_constrained_curve extends fitpack_parametric_curve to prescribe function values and derivatives at the endpoints. This is useful when the curve must join smoothly to another segment, or when physical boundary conditions (position, velocity, acceleration) are known.
ddx_begin(idim, 0:nb-1) — Left endpoint: column 0 is position \( \mathbf{s}(u_1) \), column \( \ell \) is \( \mathbf{s}^{(\ell)}(u_1) \).ddx_end(idim, 0:ne-1) — Right endpoint (same layout).Shape (idim, 0:1) prescribes position and first derivative; (idim, 0:2) adds the second derivative, and so on.
Fit \( x = u\cos u,\; y = u\sin u \) over \( [0, \pi] \) with prescribed endpoint positions and tangent vectors.
Note the two-step workflow: new_fit loads data and produces an initial fit; then set_constraints + curve\fit re-fits with boundary conditions enforced.
A full working program combining all three curve types is provided in examples/example_parametric_curves.f90. Build and run with:
All routines return an integer(FP_FLAG) error code:
When the optional ierr argument is omitted, the library halts on failure.
| Type | Use case | Key methods |
|---|---|---|
fitpack_parametric_curve | Open paths in \( \mathbb{R}^d \) | new_fit, eval, dfdx |
fitpack_closed_curve | Periodic loops | new_fit, eval, ubegin/uend |
fitpack_constrained_curve | Prescribed endpoint derivatives | set_constraints, fit, dfdx_all |