I have to integrate a function and need to split the area under the function in segments of equal area. Of all the areas I get, I need the values of the borders on the x axis.

I found an approach with NDSolve and WhenEvent to specify the area size (Nintegrate until a certain value is reached). However, I don't get it to work. For simplicity, my function I want to integrate is simply a straight line. I used an interpolating function to get the function of the straight line, because eventually I will also come from discrete points and have to fit a more complicated function. Here is what I have so far:

timeVector = Table[x, {x, 0, 99}];
yVector = Table[2.5, {100}];(*my function*)
xy = {timeVector, yVector} // Transpose;
interpol = Interpolation[xy];

(*here the actual problem*)

   WhenEvent[Integrate[interpol[t] == desiredArea, Sow[t]], 
    "StopIntegration"]}, t, {t,0,99}]]

How can I make the WhenEvent function detect multiples of the area?


1 Answer 1


Perhaps this?

   x'[t] == interpol[t], x[0] == 0,
   mark[0] == desiredArea,
   WhenEvent[x[t] == mark[t], Sow[t]; 
    mark[t] -> mark[t] + desiredArea]}, x, {t, 0, 99}, 
  DiscreteVariables -> {mark}]]

Mathematica graphics

Since x[t] will be the integral of interpol[t] from 0 up tp t, we just need to set a mark for the next time the area increases by desiredArea every time the mark is reached. Note that the interval at the end ({96, 99} in the example) will have an area less than or equal to desiredArea.

  • $\begingroup$ Thanks! This was exactly what I was looking for. Just did not think of the substitution of x[t] ==mark[t] in the WhenEvent. $\endgroup$
    – Niki
    Nov 6, 2016 at 10:37
  • $\begingroup$ @Niki You're welcome. $\endgroup$
    – Michael E2
    Nov 6, 2016 at 10:40

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.