Interpolation, keep average over certain range to fixed value

9 Ansichten (letzte 30 Tage)
Simon Schraml
Simon Schraml am 14 Sep. 2017
Hey :) I have some data for the monthly averages of a certain parameter.
Now I would like to create some varying data for the individual days of the year from this. I need it in order to have some varying inputs for a dynamic simulation. The values should not be scattered chaotically but somewhat continuous transition from one value to the next. I thought I could do so by using interpolation or fitting a curve to this data. However, the problem is that I need for the integral of each month (hence the average of the values of the respective month) to stay as the provided averages.
Is there any way to do such a thing in Matlab? I was thinking of creating a piecewise polynomial or interpolation data somehow, but i don't know how to keep it to the fixed average value.
I tried 1-D interpolation, but the average values of the month didn't stay the same:
clear all
clc
x = [0 31 59 90 120 151 181 212 243 273 304 334 365]
v = [1000 2000 500 300 700 1200 1400 1500 1100 1200 1000 800 800] %average value of that month
stairs(x,v) %this is how the data currently looks.
%average is used for each day of the month
v(13)=[] %deletes value, was only used for depiction
xq = 1:1:365
for i=1:length(x)-1
middle(i)=x(i+1)-0.5*(x(i+1)-x(i)) %middles of the months
end
figure
vq1 = interp1(middle,v,xq)
vq2 = interp1(middle,v,xq,'spline')
vq3 = interp1(middle,v,xq,'v5cubic')
plot(middle,v,'o',xq,vq1,':.',xq,vq2,'x',xq,vq3);
I hope it is clear what I mean.
Thanks for any input! :)

Antworten (2)

John D'Errico
John D'Errico am 14 Sep. 2017
Sorry. But interpolation tools (including any that I have ever written) do not allow a constraint that lets you tell it to maintain the average over one or more specific time period(s). That does not mean you could not do so, but you would need to develop an interpolating spline code of some sort with that constraint explicitly implemented.
If you do write your own, be careful with a simple piecewise approximation, as you may wish to require the interpolant to be continuous, or even differentiable. This requires more than simply creating a set of functions that apply over segments. You will need to learn to tie them together, as in a spline.
  1 Kommentar
Simon Schraml
Simon Schraml am 14 Sep. 2017
Thanks very much for your reply!
I was fearing something like that. I just found an interesting paper on the topic that explains the procedure for a simple procedure to maintain averages when doing linear interpolation (found here: http://www.sciencedirect.com/science/article/pii/S0022247X06001703) The case is very similar to mine.
Chapter 2 here mentions that linear interpolation yields a mean value over month n, which can be represented in matrix form as:
where e, f and g are calculated depending on the number of days of succeeding months.
The point is to calculate some pseudo data from your data, which will give you the right mean when linearly interpolated. This can be done by multiplying the data with the inverse of matrix A.
I have just tried to apply this fix as described in chapter 4 of the paper. For some reason though, the mean of the interpolated pseudodata doesn't match the given average. I can't find my error...
I have attached the .m file.
Thanks for any tips!

Melden Sie sich an, um zu kommentieren.


Simon Schraml
Simon Schraml am 14 Sep. 2017
Another possibility i have thought of is creating random points (e.g. normally distributed) around the mean and organizing them in some way to the whole curve as smooth as possible. However, i can't think of a good possibility / algorithm for that. The random points for some data (unorganized) look something like this
But while there would probably be some oscillation as well in reality, the values should not change so abruptly from month to month. So i don't think this approach is really usable.
  1 Kommentar
Michael König
Michael König am 7 Mai 2020
Hey, just out of curiosity - did you figure out a solution for your problem? I'm working on something that might require a similar solution. I have mean speeds of a car driving between scattered points along a route and want to interpolate a continous function with transmissions between those means around the waypoints at which the driving time and distance was taken. Did you figure something out that could help me in any way?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by