Online Curve Fitting with SplineCloud

Fitting curves to data points is a crucial part of data analysis across many fields. Whether you need to model experimental measurements, approximate results of numerical analysis, extrapolate missing data, or just visualize relationships in your data – finding the right curve is essential for obtaining correct insights, predictions and estimates. Here we'll explore the basics of curve fitting, get an overview of the most common and unirersal methods, and learn how to easily find the best fit for your data using a free interactive tool integrated into SplineCloud.


The Basics of Curve Fitting

Curve fitting is the process of finding a mathematical function that closely matches a set of data points. The goal is to find a curve (or function) that models the relationship in the data as accurately as possible.

Some common use cases for curve fitting include:

Curve fitting falls into two main categories: approximation (optimal fit) and interpolation (exact fit). The goal of approximation is to find a function that closely resembles the relationship represented by a set of data points. Interpolation, on the contrary, aims at finding function that passes exactly through data points.

Approximation
Approximation
Interpolation
Interpolation

Regardless of the type, the fitted curve provides a compact mathematical formulation of the relationship in your data. Once the curve function is found, it can be used for everything from data visualization to modeling and forecasting. And important aspect here is the ability of the curve to be reused in third-party applications or computer code. The reusability of the curve ensures that dependent variable values can be evaluated for an arbitrary range of independent variable values.

SplineCloud simplifies the process of finding the best fit and ensures curve reusability. Once constructed in the cloud with the help of the interactive and comprehensive tool, your curves become accessible over SplineCloud API, so you can retrieve and restore them in your computer code with the help of general libraries for B-splines or NURBS. For more smooth experience SplineCloud also provides open client libraries for Python and MATLAB, which automate curve load and evaluation.


Fitting Methods

Numerous algorithms have been developed over decades to tackle the task of finding the best fit to data. In the conventional approach, the choice of the appropriate method relies on the specific characteristics of the data and the intended application. However, there exist universal methods that are applicable to the majority of cases. But let's first take a look at the most common options.

Linear and Polynomial Fitting

The simplest approaches of linear and polynomial regression are the most popular, regardless of their drawbacks. Linear fitting works fine only in two cases:

  1. Data shows evidence of linear behavior → you can use a simple y= ax+ b equation.
  2. Data is big enough so that the interpolation error is relatively small → you can use piecewise linear fitting.
Linear Fitting
Linear Function
Linear Regression
Piecewise Linear Fit

Some nonlinear relations in data can be fitted with polynomials. Simple polynomial fitting uses equations like y= ax2 + bx + c . Polynomial fitting and piecewise polynomial regression work better in cases when data show evidence of nonlinear behavior, and the number of data points is not sufficient to neglect the errors obtained by linear models. However, polynomial fitting often leads to the issue of overfitting, especially for high-order polynomials, causing the resulting curve to oscillate between data points, resulting in a drastic increase of the interpolation error.

Cubic Polynomial Fit
Cubic Polynomial Fit
Polynomial Overfitting
Overfitting

Spline Functions

Obtaining smooth curves is important when data contains measurement noise or natural variability. This is a perfect application for approximation with spline functions, and particularly smoothing splines.

Smoothing Splines

A smoothing spline is a flexible non-parametric method for evaluating splines as a linear combination of basis functions (B-splines). Each B-spline is nonzero over only a subset of the data range, determined by the knot locations. Together, a linear combination of B-splines can represent complex shapes while maintaining control and smoothness, avoiding excessive oscillations.

Smoothing Spline Fitting with SplineCloud
Smoothing Spline Fitting

The solution for a smoothing spline fit is based on minimizing a cost function that combines a goodness-of-fit term and a penalty term. The penalty term is based on the curvature of the curve, which helps control the level of smoothness (s-factor). The smoothing spline finds the optimal balance between fitting the data closely and keeping the function simple, preventing overfitting.

The smoothing spline algorithm is available in the SciPy library 'interpolate'. It is also SplineCloud’s default fitting method. The curve shape can be adjusted via a set of parameters:

  1. Smoothness factor – controls the number of knots, and eventually the curvature of the spline.
  2. Degree (order) of the underline B-splines.
  3. Knot positioning – can be used to tune local curvature in some regions.

Least Squares

This is a different method for finding the optimal spline function to fit a set of data points. It works by minimizing the sum of the squares of the offsets (residuals) of data points from the curve. Still, the target of the fitting algorithm is similar and is aimed at finding the balance between smoothness and adhering closely to the data points.

SplineCloud also supports the Least Squares method, so you can choose what works best for your data.

Least Squares Spline Fitting with SplineCloud
Least Squares Spline Fitting

Interpolating Splines

Interpolating splines in fact is just an edge case for smoothing splines, for which a smoothing factor equals to 0, meaning no smoothing is applied and a maximum number of knots is selected. Use this method if you need to obtain an exact fit for your data.

