英文:
Simpson's double integral won't work in java
问题
[![这是一个指南的图片][1]][1][![图片2][2]][2]所以我不得不为辛普森双重积分编写一个算法,以便我可以更快地找到答案。我有一个指南,显示了编写此程序的步骤。在按照这个指南操作并在netbeans中运行后,我发现程序输出的值与真实答案并不非常接近。以下是我在Java中编写的代码:
//输入
double a = 0,b = 1; // 端点
int m = 8,n = 4;
double K1 = 0,K2 = 0,K3 = 0;
//输出
//步骤 1
double h = (b - a) / n;
double j1 = 0; //末项
double j2 = 0; //偶数项
double j3 = 0; //奇数项
//步骤 2
for(int i = 0; i <= n; i++){
//步骤 3
double x = a + (i * h);
double hX = (d(x) - c(x)) / m;
K1 = f(x, c(x)) + f(x, d(x)); // 末项
//步骤 4
for (int j = 1; j < (m-1); j++){
//步骤 5
double y = c(x) + (j * hX);
double q = f(x,y);
//步骤 6
if (j % 2 == 0){
K2 = K2 + q;
}
else
K3 = K3 + q;
}
//步骤 7
double l = (K1 + (2*K2) + (4*K3)) * (hX / 3);
//步骤 8
if (i == 0 || i == n)
j1 = j1 + l;
else if (i % 2 == 0)
j2 = j2 + l;
else
j3 = j3 + l;
}
double j = h * (j1 + (2 * j2) + (4 * j3)) / 3;
System.out.println("j = " + j);
}
public static double c(double x){
return x;
}
public static double d(double x){
return 2 * x;
}
public static double f(double x, double y){
return (Math.pow(y, 2) + Math.pow(x, 3));
}
我尝试了多次调试程序,但尚未找到错误的原因。如果您在我的代码中发现任何错误,请告诉我,看看是否可以修复。对于给定的示例,我得到的值是0.9069281684027777,而不是正确的值0.7838542。谢谢您的帮助。您还可以查看我所遵循的指南,以便能够创建这个程序。
[1]: https://i.stack.imgur.com/Yr002.png
[2]: https://i.stack.imgur.com/L3mf3.png
英文:
So I had to write an algorithm for the Simpson's double integral so I can find the answer in a much faster manner. I had a guide that showed the steps to follow to write this program. After following this guide and running it in netbeans, I found out that the values coming out of the program where not really close to the real answer. This is the code that I written in java:
//INPUT
double a = 0,b = 1; // Endpoints
int m = 8,n = 4;
double K1 = 0, K2 = 0, K3 = 0;
//OUPUT
//Step 1
double h = (b - a) / n;
double j1 = 0; //End terms
double j2 = 0; //Even terms
double j3 = 0; //Odd terms
//Step 2
for(int i = 0; i <= n; i++){
//Step 3
double x = a + (i * h);
double hX = (d(x) - c(x)) / m;
K1 = f(x, c(x)) + f(x, d(x)); // End terms
//Step 4
for (int j = 1; j < (m-1); j++){
//Step 5
double y = c(x) + (j * hX);
double q = f(x,y);
//Step 6
if (j % 2 == 0){
K2 = K2 + q;
}
else
K3 = K3 + q;
}
//Step 7
double l = (K1 + (2*K2) + (4*K3)) * (hX / 3);
//Step 8
if (i == 0 || i == n)
j1 = j1 + l;
else if (i % 2 == 0)
j2 = j2 + l;
else
j3 = j3 + l;
}
double j = h * (j1 + (2 * j2) + (4 * j3)) / 3;
System.out.println("j = " + j);
}
public static double c(double x){
return x;
}
public static double d(double x){
return 2 * x;
}
public static double f(double x, double y){
return (Math.pow(y, 2) + Math.pow(x, 3));
}
I tried debugging the program several times but I haven't yet found why I am encountering this mistake. If there's any mistake that you find in my code please let me know to see if it fixes it. For the given example, I am getting the value of 0.9069281684027777 instead of having the correct value which is 0.7838542. Thank you for your help. You can also see the guide that I followed to be able to create this program.
答案1
得分: 0
我没有检查数学问题,巨大的误差似乎表明算法实现中存在错误。循环边界值是可疑的。并且存在浮点误差。
与其将分数乘以递增索引(这将会放大分数中的浮点近似误差),不如这样做:
改为:
double h = (b - a) / n;
for (int i = 0; i < n; i++) {
double x = a + i * h;
或者
for (int i = 0; i < n; i++) {
double x = a + i * (b - a) / n;
或者
for (int i = 0; i <= n; i++) {
double x = a + i * (b - a) / (n + 1);
关于边界值 n,我有一些不太清楚。
英文:
I did not check the math, the large error seems to indicate an error in the algorithm implemented. The for-bounds are dubious. And floating point errors exist.
Instead of multiplying a fraction by a running index (which would multiply the floating point approximation error in the fraction), better do:
Instead:
double h = (b - a) / n;
for (int i = 0; i <= n; i++) {
double x = a + (i * h);
do
for (int i = 0; i < n; i++) {
double x = a + i * (b - a) / n;
or
for (int i = 0; i <= n; i++) {
double x = a + i * (b - a) / (n + 1);
The boundary n being a bit unclear to me.
专注分享java语言的经验与见解,让所有开发者获益!
评论