JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
Collaboration diagram for Polynomial Fits/Regression:

Functions

template<class InputItX , class InputItY , class OutputItP >
void jkqtpstatPolyFit (InputItX firstX, InputItX lastX, InputItY firstY, InputItY lastY, size_t P, OutputItP firstRes)
 fits (in a least-squares sense) a polynomial $ f(x)=\sum\limits_{i=0}^Pp_ix^i $ of order P to a set of N data pairs $ (x_i,y_i) $
 

Detailed Description

Function Documentation

◆ jkqtpstatPolyFit()

template<class InputItX , class InputItY , class OutputItP >
void jkqtpstatPolyFit ( InputItX  firstX,
InputItX  lastX,
InputItY  firstY,
InputItY  lastY,
size_t  P,
OutputItP  firstRes 
)
inline

fits (in a least-squares sense) a polynomial $ f(x)=\sum\limits_{i=0}^Pp_ix^i $ of order P to a set of N data pairs $ (x_i,y_i) $

Template Parameters
InputItXstandard iterator type of firstX and lastX.
InputItYstandard iterator type of firstY and lastY.
OutputItPoutput iterator for the polynomial coefficients
Parameters
firstXiterator pointing to the first item in the x-dataset to use $ x_1 $
lastXiterator pointing behind the last item in the x-dataset to use $ x_N $
firstYiterator pointing to the first item in the y-dataset to use $ y_1 $
lastYiterator pointing behind the last item in the y-dataset to use $ y_N $
Pdegree of the polynomial (P>=N !!!)
[out]firstResIterator (of type OutputItP ), which receives the (P+1)-entry vector with the polynomial coefficients $ p_i $

This function uses jkqtpstatLinSolve() to solve the system of equations

\[ \begin{bmatrix} y_1\\ y_2\\ y_3 \\ \vdots \\ y_n \end{bmatrix}= \begin{bmatrix} 1 & x_1 & x_1^2 & \dots & x_1^P \\ 1 & x_2 & x_2^2 & \dots & x_2^P\\ 1 & x_3 & x_3^2 & \dots & x_3^P \\ \vdots & \vdots & \vdots & & \vdots \\ 1 & x_n & x_n^2 & \dots & x_n^P \end{bmatrix} \begin{bmatrix} p_0\\ p_1\\ p_2\\ \vdots \\ p_P \end{bmatrix}  \]

\[ \vec{y}=V\vec{p}\ \ \ \ \ \Rightarrow\ \ \ \ \ \vec{p}=(V^TV)^{-1}V^T\vec{y} \]

See also
https://en.wikipedia.org/wiki/Polynomial_regression