In this task, you will write a function that performs numerical integration and
ID: 3545607 • Letter: I
Question
In this task, you will write a function that performs numerical integration and a main()
function that tests it. The prototype for the function that performs numerical integration is
double numericalIntegration(double array[], int numElements, double deltaX);
and should be placed in a file called numericalIntegration.cpp. The argument array
contains the f(x) data, which is presumed to be sampled at deltaX intervals.
Place the main() function in testNumericalIntegration.cpp. Use readDoubleArray() and
printDoubleArray() from the previous task to perform input and output of the array. Allow
at least 100 elements. Additionally, print out the result of calling numericalIntegration()
with a deltaX of 0.5. Shown below is an example run using the velocity data from the
assignment concerning projectiles. Note that the distance (integrating velocity yields
distance) compares well with the height calculated in the projectile assignment.
Note that numericalIntegration () should behave properly even when there are only two
data points. If there are fewer than two data points, it should return zero.
The input is: : g++ -Wall arrayFunctions.cpp numericalIntegration.cpp testNumericalIntegration.cpp :a 62.50 57.59 52.69 47.78 42.88 37.97 33.07 28.16 23.26 18.35 13.45 8.55 3.64 -1.27 ^Z
[62.5, 57.59, 52.69, 47.78, 42.88, 37.97, 33.07, 28.16, 23.26, 18.35, 13.45, 8.55, 3.64, -1.27]
Integration yields 199.002
Explanation / Answer
The program is
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);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.