I would use a least squares algorithm like the following that accepts vectors x and y (allows sample size variability).
Assuming inputs std::vector<double> x, y:
.....
if(x.size()!=y.size())
return;
int i, j, k, l, exp, sz = x.size();
double tot, tot2, res, X[3][3], scratch[3][3], Y[3]...