## polyFit

Block Category: Matrix Operation

Inputs:

X: Vector of independent input values.

Y: Vector of dependent output values.

Signal In: Scalar x value.

Outputs:

Coeffs: Regression coefficients (column vector with the topmost element associated with the nth power of the Signal In value.

Signal Out: Result of applying the regression polynomial to the Signal In value.

Description: The polyFit block performs a polynomial regression relating a vector of input value ) to a vector of output values y as an nth order polynomial, where the polynomial order is a user-configurable value ranging from 0 – 256.

The polynomial can be calculated once on the simulation start or iteratively. Calculate coefficients once on startup: Calculates the polynomial fit once at start of the simulation, which is fine for static data sets. The fit algorithm is numerically costly, so it should only be run if needed.

Polynomial Order: Specifies the maximum exponent for the polynomial fit.

#### Examples

1. Input and output signal connections

The block diagram below shows the input and output signal connections for a polyFit block. Here, the xVector variable is a row vector of recorded values of the independent variable; the yVector variable is a row vector of recorded dependent variables (with the same number of columns as the xVector); and the x variable is a scalar independent value that is applied to the regression polynomial coefficients (shown as the column vector Coeffs = [-20.655; 305.17]) to produce the Signal Out variable labeled as yHat.

2. Static fit

The block diagram below shows how a polyFit block is used to fit a first order polynomial to a 10-element data set. The data set consists of a 10-element row vector of independent x values recorded in the xVector variable block and a corresponding 10-element row vector of dependent y values recorded in the yVector variable block. The scalarToVec block is used to create the two vectors with the following settings:  “rows” = 1 and “columns” = 10. The polyFit block is configured to fit a first order polynomial and to calculate the coefficients once at startup. The RegressionCoefficients are calculated as

[C1 = 3.97576; C2 = 8.50909]

The regression formula used by the polyFit block to calculate yHat is:

yHat = C1 * x + C2

The plot block compares the yHat variable with the dependent variable vector yVector when the Signal In is a sweep of the xVector values. A unit ramp is used to create the x signal. The yHat value from the polyFit block is plotted in blue; a calculation of the yHat signal is plotted in red. The green trace is the recorded dependent variable from the yVector.

3. Recursive fit

This block diagram shows how the polyFit block is used to fit a second order polynomial to a 100-element moving window data set. The generation of the independent input signal x and the dependent output signal y are shown in the upper part of the block diagram.

The buffer blocks are used to record the 100-element moving window of x and y values at 0.2 sec intervals. Since the simulation update time is 0.01 sec, the buffer interval causes the moving window data sets to overlap with one another.

The polyFit block is configured to fit a second order polynomial and to calculate the coefficients at each time step of the simulation. The polyFit output signal yHat is plotted with the recorded dependent variable y.