首页 / JAVA / java-如何解决二维积分?
java-如何解决二维积分?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何解决二维积分?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1959字,纯文字阅读大概需要3分钟。
内容图文
![java-如何解决二维积分?](/upload/InfoBanner/zyjiaocheng/656/b65ac8299ea44ccba385217ce33a9c8d.jpg)
我一直在尝试为双积分实现梯形规则.我尝试了许多方法,但无法使其正常工作.
static double f(double x) {
return Math.exp(- x * x / 2);
}
// trapezoid rule
static double trapezoid(double a, double b, int N) {
double h = (b - a) / N;
double sum = 0.5 * h * (f(a) + f(b));
for (int k = 1; k < N; k++)
sum = sum + h * f(a + h*k);
return sum;
}
我了解单个变量积分的方法,但是我不知道如何针对2D积分执行该方法,例如:x(y * y).
有人可以简要解释一下吗?
解决方法:
如果您打算使用梯形规则,则可以这样做:
// The example function you provided.
public double f(double x, double y) {
return x + y * y;
}
/**
* Finds the volume under the surface described by the function f(x, y) for a <= x <= b, c <= y <= d.
* Using xSegs number of segments across the x axis and ySegs number of segments across the y axis.
* @param a The lower bound of x.
* @param b The upper bound of x.
* @param c The lower bound of y.
* @param d The upper bound of y.
* @param xSegs The number of segments in the x axis.
* @param ySegs The number of segments in the y axis.
* @return The volume under f(x, y).
*/
public double trapezoidRule(double a, double b, double c, double d, int xSegs, int ySegs) {
double xSegSize = (b - a) / xSegs; // length of an x segment.
double ySegSize = (d - c) / ySegs; // length of a y segment.
double volume = 0; // volume under the surface.
for (int i = 0; i < xSegs; i++) {
for (int j = 0; j < ySegs; j++) {
double height = f(a + (xSegSize * i), c + (ySegSize * j));
height += f(a + (xSegSize * (i + 1)), c + (ySegSize * j));
height += f(a + (xSegSize * (i + 1)), c + (ySegSize * (j + 1)));
height += f(a + (xSegSize * i), c + (ySegSize * (j + 1)));
height /= 4;
// height is the average value of the corners of the current segment.
// We can use the average value since a box of this height has the same volume as the original segment shape.
// Add the volume of the box to the volume.
volume += xSegSize * ySegSize * height;
}
}
return volume;
}
希望这可以帮助.随时问您关于我的代码的任何问题(警告:该代码未经测试).
内容总结
以上是互联网集市为您收集整理的java-如何解决二维积分?全部内容,希望文章能够帮你解决java-如何解决二维积分?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。