本文介绍了在Java中查找二次回归曲线的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三组数据,例如:

x   y
4   0
6   60
8   0

有谁知道任何(有效的)Java代码可以让我回来a,b和c的值(系数)?

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

推荐答案

我假设你想要这种形式的公式:

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

在您的示例中:

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

要根据x1,x2,x3,y1,y2和y3得到系数a,b,c,你只需要乘以fo rmula out然后收集条款。这并不困难,它会运行得非常快,但输入的代码会相当多。最好找一个已经为你做的包,但是如果你想自己做,这个你是怎么做到的。

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.

你的例子中两个y项为零的事实使公式更加简单,你可以利用那个。但如果这只是巧合而不是一般规则,那么你需要完整的公式。

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.

这篇关于在Java中查找二次回归曲线的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 07:53