用 Java 求解积分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3381817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:44:39  来源:igfitidea点击:

Solve an Integral in Java

javamath

提问by dafero

I need to develop a program in Java to solve some integrals. Integrals like this:

我需要用 Java 开发一个程序来解决一些积分问题。像这样的积分:

alt text

替代文字

I've looked for some functions to do this, in java.Math but I didn't find anything.

我在 java.Math 中寻找了一些函数来做到这一点,但我没有找到任何东西。

Has anyone an idea to get a solution for this? (Maybe some extra libraries or something like that).

有没有人想解决这个问题?(也许是一些额外的库或类似的东西)。

采纳答案by adamse

The Wikipedia article on Numerical Integration has a section on methods for one-dimensional integrals.

维基百科关于数值积分的文章有一节介绍一维积分的方法

You should have no problem implementing the "trapezoidal" or "rectangle" rule.

实施“梯形”或“矩形”规则应该没有问题。

回答by Will A

Check out Simpson's Ruleon Wikipedia.

查看维基百科上的辛普森规则

回答by renick

Take a look at JScience

看看JScience

回答by Greg Hewgill

The Apache Commons Mathlibrary contains, in the Numerical Analysissection, four different numerical integrators:

阿帕奇共享数学库包含在数值分析部分,四个不同的数值积分:

  • Romberg's method
  • Simpson's method
  • trapezoid method
  • Legendre-Gauss method
  • 罗姆伯格的方法
  • 辛普森的方法
  • 梯形法
  • 勒让德-高斯方法

回答by jiji

/*------------------------------------------------------------------------------------------------------
 * Small program that numerically calculates an integral according to 
 * Simpson's algorithm. Before executing it, you must enter:
 *  - the expression of the function f: line 12;
 *  - the lower and upper limits b of the integral: lines 39 and 40;
 *  - the number of measurements n (n is integer !!!): line 41.
 *------------------------------------------------------------------------------------------------------*/
// Class function: Defines Simpson's rule
class Function{                                                        

    // Define the function to integrate
    double f (double x) {                                              
       return Math.Cos(x);
    }

    // Simpson's method for integral calculus
    // a = lower bound
    // b = upper bound of integration
    // n = number of passes (higher = less margin of error, but takes longer)
    double IntSimpson(double a, double b,int n){                       
       int i,z;                                                       
       double h,s;                                                    

       n=n+n;
       s = f(a)*f(b);
       h = (b-a)/n;                                        
       z = 4;

       for(i = 1; i<n; i++){
          s = s + z * f(a+i*h);
          z = 6 - z;
       }
       return (s * h)/3;
    } 
}  


class integration{                                                    

    // Class result: calculates the integral and displays the result.
    public static void main(String args[]){
       // Call class function                                           
       Function function;                                   
       function = new Function();

       // ENTER the desired values of a, b and n !!!
       double a = ???? ;                                           
       double b = ???? ;
       int n = ???? ;
       // Applies simpson method to function
       double result = function.IntSimpson(a,b,n);

       // Show results
       System.out.println("Integral is: " + result);        
    }
}