Spline Interpolation with SplineCloud
Spline Interpolation

Other Methods

In addition to polynomials and splines, mathematicians have developed numerous specialized functions for curve fitting. Methods like Fourier series, Taylor polynomials, Chebyshev polynomials, Rational Thiele interpolation and others excel at fitting certain data types.

Fourier series fit periodic data by superimposing sine waves. Taylor polynomials approximate local behavior via point derivatives. Chebyshev polynomials minimize error through orthogonal bases. Thiele interpolation uses a rational function that is expressed as a continued fraction. However, each method shines best only for particular data characteristics and their complexity and narrow focus limit broader applicability.

Splines, on the contrary, stand out for offering a flexible and generalizable approach. They make no assumptions about function form or derivatives. The piecewise nature of splines allow both global and local control, which makes them an excellent default choice before considering more exotic methods. The simplicity and intuitiveness of spline construction and manipulation make them accessible to experts and non-experts alike.

And by selecting spline methods, consider parametric splines, as they put the flexibility of the shape and ease of use to the next level.


Parametric Splines

Although spline functions are highly adjustable and provide control over fit tolerance and curve shape, sometimes it is still not enough. Data may contain important local patterns that are hard to reproduce while preserving global smoothness. And this is where parametric spline fitting shines.

Basically, a parametric spline curve is represented by two distinct spline functions that use the same knot vector. This provides the ability to represent much more complex shapes. Essentially, due to their ultimate flexibility and variety of options for shape control, parametric spline curves and surfaces are widely used in computer graphics systems and professional CAD systems.

Parametric spline represented by two spline functions
Parametric spline curve represented by two spline functions

In addition to the knot vector and degree, in the case of parametric splines, the shape of the B-splines can be adjusted by changing the position of the corresponding control points and their weights. Dragging control points mimics how designers create curves in CAD software.

Control points clustered closer together result in higher local curvature, while wider spacing creates flatter sections. The weight of each control point determines how strongly it influences the curve shape. Higher weighted points pull the spline closer to its position. Lower weights let the curve pass farther away.

This level of customization is available in SplineCloud through a Fine-Tune Mode. You can add, drag, and remove control points on the screen to tweak the curve shape. Adjusting weights further sculpts the fit locally. The hands-on control takes curve fitting beyond predefined algorithms and unlocks the ability to find the best possible fit for the cases when default fitting algorithms do not satisfy expectations.

Fine tunning spline curve adjusting control points
Fine tunning spline curve shape

Moreover, Fine-Tune mode enables interactive adjustment of the knot vector of the spline curve. The knots partition the spline into segments, and altering the knot vector impacts the curvature both locally and globally. Moving knot positions slides where segments of the spline start/end, shifting where it can bend. Adding more knots gives the spline more freedom to bend and fit complex patterns in the data. Each knot enables an additional degree of localization. Removing knots forces the spline to be smoother and less inclined to overfit. With fewer segments, it behaves more like a global polynomial. By adding repeating knots you can adjust the continuity of the spline, introducing sharp corners or even flat areas if needed.

Fine tunning spline curve adjusting knots
Adjusting knot vector to fit complex step function

Eventually, knots and control points of parametric splines serve as virtual “handles” for shaping spline curves interactively. Adjusting their positions grants localized influence over the curve:

Together, knots and control points provide an intuitive what-you-see-is-what-you-get editing experience. You see the curve reshape as you drag handles on the screen. This direct manipulation grants flexibility beyond preset algorithms. You can sculpt splines to match nuanced data features that mathematical fits miss.

The interactive feedback builds physical intuition for how splines work. Over a several trials you will quickly develop a feel for tweaking knots and control points to achieve desired curves. With Fine-Tune mode curve fitting transitions from being abstract mathematical approach into tangible user-controlled design method.


Reusing Curves in Code

The ability to reuse curves in external applications is another strong advantage of the SplineCloud approach. Once you achieve the ideal fit interactively, you can export curve parameters and restore it in your code, or use the curve API link to automate access to curve data. The majority of modern systems for computer math and data analysis have packages for working with B-splines and NURBS, so restoring curves in the code should be straightforward.

API link to SplineCloud curve
API link to constructed curve

SplineCloud also provides client libraries for both Python and MATLAB that automate curve load and construction in your code. With just a few commands the fitted splines from the cloud transition into a callable function in your code, so you can reuse them to build more complex models on top, perform comparison, numerical analysis, integration, and optimization, or simply generate plots.

Load and reuse spline curve in Python
Reuse of spline curve in Python
Load and reuse spline curve in MATLAB
Reuse of spline curve in MATLAB

Treating splines as standard callable functions makes them powerful building blocks in code. And by interactively fitting splines online first, you remove coding busywork, separate data from code, and make it accessible to others.

Striving to give it a try? Sign up for free now and have fun with our interactive curve-fitting approach!


Have some questions?

leave us a message here or write to info@splinecloud.com