Navigation: Scripting Reference Manual > Functions & Subroutines > PolynomialSolve

 

PolynomialSolve

 

Contact Us: fredsupport@photonengr.com

 

Description

This function takes x,y data in the form of two arrays and fits the data to a polynomial with a user-specified order.  The coefficients of the polynomial are stored in the coeffs() array after the function call is complete and return values indicate the status of the fit.

 

 

Syntax

status = PolynomialSolve( x(), y(), coeffs(), nSizeData, nCoeffs, svdCondition, rmsError, peakError )

 

 

Parameters

status (Integer)

Returned integer indicating the solution status.  Return values have the following meaning:

Return Value

Meaning

0

An error occurred.

1

An exact solution was found.

2

No exact solution was found because the problem is over constrained.  The computed value of X is the closest answer in the sense that MX is as close as possible to b.

3

There are an infinite number of solutions because the problem is under constrained (more coefficients than data points).  In this case the computed value of X is the solution with the smallest values.

4

No exact solution was found because the problem is over constrained.  The computed value of X is the closest answer in the sense that MX is as close as possible to b.

 

x() As Double

Array of X values being fitted.  Must have the same dimensionality as y() or the function will halt with the error message, "Error: Array dimension: wrong number of elements."

 

y() As Double

Array of Y values being fitted.  Must have the same dimensionality as x() or the function will halt with the error message, "Error: Array dimension: wrong number of elements."

 

coeffs() As Double

After the PolynomialSolve function is executed, this array argument contains the coefficients of the polynomial fitted to the x(),y() dataset.  The dimensionality of coeffs() depends on the nCoeffs argument.  After the function is executed, the 0th index of the coeffs() array contains the constant term, the 1th index of the coeffs() array contains the x1 term, the 2nd index of the coeffs() array contains the x2 term, etc.

 

nSizeData As Long

Argument indicating that the first nSizeData points in x() and y() should be used for fitting.  If all data points in x() and y() are intended to be fit, then nSizeData can be set to 0 or UBound( x() ) + 1.  If 1 < nSizeData < UBound( x() ) + 1, then the first nSizeData points in the x() and y() arrays will be used for fitting.

 

nCoeffs As Long

Argument indicating the number of coefficients in the fitted polynomial (the polynomial is order nCoeffs-1).  The coeffs() array will have indexing that goes from 0 to nCoeffs-1.

 

svdCondition As Double

Argument passed in as a variable.  After the function executes it contains the value indicating the ratio of the maximum and minimum singular values of the SVD matrix.  A smaller number is generally better but the value is relative.  When comparing multiple fitted solutions, the solution with the smaller svdCondition value is generally better.

 

rmsError As Double

Argument passed in as a variable.  After the function executes it contains the RMS error of the fit.

 

peakError As Double

Argument passed in as a variable.  After the function executes it contains the peak error of the fit.

 

 

Example

The example code below demonstrates a utility function that wraps the PolynomialSolve function call to perform the fitting and report the results.  The x and y data arrays are passed into the function along with the polynomial fit order.  An array used for storing the coefficients of the fit is passed into the function by reference so that the resulting coefficient values can be accessed outside of this local function call.  Three additional variables are passed by reference that allow the function to return the SVD condition value, RMS error and peak error of the fitted solution.

 

 Function PerformPolynomialSolve( ByVal in_x() As Double, _

                                 ByVal in_y() As Double, _

                                 ByVal in_polyOrder As Long, _

                                 ByRef out_coeffs() As Double, _

                                 ByRef out_svdCondition As Double, _

                                 ByRef out_rmsError As Double, _

                                 ByRef out_peakError As Double, _

                                 Optional ByVal in_print As Boolean ) As Integer

 

 

    'Wrapper function for performing a polynomial solve and reporting results.

    'INPUTS:

    '   in_x() = array of x data values being fitted

    '   in_y() = array of y data values being fitted

    '   in_polyOrder = order of the polynomial fit

    '   in_print = optional boolean indicating whether you want the output printed

    'OUTPUTS:

    '   out_coeffs() = array of the cofficients for the polynomial fit

    '   out_svdCondition = value indicating the condition of the SVD matrix

    '   out_rmsError = value indicating the rms error of the fitted solution

    '   out_peakError = value indicating the peak error of the fitted solution

 

    'Perform fitting and specify nSizeData = -1 so that all of the data from

    'in_x() and in_y() is included.

    Dim status As Integer

    status = PolynomialSolve( in_x(), in_y(), out_coeffs(), 0, in_polyOrder+1, out_svdCondition, out_rmsError, out_peakError )

 

    If in_print Then

        'Fit status reporting

        Print ""

        Print "Solution status: ";

        Select Case status

            Case 0

                Print "[0] An error occured."

            Case 1

                Print "[1] An exact solution was found."

            Case 2

                Print "[2] No exact solution was found because the problem is over constrained.  The computed value of X is the closest answer in the sense that MX is as close as possible to b."

            Case 3

                Print "[3] There are an infinite number of solutions because the problem is under constrained (more coefficients than data points).  In this case the computed value of X is the solution with the smallest values."

            Case 4

                Print "[4] No exact solution was found because the problem is over constrained.  The computed value of X is the closest answer in the sense that MX is as close as possible to b."

        End Select

        Print ""

        Print "SVD Condition = " & out_svdCondition

        Print "RMS Error = " & out_rmsError

        Print "Peak Error = " & out_peakError

        Print ""

 

        'Fit coefficient reporting

        Dim curCoeff As Long

        Print ""

        Print "Polynomial solve: "

        Print Chr(9) & "Term" & Chr(9) & "Coefficient"

        For curCoeff = 0 To UBound(out_coeffs)

            Print Chr(9) & curCoeff & Chr(9) & out_coeffs(curCoeff)

        Next

    End If

 

    'Function return

    Return status

 

End Function

 

See Also

Math Function Script Commands

 

 

 

 

 

Copyright © Photon Engineering, LLC