Finding definition interval of a function where maximum average value is largest

Jardik568

New member
Joined
Aug 22, 2020
Messages
2
I have (possibly discontinuous) function f(x) defined on continuous intervals. Given segment length A, I am looking for interval <x;x+A> where maximum average value of the function is the largest.

Lets say function f(x) = x for x in <0;3> and f(x) = 1 for x in <3;6>. Value of the function is not defined outside of these intervals.
A is always less than difference between highest and lowest possible value of x (A < 6).

I know I can get average value between x and x+A by integrating the function by parts and dividing it by length of individual part. But I have no idea how to find value of x where such averege value would be largest.

For now I am using "dumb" solution by splitting the whole interval of definition values to segments of length A with step A/10 and calculating integral in each position and remembering where the value was highest. The solution is really slow and sometimes inaccurate.

Is there a way to actually calculate this numericaly?
 

Attachments

  • function.png
    function.png
    15.9 KB · Views: 6
So, first of all, thinking about the graph itself and where the average value would occur,
do you see where the max average value would be:
if A < 2?
if A > 5?

What is x if A < 2?
What is x if A > 5 ?

What is x if 2 <= A <= 5 ?
 
The graph I posted before is quite a simple one, however the function can be more complicated. I will try to explain better what my problem is:

I have only values of x (position) and y (force). I then create a piecewise function from the input, interpolating every two neighbour values using linear function / polynomial of maximum order 1. This can result in arbitary number of linear functions, with and without discontinuities between them (there can be 2 different values for given x, from left and from right).

Now this whole piecewise function is further split into more piecewice functions at points, where it changes sign. From these I need to get maximum average value of the function under a segment of length A. If A is greater than whole segment, I simply calculate average of the whole segment. If A is smaller than whole segment, I need to find position x, where the segment with maximum average value starts, so I can calculate the average.

If there was only single polynomial in the piecewise function, I can find the solution easily (attached image 1). Problem is when there are multiple polynomials and every can have different width. Sometimes there would be several polynomials that would fit under the segment of A.

I just can't come up with algorithm, that I could use to find x, other than the one I described in original post (go step-by-step and keep calculating integrals). I understand that this is no forum about algorithms, but if I knew how to do this mathematically, I might be able to come up with something.
 

Attachments

  • f2.png
    f2.png
    48.7 KB · Views: 2
  • f3.png
    f3.png
    10.7 KB · Views: 2
Top