更高效的集成回路 [英] More efficient Integration Loop
问题描述
public double Integral(double[] x, double intPointOne, double intPointTwo)
{
double integral = 0;
double i = intPointOne;
do
{
integral += Function(x[i])*.001;
i = i + .001;
}
while (i <= intPointTwo);
return integral;
}
下面是简单地用一个函数我一定要整合来自X1-X2功能零件的总和。我怎么可以让这个循环更高效(使用更少的循环),但更准确呢?
Here's a function I have to integrate a function from x1-x2 simply using a summation of parts. How can I make this loop more efficient (using less loops), but more accurate?
的其中,功能
变化,每一次迭代中,但它应该是无关紧要的,因为它的幅度(或边界秩序)应该保持相对同... 的
Where Function
changes every iteration, but it should be irrelevant as it's order of magnitude (or boundary) should stay relatively the same...
推荐答案
1)考虑部分的http://apps.nrbook.com/c/index.html 以不同的算法。
1) look into section 4.3 of http://apps.nrbook.com/c/index.html for a different algorithm.
2)要控制你可能需要指定范围的准确度/速度系数 x_low
和 x_high
以及你想在积分多少片。所以,你的函数看起来像这样
2) To control the accuracy/speed factor you may need to specify the bounds x_low
and x_high
as well as how many slices you want in the integral. So your function would look like this
// Integrate function f(x) using the trapezoidal rule between x=x_low..x_high
double Integrate(Func<double,double> f, double x_low, double x_high, int N_steps)
{
double h = (x_high-x_low)/N_steps;
double res = (f(x_low)+f(x_high))/2;
for(int i=1; i < N; i++)
{
res += f(x_low+i*h);
}
return h*res;
}
一旦你明白了这个基本的整合,你可以移动到更周密的计划提到在数值Recipies和其他来源。
Once you understand this basic integration, you can move on to more elaborate schemes mentioned in Numerical Recipies and other sources.
要使用此代码的问题,比如命令A =整合(Math.Sin,0,Math.PI, 1440);
To use this code issue a command like A = Integrate( Math.Sin, 0, Math.PI, 1440 );
这篇关于更高效的集成回路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!