1

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];
desiredArea=20;

(*here the actual problem*)

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

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

Niki
  • 960
  • 4
  • 14

1 Answers1

2

Perhaps this?

Reap[NDSolve[{
   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.

Michael E2
  • 235,386
  • 17
  • 334
  • 747