好的,所以数学不是我的强项!
我有这些数据,我使用zunzun.com为我的数据创建一个用户定义的多项式曲线,其曲线为y = a + bx1 + cx2 + dx3 + ex4 + fx5
但是,当我使用建议的代码时:
double a = -4.2294409347240979E+01;
double b = 5.5946102161174718E+00;
double c = -1.3446057748924720E-01;
double d = 1.5448461146899649E-03;
double e = -8.2537158069276241E-06;
double f = 1.7176546321558128E-08;
temp = f;
temp = temp * x_in + e;
temp = temp * x_in + d;
temp = temp * x_in + c;
temp = temp * x_in + b;
temp = temp * x_in + a;
return temp;
它给出了'平方绝对误差之和'?假设X值为94,我应该得到一个大约60.3的值,但这段代码给了我-46.152。我意识到自己很愚蠢,最后显然错过了一个方程式。但是,任何人都可以通过这个论坛帮助我从我的X预测我的Y值吗?该图非常适合它,并且不需要使用查找表就很棒。
非常感谢!
X Y
180 200
178 190
176 180
174 170
170 160
168 150
164 140
160 130
154 120
149 110
142 100
134 90
122 80
110 70
92 60
66 50
30 40
谢谢大家!代码现在正在运行: http://img72.imageshack.us/img72/3705/ps3lrftest.png
答案 0 :(得分:1)
无论临时计算,它都不是y值。但你已经想出了多少:) 这应该做什么?
def y(x, coefficients):
result = 0
xpower = 1
for coeff in coefficients:
result += xpower*coeff
xpower *= x
return result
答案 1 :(得分:1)
请原谅我的四舍五入,但你正在实施;
Y = a + b x + c x ^ 2 + d x ^ 3 + e x ^ 4 + f * x ^ 5
x = 94
x ^ 2 == x x = 8836
x ^ 3 == x x x = 830584
x ^ 4 == x x x x = 78074896
x ^ 5 == x x x x x = 7339040224
A~-42.29440934
B~05.59461021
C~-0.134460577
D~0.001544846
E~ -0.000008253
F~0.000000017
Y = -42.29440934 + 05.59461021 * 94 + -0.134460577 * 8836 + 0.001544846 * 830584 + -0.000008253 * 78074896 + 0.000000017 * 7339040224
Y = -42.29440934 + 525.89335974 - 1188.093658372 + 1283.124370064 - 644.352116688 + 124.763683808
Y = 59.041229212
不完全是你的答案,但要有足够的准确性你应该是好的。
答案 2 :(得分:0)
我不知道您的实现有什么问题,但您提供的代码是正确的。您可以编写(为简单起见使用二次多项式):
y = a + b*x + c*x^2
作为
y = a + (b + (c)*x)*x
或等同于:
temp = c
temp = temp*x + b
temp = temp*x + a
y = temp
您只需将括号内的表达式分配给最内层的表达式并向外扩展。
这与您在上面显示的格式相同。只需使用6个系数而不是3个。有关此多项式评估方法的更多详细信息,您可以阅读维基百科中的Horner's Scheme。
使用Python进行验证:
>>> a = -4.2294409347240979E+01
>>> b = 5.5946102161174718E+00
>>> c = -1.3446057748924720E-01
>>> d = 1.5448461146899649E-03
>>> e = -8.2537158069276241E-06
>>> f = 1.7176546321558128E-08
>>> def test(x):
... t = f
... t = t*x + e
... t = t*x + d
... t = t*x + c
... t = t*x + b
... t = t*x + a
... return t
...
>>> test(94)
60.281114720346885
答案 3 :(得分:0)
以下是我用Java编写的方法:
/**
* Polynomial
* User: Michael
* Date: Jul 27, 2010
* Time: 7:15:41 PM
*/
public class Polynomial
{
private double [] coeff;
public Polynomial(double[] coeff)
{
if ((coeff == null) || (coeff.length == 0))
throw new IllegalArgumentException("coefficient array cannot be null or empty");
this.coeff = new double [coeff.length];
System.arraycopy(coeff, 0, this.coeff, 0, coeff.length);
}
public double eval(double x)
{
int numTerms = coeff.length;
double value = coeff[numTerms-1];
for (int i = (numTerms-2); i >= 0; --i)
{
value += value*x + this.coeff[i];
}
return value;
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder(128);
for (int i = 0; i < (this.coeff.length-1); ++i)
{
builder.append("a[").append(i).append("]=").append(this.coeff[i]).append(",");
}
builder.append("a[").append(this.coeff.length-1).append("]=").append(this.coeff[this.coeff.length-1]);
return builder.toString();
}
}
以下是我测试的方法:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* PolynomialTest
* User: Michael
* Date: Jul 27, 2010
* Time: 7:26:15 PM
*/
public class PolynomialTest
{
private static final double [] a =
{
-4.2294409347240979E+01,
5.5946102161174718E+00,
-1.3446057748924720E-01,
1.5448461146899649E-03,
-8.2537158069276241E-06,
1.7176546321558128E-08,
};
private Polynomial p = new Polynomial(a);
@Test
public void testEval()
{
double x = 94.0;
double expected = 60.83781703621137;
double actual = p.eval(x);
assertEquals(expected, actual, 1.0e-3);
}
@Test
public void testToString()
{
String expected = "a[0]=-42.29440934724098,a[1]=5.594610216117472,a[2]=-0.1344605774892472,a[3]=0.0015448461146899649,a[4]=-8.253715806927624E-6,a[5]=1.7176546321558128E-8";
String actual = p.toString();
assertEquals(expected, actual);
}
}
答案 4 :(得分:0)
双曲线方程通常适合这些数据 好。我的个人建议是使用
“简单公式21”
http://zunzun.com/Equation/2/Simple/Simple%20Equation%2021/
而不是多项式。但是,如果你有所作为,没有特别的理由改变它。
詹姆斯菲利普斯 zunzun@zunzun.com