I'm playing around with the built-in matlab `movmean` function which, using default options, creates the simple and centered moving average of given data (I'm pretty sure there is a built-in function in other programming languages too).

I noticed that in some cases the curve badly fits/smoothens the data at the last values, consider the following example

```
data = [386 379 295 238 159 190 384 402 552 347 463 259 412 481 523 589 611 479 320 403 642];
plot(movmean(data,7)) % 7 since data are collected daily and show cycles of length 7
hold on
plot(data)
legend('movmean','data','Location','northwest')

```

Since data are increasing, the movmean is supposed to increase too, while it drops at the end (look at the image below).
Is there a way to avoid this behaviour?

p.s. if you don't have the `movmean` function you can use this

```
S = [];
for c = 1:numel(data)
    f = [c-3:c-1 c c+1:c+3]; % centered with 3 values on left and 3 on right
    f = f(f>0 & f<=numel(data));
    S(c) = sum(data(f))/numel(f);
end

```

[![enter image description here](https://i.stack.imgur.com/C2kEl.png)](https://i.stack.imgur.com/C2kEl.png)