Hi, I'm starting to learn C by self-study. I can't figure out how this function doesn't work out properly. It doesn't return the correct answer. It calculates the numerical integration of f(x) = 1/(1+x)^2 over the interval [a,b]. Could you pls help?
Code:
#include<stdio.h>
double f_int(double a, double b, double n); /*Declarations*/
int main(void)
{
double a, b, n;
printf("This function calculates the integration from a to b of ");
printf("the function f(x) = 1/(1+x)^2\n");
printf("Enter left endpoint: ");
scanf("%lf", &a);
printf("Enter right endpoint: ");
scanf("%lf", &b);
printf("Enter number of partition intervals: ");
scanf("%lf", &n);
printf("The numerical result of this integration is %lf.\n", f_int(a, b, n));
return 0;
}
double f_int(double a, double b, double n) /*Definitions*/
{
double h, xi, I_midpoint = 0.0f;
int i;
h = (b - a) / n;
for(i = 1; i <= n; i++)
{
xi = a + (i - 0.5) * h;
I_midpoint = I_midpoint + 1.0 / (1.0 + xi) * (1.0 + xi);
}
I_midpoint = h * I_midpoint;
return I_midpoint;
}