2

I would like to compute the coefficients a0, a1, and a2 of a quadradic function (polynomial of degree 2) given three points (x0, y0), (x1, y1), and (x2, y2), where yi = a0 + a1*xi + a2*xi*xi?

I have tried the following two formulas, but I am not really impressed with the precision of the output

final double x0mx1, x0mx2, x1mx2, t0, t1, t2;
double a2, a1, a0;

x0mx1 = (x0 - x1);
x0mx2 = (x0 - x2);
x1mx2 = (x1 - x2);


// method one
t0 = (y0 / (x0mx1 * x0mx2));
t1 = (y1 / (-x0mx1 * x1mx2));
t2 = (y2 / (x0mx2 * x1mx2));

a2 = (t0 + t1 + t2);
a1 = -((t0 * (x1 + x2)) + (t1 * (x0 + x2)) + (t2 * (x0 + x1)));
a0 = (t0 * x1 * x2) + (t1 * x0 * x2) + (t2 * x0 * x1);

// method two
a2 = ((((y1 - y0) * (x0mx2)) + ((y2 - y0) * ((-x0mx1)))) /
                (((x0mx2) * ((x1 * x1) - (x0 * x0)))
                + (((-x0mx1)) * ((x2 * x2) - (x0 * x0)))));
a1 = (((y1 - y0) - (a2 * ((x1 * x1) - (x0 * x0)))) / ((-x0mx1)));
a0 = y0 - (a2 * x0 * x0) - (a1 * x0);

The results do sort of fit, i.e., seem roughly to be within a +/- 1e-5 * max{ |a0'|, |a1'|, |a2'| } window of the real solution a0', a1', and a2'.

Is there a better, more numerically stable way to compute the coefficients?

I am using Java, btw, although I think this does not matter.

Cheers, Thomas.

Thomas Weise
  • 389
  • 6
  • 13
  • Since I got no answer so far, what I do is this: I use both formulas and compute their results for all six permutations of the input point. I pick the result leading to the smallest sum of absolute errors. It sometimes seems to come from method one, sometimes from method two. This is somewhat unsatisfying, but seems to work reasonably well. – Thomas Weise Nov 01 '15 at 21:29
  • 1
    Have a look at this paper: https://www.inf.ethz.ch/personal/gander/papers/changing.pdf it outlines various options for interpolating polynomials and how to get from them to a monomial basis. Do you need a monomial representation? This book chapter on interpolations might also be helpful: http://math.iit.edu/~fass/578_ch6.pdf – haraldkl Nov 02 '15 at 03:23
  • Yes, I want to get a monomial base of a quadratic function from three points, i.e., do a fit of `y=f(x)=a0+a1*x+a2*x^2` to the three points `(x0,y0)`, `(x1,y1)`, and `(x2,y2)`. The basic maths behind it is clear, also thanks to your links (Thanks!). I am wondering whether there are numerical issues, whether one formula is better than another one. For instance, I gave two example methods for the transformation. Due to numerical issues, they sometimes may return slightly different results (I have tested this). I wonder whether there is "the right way" of doing it from a numerical perspective. – Thomas Weise Nov 02 '15 at 07:41
  • 1
    It might be more numerically stable to use a different basis for the interpolation and then do the transformation to monomial basis. Thus, following Gander's Paper you could, for example, try Lagrangian interpolation and then transform the result to monomials. I believe, there is no optimal solution that fits all possible points and the most accurate approach will always depend on your three points. – haraldkl Nov 02 '15 at 08:37

0 Answers0