numerical integration - midpoint / trapezoidal / simpson's rule

50 Views Asked by At

I'm not sure if this is the right place to post this as it is a coding problem. Since it is part of my numerical analysis course however, I'll try it here.

Let $I(f) = \int_{0}^{1} f(x) dx$.

Code the composed midpoint rule: $I(f) \approx Q_M^n(f) = \frac{1}{n}\sum_{i=1}^n f_i$ $\quad$ ,with $f_i = f(\frac{2i-1}{2n})$

Input: $f,n$ and exact value of $I(n)$.

Output:$Q_M^n(f)$ and error $e_n(f)$

The exercise consists of more subtasks but I think I can do them if I understand this correctly.

The reason why I'm posting this here is that usually when looking through my script and this forum, most of the questions have a fixed function and interval and ask for a good approximation of the integral. However, this one seems to approach it from the other side, as the input is already the exact value.

For the error $e_n(f)$ , I think the way to solve this is by approximating the integral with $Q_M^n(f)$ and then taking $\lvert I(f) - Q_M^n(f)\rvert$ , which shows us by how much the exact solution and the approxiamtion differ.

For $Q_M^n(f):$ This is where I don't quite know how to approach the problem. Given $n$ and $f$, I'm struggling to implement it in a way where I can calculate $f(\frac{2i-1}{2n})$.