Im currently having a problem with the program i developed to calculate the value of my integral from 0 to infinity for the function 1/((1+x^2)(x^(2/3)))
. my code is as follows:
#include <stdio.h>
#include <math.h>
#include <float.h>
double integral_Function(double x){ //Define a function returning a double, this funtion calculates f(x) for a given x value
double value;
value = 1/((1+ pow(x, 2))*(pow(x, (2/3)))); //pow(x, 2) = x^2
return value;
}
double trapezium_Rule(double lower, double upper, int n){ //Define a function containing an algorithm for the trapezium rule
double h;
double total;
int i;
h = (upper - lower)/n; //h = strip width
total = (h/2) * (integral_Function(upper) + integral_Function(lower)); // Add the two end points
for(i = 1; i < n; i++){
total += (h * integral_Function(lower + (i * h))); //add the volume of each of the middle points to the total
}
return total;
}
int main() {
double sum = 0;
double upper = DBL_EPSILON * 1.001; //DBL_EPSILON is the smallest non-zero value of a double
double lower = DBL_EPSILON;
int accuracy;
printf("Enter the degree of accuracy required: \n");
scanf("%d", &accuracy);
while (upper <= DBL_MAX){
sum += trapezium_Rule(lower, upper, 2000);
lower = upper;
upper = upper * 1.02;
}
printf("The Integral = %.*f to %d decimal places.", accuracy,sum, accuracy);
return 0;
}
In my program the integral is split up into many small chunks using the while loop in int main(), I have specified the number of strips for each integral.
Currently my code produces an answer that is exactly half what it should be, however I cannot see where this problem occurs. It also produces an incorrect value for other functions.