What is a good algorithm for Second Order Polynomial Curve Fit without Cramer's Rule?

484 Views Asked by At

I'm writing some code in C++ to extract a 2nd Order Polynomial Curve out of a set of data to remove non-periodic elements from it before performing signal analysis. My strategy for generating the curve was to use Least Sum Squares using Cramer's Rule, where the coefficients of the polynomial, $a_k$, is $$a_k=\frac{det(M_k)}{det(M)},$$

$$M=\begin{matrix} N & \sum^N_{i=1}x_i & \sum^N_{i=1}x^2_i \\ \sum^N_{i=1}x_i & \sum^N_{i=1}x^2_i & \sum^N_{i=1}x^3_i \\ \sum^N_{i=1}x^2_i & \sum^N_{i=1}x^3_i & \sum^N_{i=1}x^4_i \\ \end{matrix}$$

where $N=$ number of samples taken, $x_i=$ independent variable value at sample $i\in [1,...N]$, and $M_i=$ the matrix $M$ with the $k$th column (for $k\in[1,2,3]$) replaced by

$$b=\begin{matrix} \sum^N_{i=1}y_i \\ \sum^N_{i=1}x_iy_i \\ \sum^N_{i=1}x^2_iy_i \end{matrix}$$

For example, $$M_1=\begin{matrix} \sum^N_{i=1}y_i & \sum^N_{i=1}x_i & \sum^N_{i=1}x^2_i \\ \sum^N_{i=1}x_iy_i & \sum^N_{i=1}x^2_i & \sum^N_{i=1}x^3_i \\ \sum^N_{i=1}x^2_iy_i & \sum^N_{i=1}x^3_i & \sum^N_{i=1}x^4_i \end{matrix}$$

This works to generate the coefficients of the 2nd order polynomial function that fits the set of data $$y=a_2x^2+a_1x+a_0$$

Working with the data set that I have, I am capable of making the assumption that the time change between samples is constant across all the samples; therefore, I am handing the summation of $x$ independent variables as simply the summation of integer values up to the Sample $i$ in question (for simplicity sake. I'd rather only have to handle a collection of dependent variable values rather than that and maintaining the independent variables as well).

The problem is I have sample sizes upwards of 400 samples per data set, and performing some of the summations using this (such as $\sum^{400}_{i=1}x^4_i$) causes... well, let's just say that the memory required for that calculation is not available for use. Positive numbers turn to negative numbers, cats sleep with dogs, etc.

So, my question is: is there a more efficient algorithm for finding $a_k$ coefficients that would not be as taxing on memory requirements?