Copying a simple example from [here](https://money.stackexchange.com/a/26662/11768), showing 4 deposits and 3 withdrawals.

*Planning to retire in 4 months and draw monthly income of £1000 (present value) for 3 months, adjusted for inflation.  APR is 8% and inflation is 4%.  What should the pot be?*

![enter image description here][1]

Calculating the monthly rates, assuming effective annual rates.

    inf = 0.04;
    i = (1 + inf)^(1/12) - 1 = 0.00327374

    apr = 0.08;
    m = (1 + apr)^(1/12) - 1 = 0.00643403

To illustrate the calculation, say we know at month 3 immediately after the final deposit, the pension pot `p` should be £3010.57

In month 4 it will have grown by `(1 + m)` and the inflation adjusted withdrawal will be `w (1 + i)^4`, where `w = £1000`.  So the pension will decrease like so

    p = 3010.57
    p = p (1 + m) - w (1 + i)^4 = 2016.78
    p = p (1 + m) - w (1 + i)^5 = 1013.28
    p = p (1 + m) - w (1 + i)^6 = 0

This can be calculated in one go using a formula

    o = 4  .  .  the month number
    n = 3  .  .  the number of months
    p = 3010.57

    ((1 + i)^o (-(1 + i)^n + (1 + m)^n) w)/(i - m) + (1 + m)^n p = 0

and more usefully, it can be expressed as a formula for `p`

    p = ((1 + i)^o (1 + m)^-n ((1 + i)^n - (1 + m)^n) w)/(i - m) = 3010.57

So we need £3010.57 in month 3 for inflation adjusted withdrawals of £1000.

Starting with deposit `d` and increasing it to compensate for inflation

    p = d
    p = p (1 + m) + d (1 + i)^1 = 2.00971 d
    p = p (1 + m) + d (1 + i)^2 = 3.0292 d
    p = p (1 + m) + d (1 + i)^3 = 4.05854 d

This can also be calculated with a formula

    q = 3  .  .  the final month number

    p = (d ((1 + i)^(1 + q) - (1 + m)^(1 + q)))/(i - m) = 4.05854 d

We know `p = 3010.57`

    ∴ d = 3010.57/4.05854 = 741.79

The above can be expressed as a formula for `d`

    d = ((i - m) p)/((1 + i)^(1 + q) - (1 + m)^(1 + q)) = 741.79

So the first deposit will be £741.79

The next month the deposit will be `£741.79 (1 + i) = £744.21` etc.

The first withdrawal will be `£1000 (1 + i)^4 = £1013.16` etc.

Putting the steps together.

    inf = 0.04;
    i = (1 + inf)^(1/12) - 1 = 0.00327374

    apr = 0.08;
    m = (1 + apr)^(1/12) - 1 = 0.00643403

    o = 4  .  .  the first withdrawal month number
    n = 3  .  .  the number of withdrawal months
    w = 1000  .  the present value of the withdrawal amount

    p = ((1 + i)^o (1 + m)^-n ((1 + i)^n - (1 + m)^n) w)/(i - m) = 3010.57

    q = 3  .  .  the final deposit month number

    d = ((i - m) p)/((1 + i)^(1 + q) - (1 + m)^(1 + q)) = 741.79

These same formulas can be used on a more realistically scaled calculation.

For example, suppose someone at age 25 wants to withdraw $1000 per month present value from age 65 to 100.  Inflation is 2% pa and interest is 3% pa (effective rates).

    (65 - 12) * 12  = 480 deposit months
    (100 - 25) * 12 = 900 months overall

    inf = 0.02;
    i = (1 + inf)^(1/12) - 1 = 0.00165158

    apr = 0.03;
    m = (1 + apr)^(1/12) - 1 = 0.00246627

    o = 480
    n = 420
    w = 1000

    p = ((1 + i)^o (1 + m)^-n ((1 + i)^n - (1 + m)^n) w)/(i - m) = 784011.41

    q = 479

    d = ((i - m) p)/((1 + i)^(1 + q) - (1 + m)^(1 + q)) = 606.00

[![enter image description here][2]][2]

[![enter image description here][3]][3]


  [1]: https://i.sstatic.net/aCjF6.png
  [2]: https://i.sstatic.net/epA3L.png
  [3]: https://i.sstatic.net/8j9gQ.png