Best way to find Quadratic Regression Curve in Java

前端 未结 3 1560
春和景丽
春和景丽 2021-01-23 00:20

I\'ve three sets of data such as:

x   y
4   0
6   60
8   0

Does anyone know any (efficient) Java codes that can give me back the values of a, b

3条回答
  •  独厮守ぢ
    2021-01-23 00:57

    I assume you want the formula in this form:

    y = a * x^2 + b*x + c
    

    If you have only three points you can describe the quadratic curve that goes through all three points with the formula:

    y = ((x-x2) * (x-x3)) / ((x1-x2) * (x1-x3)) * y1 +
        ((x-x1) * (x-x3)) / ((x2-x1) * (x2-x3)) * y2 +
        ((x-x1) * (x-x2)) / ((x3-x1) * (x3-x2)) * y3
    

    In your example:

    x1 = 4, y1 = 0, x2 = 6, y2 = 60, x3 = 8, y3 = 0
    

    To get the coefficients a, b, c in terms of x1, x2, x3, y1, y2 and y3 you just need to multiply the formula out and then collect the terms. It's not difficult, and it will run very fast but it will be quite a lot of code to type in. It would probably be better to look for a package that already does this for you, but if you want to do it yourself, this is how you could do it.

    The fact that two of the y terms are zero in your example makes the formula a lot simpler, and you might be able to take advantage of that. But if that was just a coincidence and not a general rule, then you need the full formula.

提交回复
热议问题