Hello,

I used the Least Square Regression method described on the link posted by manor_royal to fit the curve and find the “a” term of the equation (y = a*x² + b*x + c) and calculate gravity correctly. You can find the function I wrote below.

Note1: It will not compile for the Atmega (UNO/Mega) unless you set the compiler optimization to “O2” for the Fitting function. It is not necessary for the ARM (DUE).

Note2: In the Atmegas, as it only deals with floating numbers up to 32bits, you’re gonna have an error due to the big numbers you get in the calculations. I did not write down the number, but is was something aroung 0.1%. It does not happen when using the DUE.

I hope it helps somebody else. Here is the code:

```
// USAGE: double g = fit_G( sizeof(x)/sizeof(double), t, x);
// should be declared like that to optimize for the 8bits microcontroller core.
double fit_G(int N_points, double px[], double py[]) __attribute__((__optimize__("O2"))); //Remove this one if you're using DUE.
double fit_G( int N_points, double px[], double py[] ) {
int i;
double S00, S10, S20, S30, S40, S01, S11, S21;
double denom, x, y, a, b, c;
S00=S10=S20=S30=S40=S01=S11=S21=0;
for (i=0; i<N_points; i++) {
x = px[i];
y = py[i];
//S00 += 1; // x^0+y^0
S10 += x;
S20 += x * x;
S30 += x * x * x;
S40 += x * x * x * x;
S01 += y;
S11 += x * y;
S21 += x * x * y;
}
S00 = N_points;
denom = S00*(S20*S40 - S30*S30) - S10*(S10*S40 - S20*S30) + S20*(S10*S30 - S20*S20);
/* c = (S01*(S20*S40-S30*S30)-S11*(S10*S40-S20*S30)+S21*(S10*S30-S20*S20))/denom;
b = (S00*(S11*S40-S30*S21)-S10*(S01*S40-S21*S20)+S20*(S01*S30-S11*S20))/denom;*/
a = ( S00*(S20*S21 - S11*S30) - S10*(S10*S21 - S01*S30) + S20*(S10*S11 - S01*S20) )/denom;
double g = a*2;
return g;
}
```