The error is because you have increased the number of elements in the time array x too high for the number of elements in the result array F. When you asked for x = 0:40:520 you got 14 elements to match the first 14 elements in F - note that F(14) is not the last F - but when you used x = 0:20:520 you got 27 time elements with only 26 result elements.
I think this raises two problems for you.
First, do you need to increase the number of results or decrease your time interval? For example, is it meant to stop at t = 500 instead?
Second, you're supposed to span the whole interval even with h = 40, so you need to find a way to make the number of results equal to the number of times. For example, you could pull out every other sample until you reach the end:
h = 20;
x = 0:h:520;
F = [0 139 298 433 685 1026 1279 1373 1490 1634 1800 1986 2417 2651 ...
2915 3303 3516 3860 4216 4630 5092 5612 6184 6760 7327 7581];
n = length(x);
sampling_rate = ceil(40/h);
F_sampled = F(1:sampling_rate:end);