I am supposed to perform an integral of function $y(x)$ from say $x_1$ to $x_2$. Now the issue is I don't have an actual function $y(x)$, but I do have a list of values for $y$ and $x$.
In what way could I calculate the "integral"? It's okay if I have one value of the "integral" for each value of $x$ and $y$. So instead of ending up with a function as the result of my integral, I would end up with another list of values of the same length as the list of $x$ and $y$ values.
$\endgroup$ 12 Answers
$\begingroup$Use the trapezoid rule. You can find information on this here:
The integral can be interpreted as the area "under" the curve. You can approximate this area using the trapezoid rule, which will give you a value.
$\endgroup$ 2 $\begingroup$For sure, as pbs answered, adding the surface of the trapezoids is a way to go.
However, you can do slightly better if, by chance, you have an odd number of data points. The method consists in fitting three data points by a parabola and integrate the resulting equation.
Consider points $(x_1,y_1)$, $(x_2,y_2)$, $(x_3,y_3)$ and you want the area between $x_1$ and $x_3$. So, make $$y=a+b x+c x^2$$ and adjust the coefficients $a,b,c$ in order they be along the parabola. They are given by $$c=\frac{x_3 (y_2-y_1)+x_2 (y_1-y_3)+x_1 (y_3-y_2)}{(x_1-x_2) (x_1-x_3) (x_2-x_3)}$$ $$b=\frac{y_1-y_2}{x_1-x_2}-c(x_1+x_2)$$ $$a=y_1-bx_1-cx_1^2$$ Now $$\int_{x_1}^{x_3}y\, dx=a (x_3-x_1)+\frac b2 (x_3^2-x_1^2)+\frac c6 (x_3^3-x_1^3)$$ Just do it for all triplets $[x_1,x_2,x_3]$, $[x_3,x_4,x_5]$, $[x_5,x_6,x_7]$, $\cdots,$ $[x_{2n-1},x_{2n},x_{2n+1}]$.
If you have an even number of points, do the same up to only one pair of point remains and use the trapezoid rule for this last piece.
All of the above was done assuming that the $x_i$'s are not equally spaced. If they were, there are very nice, simple and accurate methods to perform this integration. If you face such a case, post it and I shall put more in my answer.
$\endgroup$