Blog

All Blog Posts  |  Next Post  |  Previous Post

Curve Fitting with FNC Math Components

Monday, December 27, 2021

In this article, we’ll show how to implement curve fitting with FNC math components presented in version 3.2 of TMS Analytics & Physics library, most importantly, with little to no code!

Curve fitting (approximation) is a mathematical method of finding a math expression (function) that fits best to some discrete data (series of points). Commonly, we need the following three ingredients to implement the curve fitting:

  • Discrete data (a series of points) to fit.
  • A set of functions (basis functions) to construct the curve.
  • A method of solving the fitting problem (optimization algorithm).
Let’s go through the FNC components for these three elements. The first component is TFNCDataSource1D. The component provides data for curve fitting and has the following published properties:

  • Data (TDataCollection1D) – a collection of points. Each collection item has two properties of real type: ‘V’ – the value of the variable; ‘F’ – the value of the function.
  • DataCount (integer) – the number of points in the collection (read-only).
With standard Delphi IDE tools for collection editing, we can add and delete points, edit values of the points in design time. There is a binding component to show the data on an FNC chart - TFNCData1DPlotter. It has two primary published properties:

  • Data (TFNCDataSource1D) – a data source to show on the chart.
  • Chart – a chart to show the data.
When assigning an FNC chart to the Chart property in design time, the plotter component creates a series for the chart. You can tune the series' properties to get the appropriate data representation. In the picture below, we showed an example of data, drawn with the data plotter.

TMS Software Delphi  Components

The second ingredient for curve fitting is the set of basis functions. We’ll use the TFNCLinearConstructedBasis1D component. This component provides the easiest way to get a set of functions for constructing the fitted curve. The main published properties of the component are the following

  • Variable (TVariableProperty) – provides the name of the variable for the basis functions.
  • Coefficient (TVariableProperty) – provides the name of the coefficient for the fitting problem.
  • Kind (TBasisKind) – the type of predefined basis functions.
  • Order (integer) – number of basis functions for approximation.
  • Expression (string) – read-only math expression of the constructed function.
  • Parameter (TParameterCollection) – a set of parameter values for parametric basis functions.


    With the Kind property, you can select the appropriate basis functions for building the fitted curve: polynomials, Taylor series, exponents, Fourier series, exponential functions. The Order property sets up the number of basis functions to build the curve. Here is an example of the component’s properties, shown in the Object Inspector.

    TMS Software Delphi  Components

    The last component we need to complete curve fitting is TFNCApproxFunction1D. This component joins a data source with a basis and implements the least-squares approximation algorithm. It has the following published properties:

    • DataSource (TFNCDataSource1D) – a data source for approximation.
    • Basis (TFNCBasis1D) – a basis for approximation.
    • Formula (TBaseFormulaProperty) – read-only formula of the curve.
    • Expression (string) – read-only math expression of the fitted curve.

    When you assign the data source and the basis properties, the component runs the approximation algorithm and evaluates the formula and the expression properties, as shown in the picture below.


    TMS Software Delphi  Components

    The TFNCApproxFunction1D is a descendant of the TFNCBaseFunction1D component. So, you can draw the curve on an FNC chart as described in the previous article. An example of the resulting curve is shown in the following picture:

    TMS Software Delphi  Components

    You can change the kind and the order (number of functions) of the basis at design time. Any change leads to the approximation algorithm run, and you can see the resulting curve on the chart. The same is true for the data source: when a point from the collection changed, the curve is reconstructed to fit the new data. In the following two pictures, we showed the approximated curve for the basis Order=6 and for changed data points.

    TMS Software Delphi  Components

    TMS Software Delphi  Components

    The last option to mention is the Parameters property of the TFNCLinearConstructedBasis1D component. Some basis functions are parametric and they require parameter values for evaluation. For example, the Fourier basis requires the ‘L’ parameter – period of the sine and cosine functions in the Fourier series. When the basis kind changes, the component automatically adds required parameters into the collection, and you can assign the appropriate values to them. An example of the data, approximated with Fourier basis for L=4*Pi is shown in the picture below:

    TMS Software Delphi  Components

    With the FNC math components, you can create the curves fitting application visually, with no line of code. You can tune the components and set the appropriate options at design time to select the best-fitted curve for your data.

    The source code of the demo project for the article can be downloaded from here. To start using the FNC math components in your applications you need to install the TMS Analytics & Physics and TMC FNC Chart products.





    Bruno Fierens




    This blog post has not received any comments yet.



    Add a new comment

    You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
    All fields are required.



    All Blog Posts  |  Next Post  |  Previous Post