利用java求积分(定积分和无穷限积分)
生活随笔
收集整理的這篇文章主要介紹了
利用java求积分(定积分和无穷限积分)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
【0】README
0.1)本文部分文字描述轉(zhuǎn)自或譯自 https://en.wikipedia.org/wiki/Simpson%27s_rule和?https://en.wikipedia.org/wiki/Numerical_integration#Methods_for_one-dimensional_integrals;旨在利用java求積分;(定積分和無窮限積分)
0.2)you can also refer to this link for source code:?https://github.com/pacosonTang/postgraduate-research/tree/master/integration
0.3)o m g. CSDN編輯器掉鏈子,無法正常顯示source code, 大家湊合著看吧。oh.
【1】求定積分 1)intro:由 wikepedia 上關(guān)于 辛普森法的intro 以及 《高等數(shù)學(xué)第6版上冊(cè)同濟(jì)版》p229 關(guān)于定積分的近似計(jì)算中提到的辛普森法,本文求定積分的方法采用了辛普森近似法; 2)下面引用《高等數(shù)學(xué)第6版上冊(cè)同濟(jì)版》p229?關(guān)于辛普森法的描述
3)計(jì)算函數(shù)定積分的源代碼如下: // compute the numeric integration. public class Integration {public Integration(){}// apply simpson rule to approximately compute the integration. public double simpsonRule(double upper, double lower, int n, Function df) {double result = 0;double unit = (upper-lower)/n;double factor1 = unit / 3;double[] x = new double[n+1];for (int i = 0; i < x.length; i++) {x[i] = lower + unit*i;}for (int i = 0; i < x.length; i++) {if(i==0 || i==x.length-1) {result += df.fun(x[i]);}else if(i%2 == 0) { // if i is even num.result += 2*df.fun(x[i]);}else { // if i is odd num. result += 4*df.fun(x[i]);}} result *= factor1;return result;}// compute the standard normal distribution integration// refer to the integration table in p382 of "probability and statistics" from ZheJiang University.public double stdGaussValue(double realUpper) {Integration integration = new Integration();double upper = 1.0;double lower = 0.0;int n = 200; // splited into 200 subintervals.// double realUpper = 0.03;if(realUpper >= 5.0) {return 1.0;}double result = integration.simpsonRule(upper, lower, n, new Function() {@Overridepublic double fun(double x) {if(x==0) {return 0;}double t = realUpper-(1-x)/x;return Math.pow(Math.E, -0.5*t*t) / (x*x); }});result /= Math.pow(2*Math.PI, 0.5);result = new BigDecimal(result).setScale(6, RoundingMode.HALF_UP).doubleValue(); // save 6 decimal places.return result;} } public class IntegrationTest { //test case.public static void main(String[] args) {Integration integration = new Integration();double result = integration.stdGaussValue(4.42);System.out.println(result);}public static void main3(String[] args) {Integration integration = new Integration();double upper = 1.0;double lower = 0.0;int n = 50;double realUpper = 0.39;double result = integration.simpsonRule(upper, lower, n, new Function() {@Overridepublic double fun(double x) {if(x==0) {return 0;}double t = realUpper-(1-x)/x;return Math.pow(Math.E, -0.5*t*t) / (x*x); }});result /= Math.pow(2*Math.PI, 0.5);result = new BigDecimal(result).setScale(4, RoundingMode.HALF_UP).doubleValue(); System.out.println(result);}public static void main2(String[] args) {Integration integration = new Integration();double upper = 1.0;double lower = 0.0;int n = 10;double result = integration.simpsonRule(upper, lower, n, new Function() {@Overridepublic double fun(double x) {return Math.pow(Math.E, -x*x/2); }});result /= Math.pow(2*Math.PI, 0.5);System.out.println(result);BigDecimal decimal = new BigDecimal(result).setScale(4, RoundingMode.HALF_UP);result = Double.valueOf(decimal.toString());System.out.println(result);}public static void main1(String[] args) {Integration integration = new Integration();double upper = 1.0;double lower = 0;int n = 10;double result = integration.simpsonRule(upper, lower, n, new Function() {@Overridepublic double fun(double x) {return 4 / (1+Math.pow(x,2.0)); }});System.out.println(result);} }
Attention) A1)以上測試用例中涉及到的積分函數(shù)來自?《高等數(shù)學(xué)第6版上冊(cè)同濟(jì)版》p230的例2; A2)定積分表達(dá)式為
【2】求無窮限積分(本文以求標(biāo)準(zhǔn)正態(tài)分布的無窮下限反常積分為例) 1)求無窮限積分是基于定積分的;如何求定積分,本文在章節(jié)【1】中已經(jīng)講了;
2)所以標(biāo)準(zhǔn)正態(tài)分布的無窮下限反常積分函數(shù)可轉(zhuǎn)化為:
3)計(jì)算標(biāo)準(zhǔn)正態(tài)分布無窮下限積分的測試用例如上所示。 Attention) A1)上述求標(biāo)準(zhǔn)正態(tài)分布無窮下限積分的代碼對(duì)realUpper 有要求,小于等于5.0;因?yàn)楫?dāng)realUpper>5的話,其value=1了; A2)需要求標(biāo)準(zhǔn)正態(tài)分布的下限積分時(shí),強(qiáng)烈建議使用?integration.stdGaussValue() 其精度要高些。
【1】求定積分 1)intro:由 wikepedia 上關(guān)于 辛普森法的intro 以及 《高等數(shù)學(xué)第6版上冊(cè)同濟(jì)版》p229 關(guān)于定積分的近似計(jì)算中提到的辛普森法,本文求定積分的方法采用了辛普森近似法; 2)下面引用《高等數(shù)學(xué)第6版上冊(cè)同濟(jì)版》p229?關(guān)于辛普森法的描述
3)計(jì)算函數(shù)定積分的源代碼如下: // compute the numeric integration. public class Integration {public Integration(){}// apply simpson rule to approximately compute the integration. public double simpsonRule(double upper, double lower, int n, Function df) {double result = 0;double unit = (upper-lower)/n;double factor1 = unit / 3;double[] x = new double[n+1];for (int i = 0; i < x.length; i++) {x[i] = lower + unit*i;}for (int i = 0; i < x.length; i++) {if(i==0 || i==x.length-1) {result += df.fun(x[i]);}else if(i%2 == 0) { // if i is even num.result += 2*df.fun(x[i]);}else { // if i is odd num. result += 4*df.fun(x[i]);}} result *= factor1;return result;}// compute the standard normal distribution integration// refer to the integration table in p382 of "probability and statistics" from ZheJiang University.public double stdGaussValue(double realUpper) {Integration integration = new Integration();double upper = 1.0;double lower = 0.0;int n = 200; // splited into 200 subintervals.// double realUpper = 0.03;if(realUpper >= 5.0) {return 1.0;}double result = integration.simpsonRule(upper, lower, n, new Function() {@Overridepublic double fun(double x) {if(x==0) {return 0;}double t = realUpper-(1-x)/x;return Math.pow(Math.E, -0.5*t*t) / (x*x); }});result /= Math.pow(2*Math.PI, 0.5);result = new BigDecimal(result).setScale(6, RoundingMode.HALF_UP).doubleValue(); // save 6 decimal places.return result;} } public class IntegrationTest { //test case.public static void main(String[] args) {Integration integration = new Integration();double result = integration.stdGaussValue(4.42);System.out.println(result);}public static void main3(String[] args) {Integration integration = new Integration();double upper = 1.0;double lower = 0.0;int n = 50;double realUpper = 0.39;double result = integration.simpsonRule(upper, lower, n, new Function() {@Overridepublic double fun(double x) {if(x==0) {return 0;}double t = realUpper-(1-x)/x;return Math.pow(Math.E, -0.5*t*t) / (x*x); }});result /= Math.pow(2*Math.PI, 0.5);result = new BigDecimal(result).setScale(4, RoundingMode.HALF_UP).doubleValue(); System.out.println(result);}public static void main2(String[] args) {Integration integration = new Integration();double upper = 1.0;double lower = 0.0;int n = 10;double result = integration.simpsonRule(upper, lower, n, new Function() {@Overridepublic double fun(double x) {return Math.pow(Math.E, -x*x/2); }});result /= Math.pow(2*Math.PI, 0.5);System.out.println(result);BigDecimal decimal = new BigDecimal(result).setScale(4, RoundingMode.HALF_UP);result = Double.valueOf(decimal.toString());System.out.println(result);}public static void main1(String[] args) {Integration integration = new Integration();double upper = 1.0;double lower = 0;int n = 10;double result = integration.simpsonRule(upper, lower, n, new Function() {@Overridepublic double fun(double x) {return 4 / (1+Math.pow(x,2.0)); }});System.out.println(result);} }
Attention) A1)以上測試用例中涉及到的積分函數(shù)來自?《高等數(shù)學(xué)第6版上冊(cè)同濟(jì)版》p230的例2; A2)定積分表達(dá)式為
【2】求無窮限積分(本文以求標(biāo)準(zhǔn)正態(tài)分布的無窮下限反常積分為例) 1)求無窮限積分是基于定積分的;如何求定積分,本文在章節(jié)【1】中已經(jīng)講了;
2)所以標(biāo)準(zhǔn)正態(tài)分布的無窮下限反常積分函數(shù)可轉(zhuǎn)化為:
3)計(jì)算標(biāo)準(zhǔn)正態(tài)分布無窮下限積分的測試用例如上所示。 Attention) A1)上述求標(biāo)準(zhǔn)正態(tài)分布無窮下限積分的代碼對(duì)realUpper 有要求,小于等于5.0;因?yàn)楫?dāng)realUpper>5的話,其value=1了; A2)需要求標(biāo)準(zhǔn)正態(tài)分布的下限積分時(shí),強(qiáng)烈建議使用?integration.stdGaussValue() 其精度要高些。
總結(jié)
以上是生活随笔為你收集整理的利用java求积分(定积分和无穷限积分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL事务管理+安全管理+MySQL
- 下一篇: 一键超频上5GHz一键超频有用吗