Three points are enough to fit a quadratic curve to the data. Here is a simple program that will do that (it can use more input points, if you have them).
// fit quadratic curve to x and y
float x[] = {0.0, 1.0, 2.0};
float y[] = {1.1, 3.5, 2.1};
float a = 0.0, b = 0.0, c = 0.0;
void fit_G( int N_points, float px[], float py[], float* a, float* b, float* c ) {
int i;
float S00, S10, S20, S30, S40, S01, S11, S21;
float denom, x, y;
S00 = S10 = S20 = S30 = S40 = S01 = S11 = S21 = 0;
for (i = 0; i < N_points; i++) {
x = px[i];
y = py[i];
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;
}
void setup() {
Serial.begin(115200);
delay(2000); //wait for connection
Serial.println("Quadratic fit program");
// find a, b, c for curve fit
fit_G( 3, x, y, &a, &b, &c);
Serial.print("a, b, c = ");
Serial.print(a);
Serial.print(", ");
Serial.print(b);
Serial.print(", ");
Serial.println(c);
Serial.println("Check: x, calculated y");
for (int i = 0; i < 3; i++) {
float yc = a * x[i] * x[i] + b * x[i] + c;
Serial.print(x[i]);
Serial.print(", ");
Serial.println(yc);
}
}
void loop() {}
Output from above:
Quadratic fit program
a, b, c = -1.90, 4.30, 1.10
Check: x, calculated y
0.00, 1.10
1.00, 3.50
2.00, 2.10