3
$\begingroup$

I'm pretty new to Mathematica (as in been playing with it for about a day), and would like to get an average of some currents (the y-co-ordinate in the data that follows), with respect to time (the x-co-ordinate). However the data was taken at irregular time intervals. I resolved that i was probably best to go about this, by plotting the data, interpolating, Numerically integrating (I assume mathematica basically just uses the trapezium rule for this) and then dividing by the number of points provided by the interpolating (the number of trapeziums). I'm happy to revise this method, that's just where I'm up to so far.

I've managed to use ListLinePlot with InterpolatingOrder->3 to plot my data and interpolate, but can't figure out how integrate the data sets to get the average current.

dataNi58 = {{60, 17.5}, {229, 53.5}, {516, 52.8}, {767, 40.8}}
dataMo72 = {{83, 4.5}, {128, 12.8}, {161, 18.0}, {290, 22.0}, {349, 
22.6}, {382, 24.0}, {475, 25.0}, {520, 25.8}, {541, 26.1}, {613, 
26.0}, {650, 25.8}, {671, 25.7}, {726, 25.8}, {830, 22.7}, {848, 
23.3}, {955, 23.2}, {1071, 23.5}, {1171, 24.3}, {1219, 
23.8}, {1321, 24.2}, {1386, 24.3}}

ListLinePlot[{dataNi58, dataMo72}, PlotTheme -> "Detailed", 
PlotRange -> {{0, 1400}, {0, 60}}, 
PlotLabel -> 
HoldForm["Comparison of Current Yields from a Nickel and a \
Molybdenum Cathode (Interpolated)"], 
PlotLegends -> {"Cathode 58 - Nickel" , "Cathode 72 - Molybdenum"}, 
PlotMarkers -> Automatic, PlotStyle -> 96, Axes -> True, 
Frame -> {{True, False}, {True, False}}, 
FrameLabel -> {HoldForm["Current (\[Mu]A)"], 
HoldForm["Cathode Runtime (minutes)" ]}, 
LabelStyle -> {GrayLevel[0], Bold}, ImageSize -> {850, 550}, 
InterpolationOrder -> 3]

Thanks for any help.

$\endgroup$
5
  • $\begingroup$ Try this: average[lst_] := Mean[Transpose[lst][[2]]] and then apply it to your lists. For example average[dataNi58] yields 41.2. Have fun! $\endgroup$ Jun 15, 2017 at 14:30
  • $\begingroup$ It may help the clarity of your question if you strip the formatting details from your code, to make it more legible. $\endgroup$
    – MarcoB
    Jun 15, 2017 at 14:30
  • $\begingroup$ Unless you have some sort of physics-based reason for assuming a 3rd order interpolation for the Nickel plot, I'd be wary of that resulting plot. Can you argue from physics that the curve should be concave downwards? $\endgroup$
    – MikeY
    Jun 15, 2017 at 17:05
  • $\begingroup$ This is a fair assessment. Honestly, the reason it's set to 3 is that i was playing with it to see what difference it made to the curves and that was what it was on last before posting this question. Though to be fair, a somewhat concave plot (imagine the molybdenum one reflected about x=1400) is expected. This data represents the current yield of a sputter source on an accelerator I operate. You expect a rise as the sputter crater forms, a plateau as the sputter area remains roughly constant for several hours, then a decline as you start to hit the bottom of the cathode and run out of $\endgroup$
    – Epideme
    Jun 19, 2017 at 14:16
  • $\begingroup$ material. The Nickel cathode was almost used up, whereas the Molybdenum cathode still had many hours to go. The lifetimes of each material seems ot vary wildly. In truth, neither of these data sets are 'serious data', I just used them as a practice set whilst i figured out how to get Mathematica to make the graphs and output the average I wanted. In reality, i'd have no real reason to compare Nickel to Molybdenum as the comparison doesn't make sense, but I wanted to know how to do it for when i came to plot actual data (Which would be of cathodes of the same element, and >70 data points) $\endgroup$
    – Epideme
    Jun 19, 2017 at 14:17

1 Answer 1

3
$\begingroup$

this is two ways to do "classical" trapezoidal integration on the raw data:

Integrate[
 Interpolation[dataMo72, InterpolationOrder -> 1][x] , {x, 
  dataMo72[[1, 1]], dataMo72[[-1, 1]]}] 

30020.4

 (-Subtract @@ #[[All, 1]]) Mean[#[[All, 2]]] & /@ 
     Partition[dataMo72, 2, 1] // Total

or

  MovingAverage[#[[2]], 2].Differences@#[[1]] &@Transpose@dataMo72

30020.4

to get the mean value do :

Integrate[
  Interpolation[dataMo72, InterpolationOrder -> 1][x] , 
   {x, dataMo72[[1, 1]], dataMo72[[-1, 1]]}]/
     (Subtract @@ dataMo72[[{-1, 1}, 1]])

or

MovingAverage[#[[2]], 2].Differences@#[[1]]/
   Subtract @@ #[[1, {-1, 1}]] &@Transpose@dataMo72

23.0394

You can set InterpolationOrder higher in the first case and get a somewhat different result. It is debatable whether that is an improvement, and potentially will give a highly inaccurate result in case of noisy data.

$\endgroup$
4
  • $\begingroup$ Thanks for the methods... Integrate[ Interpolation[dataMo72, InterpolationOrder -> 1][x], {x, dataMo72[[1, 1]], dataMo72[[-1, 1]]}] (-Subtract @@ #[[All, 1]]) Mean[#[[All, 2]]] & /@ Partition[dataMo72, 2, 1] // Total Just returned 30020.4 twice for me. However doing... Integrate[ Interpolation[dataMo72, InterpolationOrder -> 1][x], {x, dataMo72[[1, 1]], dataMo72[[-1, 1]]}] %/(Subtract @@ dataMo72[[{-1, 1}, 1]]) Did return 23.0394. I must confess I don't really understand why the arguments on 'dataMo72 are [[1,1]] and [[-1,1]] though $\endgroup$
    – Epideme
    Jun 15, 2017 at 15:47
  • $\begingroup$ dataMo72[[1,1]] and dataMo72[[-1,1]] are referencing the first and last x values in the data. Likewise (Subtract @@ dataMo72[[{-1, 1}, 1]]) is the full x span. I put the full expressions for the mean values up.. $\endgroup$
    – george2079
    Jun 15, 2017 at 16:19
  • $\begingroup$ Thank you George, that's helped me immensely, both the method and the explanation, very much appreciated. I think that's my question considered solved. Both datasets return about the expected values. $\endgroup$
    – Epideme
    Jun 19, 2017 at 9:56
  • $\begingroup$ Out of interest, do you know I could display this average within the graphic for the graph? Like physically on the grapjh itself. $\endgroup$
    – Epideme
    Jun 19, 2017 at 14:47

Your Answer

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

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