# Integrating a list of values

The data given here

data = Table[Clip[Sin[x], {0, 1}], {x, 0, 2 \[Pi], 0.1}]


generates the following curve

ListPlot[data]


I want to know, how to compute the integral of this curve using only the data given above.

• If you only have a list of function values, you need to give the step size as well. – J. M.'s ennui Apr 1 '19 at 12:56

## 4 Answers

Assuming the stepsize is 0.1 as suggested by the construction of the Table, you can calculate:

0.1*Total[data]


to get the numerical integral. To visualize the integral and plot it you can ListPlot:

0.1*Accumulate[data]


Hence:

data = Table[Clip[Sin[x], {0, 1}], {x, 0, 2 \[Pi], 0.1}];
ListPlot[{data, 0.1*Accumulate[data]}] • How is the function Accumulate related to Integration? – H. Kenan Apr 1 '19 at 15:16
• As you are accumulating the data, you are integrating the function up to that point. So this is the answer to your statement that you "want the integral as a plot." – bill s Apr 1 '19 at 16:02
• Thanks, @bill, it works nice. But looking at the definition of Accumulate[], it is not immediately clear how it should give an integral when multiplied by the stepsize. I mean given the definition Accumulate[i,j,k]=i,i+j,i+j+k, how does this lead to integration? – H. Kenan Apr 2 '19 at 9:12
• This is called the Rieman approximation to the integral. – bill s Apr 2 '19 at 16:40
• Correct me if I am wrong. In a Rieman sum, nth term is not the sum of all (n-1) terms, which seems to be the case with Accumulate[]. en.wikipedia.org/wiki/Riemann_sum – H. Kenan Apr 2 '19 at 17:10

It seems that Simpson's rule has not been mentioned yet, which is the result from a 2nd-order interpolation and will have a smaller error than that from a 1st-order one. So according to the formula, the inputs are the List of samples of the function data and the step size h:

simpsoncoefficients[n_] := SparseArray[{1 -> 1, -1 -> 1, i_?EvenQ -> 4}, n, 2]
integral[data_, h_] := (h/3) simpsoncoefficients[Length[#]].# &[data]


Then integral[data, 0.1] gives 2.00024.

Using Tai's method:

ω = ConstantArray[0.1, Length[data]];
ω[] *= 0.5;
ω[[-1]] *= 0.5;
ω.data


Alternatively

a = Table[{x, Clip[Sin[x], {0., 1.}]}, {x, 0, 2 π, 0.1}];
Integrate[Interpolation[a][x], {x, a[[1, 1]], a[[-1, 1]]}]


2.00038

• For Interpolation you can also play with the InterpolationOrder option to increase the accuracy (sometimes). In this case it doesn't do much though. – Roman Apr 1 '19 at 13:05
• Jepp. The reseason is the kink in the middle of the integral. This way, one cannot profit from higher order quadrature rules. Trapezoidal rule is almost optimal. – Henrik Schumacher Apr 1 '19 at 13:07
• I want the integral as a plot, a curve. Any way of doing that? – H. Kenan Apr 1 '19 at 14:16

You mention that you want the integral as a plot in a comment; I wonder if the following is what you had in mind. Here I am using your definition of data, and assuming a $$0.1$$ step size, as hinted at by your Table expression.

tuples = Transpose@{Range[0, 2 Pi, 0.1], data};

Show[
Plot[
NIntegrate[Interpolation[tuples][x], {x, 0, xmax}, Method -> "Trapezoidal"],
{xmax, 0, 2 Pi}, PlotLegends -> {"integral"}
],
ListPlot[
Style[tuples, Thick, ColorData],
Mesh -> All, MeshStyle -> Directive[Black, PointSize[0.01]],
PlotLegends -> {"data"}, Joined -> True
]
] 