You've almost got it. Sorry my last comment was a bit vague and didn't answer the question well, I was kinda tired that night.
Anyway, you have the EV cap at the lower level right; the thing you don't seem to understand is this:
If you get all your EV points at level 40 (let's say in a basic 255 Attack and 255 Special Attack spread), then you'd need 10 EVs points to get the stat point according to the equation, thus meaning you'd get 25 points in Attack from the EVs and 25 in Special Attack as well. However, when you level up, the number of EVs required for a stat increase lowers.
The thing you don't seem to understand is: the number (10) that, with 255 EVs in the two stats, caused you to get 25 stat points in your offensive stats will lower, meaning the 255 will be worth more when you level up. From what you said you seem to think that the number stays at 25, but this is the area where you are wrong.
For example:
At level 5, you need 80 EV points to get the stat increase. If you reach the cap of 510 at this level, you would only have accumulated 6 stat points overall. However, the number of 6 increases.
At level 20, those 510 EVs you got back at level 5 would not be worth 6 stat points anymore. According to the equation, a level 20 reqires 20 EVs in the stat to get the increase, thus meaning the 510 you earned at level 5 would be worth 25 stat points (with 10 EV points extra that aren't worth anything due to not reaching the number of 20).
This continues as you level up; the amount of points you got from the training at the lower level according to the equation does not "freeze".
I probably missed a question on something there, if you have anymore queries (likely, I'm bad at explaining things), do comment agin. I'll continue to try to answer at the best of my ability.