4
$\begingroup$

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.

$\endgroup$
1
  • 4
    $\begingroup$ If you only have a list of function values, you need to give the step size as well. $\endgroup$ Commented Apr 1, 2019 at 12:56

4 Answers 4

4
$\begingroup$

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]}]

enter image description here

$\endgroup$
6
  • $\begingroup$ How is the function Accumulate related to Integration? $\endgroup$
    – H. Kenan
    Commented Apr 1, 2019 at 15:16
  • 1
    $\begingroup$ 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." $\endgroup$
    – bill s
    Commented Apr 1, 2019 at 16:02
  • $\begingroup$ 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? $\endgroup$
    – H. Kenan
    Commented Apr 2, 2019 at 9:12
  • $\begingroup$ This is called the Rieman approximation to the integral. $\endgroup$
    – bill s
    Commented Apr 2, 2019 at 16:40
  • 1
    $\begingroup$ Each term in the Rieman sum is the area of the rectangle. The area is the height of the curve times the width of the box -- hence it is the value of the data at that point times the stepsize 0.1. When you integrate, you are adding up all the areas of all the boxes... hence accumulating the value. $\endgroup$
    – bill s
    Commented Apr 2, 2019 at 18:09
3
$\begingroup$

Using Tai's method:

ω = ConstantArray[0.1, Length[data]];
ω[[1]] *= 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

$\endgroup$
3
  • $\begingroup$ For Interpolation you can also play with the InterpolationOrder option to increase the accuracy (sometimes). In this case it doesn't do much though. $\endgroup$
    – Roman
    Commented Apr 1, 2019 at 13:05
  • $\begingroup$ 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. $\endgroup$ Commented Apr 1, 2019 at 13:07
  • $\begingroup$ I want the integral as a plot, a curve. Any way of doing that? $\endgroup$
    – H. Kenan
    Commented Apr 1, 2019 at 14:16
3
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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[97][2]],
    Mesh -> All, MeshStyle -> Directive[Black, PointSize[0.01]],
    PlotLegends -> {"data"}, Joined -> True
  ]
]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.