My integral:
\int_{-1}^{1} \frac{1}{1+x^4}dx
I was asked to estimate using a midpoint sum. But first I had to find the minimum value of n for an error of less than 0.001. Here’s the error formula for a midpoint sum:
\text{error} \leq M_2\frac{(b-a)^3}{24n^2}.
I found the second derivative of the function inside my integral:
32x^6(1 + x^4)^{-3}
Which looked like this in Desmos:
I thus assigned M_2 a value of 4.
I plugged in the appropriate values and solved for n:
0.001 \le 4*\frac{2^3}{24n^2}
n^2 \ge 4*\frac{2^3}{24*0.001}
n \ge \sqrt{4*\frac{2^3}{24*0.001}}
n \ge 36.51 \ \ \ \ \ \ \ n = 37
An n value of 37 certainly gets me well within 0.001 of the value the Prof. McClure’s numerical integrator gives, but I also got within 0.001 with n values as low as 19. I’m not sure if this means I did the error calculation wrong, or if it means the formula sometimes gives an n value higher than necessary for the given error.
Anyway, I coded my homemade numerical integrator for this problem in C++. Check out the code:
#include <iostream>
using namespace std;
inline double toTheFourthPower(double x){
x *= x;
x *= x;
return x;
}
int main(){
cout << “What value of n do you want?” << endl;
int n;
cin >> n;
double deltaX = 2.0/n;
double midpoint = -1 + deltaX/2;
double sum = 0;
for (int i = 0; i < n; i++){
sum += 1/(1 + toTheFourthPower(midpoint));
midpoint += deltaX;
}
sum *= deltaX;
cout << "Here is my estimate: " << sum << endl;
return 0;
}