Approximating Average of Squares - Square of Average using Trapezoidal Rule

17 Views Asked by At

First, the simple problem: I have a set of points $\{(x_i, y_i)\}$ and want to approximate the average y-value, $<y> = \frac{1}{x_n - x_1} \int{y dx}$. Here I decided to use the trapezoidal rule to get a numerical approximation for the integral.

Okay, so now the slightly harder problem: I have the same set of points, but I want to calculate $<y^2> - <y>^2$. I already have $<y>$ so $<y>^2$ is no problem. I want to calculate $<y^2> = \frac{1}{x_n-x_1} \int{y^2 dx}$.

If we imagine the curve $y$ to be made of trapezoids, then the curve $y^2$ will be made of connected parabola segments.

My first question: would it make sense to approximate $<y^2>$ by calculating the area under these segments?

I worked out the math as follows. (Please double check to make sure I didn't make any mistakes):

Imagine a line segment between two adjacent points in the set: $(x_1, y_1)$ and $(x_2, y_2)$.

We get the equation $y = m(x - x_1) + y_1$, where $m = \frac{y_2-y_1}{x_2-x_1} = \frac{\Delta y}{\Delta x}$.

$$\int_{x_1}^{x_2}{y^2 \cdot dx} = \int_{x_1}^{x_2}{(m(x - x_1) + y_1) \cdot dx}$$ $$ = \int_{x_1}^{x_2}{(m x - m x_1 + y_1) \cdot dx} $$ $$ = \int_{x_1}^{x_2}{m x \cdot dx} + \int_{x_1}^{x_2}{(-m x_1 + y_1) \cdot dx} $$ $$ = m \frac{x_2^2 - x_1^2}{2} + (-m x_1 + y_1) (x_2 - x_1) $$ $$ = \frac{\Delta y}{\Delta x} \frac{(\Delta x)(x_1+x_2)}{2} + y_1 \Delta x - \frac{\Delta y}{\Delta x} x_1 \Delta x $$ $$ = \frac{\Delta y (x_1+x_2)}{2} - x_1 \Delta y + y_1 \Delta x $$ $$ = (\frac{(x_1+x_2)}{2} - x_1) \Delta y + y_1 \Delta x $$ $$ = \frac{\Delta x}{2} \Delta y + y_1 \Delta x $$ $$ = (\frac{\Delta y}{2} + y_1) \Delta x $$ $$ \therefore <y^2> = \frac{1}{x_n - x_1} \sum_{1}^{n-1}{(\frac{\Delta y_i}{2} + y_i) \Delta x_i} $$

My second question: Does this make sense?