java辛普生公式_Java – Simpson的方法和错误

我正在为Simpson的方法编写一个Java程序.基本程序按预期工作,但我不能让(绝对)错误部分工作.

我想我需要以不同方式引用我的while(absError< 0.000001)循环.我究竟做错了什么?

第一次尝试

public static double function(double x, double s) {

double sech = 1 / Math.cosh(x); // Hyperbolic cosecant

double squared = Math.pow(sech, 2);

return ((Math.pow(x, s)) * squared);

}

// Simpson's rule - Approximates the definite integral of f from a to b.

public static double SimpsonsRule(double a, double b, double s, int n) {

double dx, x, sum4x, sum2x;

double absError = 1.0;

double simpson = 0.0;

double simpson2 = 0.0;

dx = (b-a) / n;

sum4x = 0.0;

sum2x = 0.0;

// 4/3 terms

for (int i = 1; i < n; i += 2) {

x = a + i * dx;

sum4x += function(x,s);

}

// 2/3 terms

for (int i = 2; i < n-1; i += 2) {

x = a + i * dx;

sum2x += function(x,s);

}

// Compute the integral approximation.

simpson = function(a,s) + function(a,b);

simpson = (dx / 3)*(simpson + 4 * sum4x + 2 * sum2x);

while ( absError < 0.000001)

{

simpson2 = SimpsonsRule(a, b, s, n);

absError = Math.abs(simpson2 - simpson) / 15;

simpson = simpson2;

n++;

}

System.out.println("Number of intervals is " + n + ".");

return simpson2;

}

这不起作用,因为我没有写

simpson2 = SimpsonsRule(a, b, s, n);

正确.

我尝试了第二种方式,但解决方案最终也失败了.

public static double function(double x, double s) {

double sech = 1 / Math.cosh(x); // Hyperbolic cosecant

double squared = Math.pow(sech, 2);

return ((Math.pow(x, s)) * squared);

}

// Simpson's rule - Approximates the definite integral of f from a to b.

public static double SimpsonsRule(double a, double b, double s, int n) {

double dx, x, sum4x, sum2x;

double absError = 1.0;

double simpson = 0.0;

double simpson2 = 0.0;

dx = (b-a) / n;

sum4x = 0.0;

sum2x = 0.0;

// 4/3 terms

for (int i = 1; i < n; i += 2) {

x = a + i * dx;

sum4x += function(x,s);

}

// 2/3 terms

for (int i = 2; i < n-1; i += 2) {

x = a + i * dx;

sum2x += function(x,s);

}

// Compute the integral approximation.

simpson = function(a,s) + function(a,b);

simpson = (dx / 3)*(simpson + 4 * sum4x + 2 * sum2x);

while ( absError < 0.000001)

{

n++;

dx = (b-a) / n;

// 4/3 terms

for (int i = 1; i < n; i += 2) {

x = a + i * dx;

sum4x += function(x,s);

}

// 2/3 terms

for (int i = 2; i < n-1; i += 2) {

x = a + i * dx;

sum2x += function(x,s);

}

simpson = function(a,s) + function(a,b);

simpson2 = (dx / 3)*(simpson + 4 * sum4x + 2 * sum2x);

absError = Math.abs(simpson2 - simpson) / 15;

simpson = simpson2;

}

System.out.println("Number of intervals is " + n + ".");

return simpson2;

}

我需要以不同的方式编写while循环.在while循环中引用错误的方式有什么问题?

直到java代码

while ( absError < 0.000001)

{

simpson2 = SimpsonsRule(a, b, s, n);

absError = Math.abs(simpson2 - simpson) / 15;

simpson = simpson2;

n++;

}

System.out.println("Number of intervals is " + n + ".");

return simpson2;

工作正常,正确计算辛普森的方法.

解决方法:

看起来你的Simpson的方法实现没有收敛.你可以做的最简单的事情是避免无限循环 – 你必须添加另一个条件 – 最大迭代次数.

像这样的东西:

int n = 0;

while (error < ACCURACY && n++ < MAX_ITERATIONS) {

// while body

}

其中ACCURACY为0.000001(或1e-6),MAX_ITERATIONS为整数常量,例如100000或1e 6.

为什么你的算法没有收敛 – 这是另一个问题 – 仔细研究你的公式 – 使用调试工具.祝好运!

标签:java,numerical-methods,estimation

来源: https://codeday.me/bug/20190623/1273615.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值