定積分をより適切に計算するにはどうすればよいですか? 関数を使用して統合し、別の関数を使用して階乗を再帰的に見つけています。
アルゴリズムや効率、さらには精度を向上させたいと思っています。
public static double testStatistic(double meanTreatmentSumOfSquares, double meanErrorSumOfSquares)
{
return (meanTreatmentSumOfSquares / meanErrorSumOfSquares);
}
public static double pValue(double fStatistic, int degreeNum, int degreeDenom)
{
double pValue = 0;
pValue = integrate(0, fStatistic, degreeNum, degreeDenom);
return pValue;
}
public static double integrate(double start, double end, int degreeFreedomT, int degreeFreedomE)
{
int iterations = 100000;
double x, dist, sum = 0, sumT = 0;
dist = (end - start) / iterations;
for (int i = 1; i <= iterations; i++)
{
x = start + i * dist;
sumT += integralFunction(x - dist / 2, degreeFreedomT, degreeFreedomE);
if (i < iterations)
{
sum += integralFunction(x, degreeFreedomT, degreeFreedomE);
}
}
sum = (dist / 6) * (integralFunction(start, degreeFreedomT, degreeFreedomE) + integralFunction(end, degreeFreedomT, degreeFreedomE) + 2 * sum + 4 * sumT);
return sum;
}
public static double integralFunction(double x, int degreeFreedomT, int degreeFreedomE)
{
double temp=0;
temp = ((Math.Pow(degreeFreedomE, degreeFreedomE / 2) * Math.Pow(degreeFreedomT, degreeFreedomT / 2)) / (factorial(degreeFreedomE / 2 - 1) * factorial(degreeFreedomT / 2 - 1))) * (factorial(((degreeFreedomT + degreeFreedomE) / 2 - 1)))*((Math.Pow(x, degreeFreedomE / 2 - 1)) / (Math.Pow((degreeFreedomT + degreeFreedomE * x), ((degreeFreedomE + degreeFreedomT) / 2))));
return temp;
}
public static double factorial(double n)
{
if (n == 0)
{
return 1.0;
}
else
{
return n * factorial(n - 1);
}
}
}
}